SAVE ENCRIPTED PASSWORD INTO ACCESS DATABASE

i have an input text that asks for the user to put a password on it..and a button that sends the string to this function i found in the forum to encrypt the code..

PublicSharedFunction HashPassword(ByVal passwordAsString)AsString

Dim shaAsNew SHA1Managed

Dim passwordBytesAsByte() = Encoding.UTF8.GetBytes(password)

'compute the hash

Dim hashAsByte() = sha.ComputeHash(passwordBytes)

Return Convert.ToBase64String(hash)

EndFunction

this function returns the password encrypted, now...i need to save it in my Access database...

Personal.mdb

EmployeeID:string; Name: String; LastName: String & Password:String.

My idea is just to save the result from my function in the Personal Password registry. No then when i need to use it, i ask for password and compare it with the one i have in my database... but i am stuck in the saving.. HELP

[2847 byte] By [ivanatilca] at [2007-12-25]
# 1

Use an SQL Update staement executed with a command object:

Dim MySQLUpdate As String = "UPDATE Table SET Password = 'blah' WHERE (((EmployeeId)=1))"

Dim MyCmd As New OleDb.OleDbCommand()

Dim cnxn As New OleDb.OleDbConnection("ConnectionString")

With MyCmd

.Connection = cnxn

.CommandType = CommandType.Text

.CommandText = MySQLUpdate

End With

cnxn.Open()

Dim RecordsUpdated As Integer = MyCmd.ExecuteNonQuery

DMan1 at 2007-9-3 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 2

you have to create and replace the variable names appropriately to your code.

take a look at this on how to insert data into a database:

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=753872&SiteID=1

take a look at my first response and the 2nd code snippet on how to insert values into an MS Access database.

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=728535&SiteID=1

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=764599&SiteID=1

Anything that is with SQL can be replaced using the OleDb classes instead of the Sql classes generally.

the action is:

  • create a database connection (connectionstring)

  • create your insert command for example, giving it the statement to execute (as shown in the examples)

  • give it the values to insert in the statement or via the parameter methods (better) again as shown in the examples

  • execute the query

  • ahmedilyas at 2007-9-3 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
    # 3

    Thanks for the links! they where very helpfull i think i am in the right way..now let me explain this...i coded this using some samples you put in the link

    Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click

    Dim password As String = contra.Text ' "contra" Is a ToolStripTextBox

    Dim encriptado As String = HashPassword(password) 'HashPassword is the function that returns de string encrypted password

    'Ahmedilyas code i have in my main form

    'Dim theOleDbCommand As New OleDbCommand()

    'Dim theOleDbDataAdapter As New OleDbDataAdapter(theOleDbCommand)

    'Dim theDataSet As New DataSet()

    'Insert record

    Me.theOleDbCommand = New OleDbCommand("INSERT INTO [Personal.db] (contra) VALUES (contra.string)")

    Me.theOleDbCommand.Parameters.Add(new OleDbParameter("?p1", OleDbType.fieldType).Value = encriptado

    Me.theOleDbCommand.Connection.Open()

    Me.theOleDbCommand.ExecuteNonQuery()

    Me.theOleDbCommand.Connection.Close()

    End Sub

    As you can see...the button i have to save my password encrypted is in the toolstrip...i added the stuff but the code underlines some of them (the ones i put in yellow), does this happen because i declared the OleDBComand in the main form?, or because i dragged and dropped a DataGridView from my data source window? ... This is the code i have in my form

    Private Sub Seguridad_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    'TODO: This line of code loads data into the 'PersonalDataSet.Personal' table. You can move, or remove it, as needed.

    Me.PersonalTableAdapter.Fill(Me.PersonalDataSet.Personal)

    Dim theOleDbCommand As New OleDbCommand()

    Dim theOleDbDataAdapter As New OleDbDataAdapter(theOleDbCommand)

    Dim theDataSet As New DataSet()

    End Sub

    I dragged dataGrid because i add a query to make a search engine, the one i found in this link http://windowssdk.msdn.microsoft.com/en-us/library/hbsty6z7.aspx I would appresiate if you can explain me de difference between DBCommand, Adapter and Set. And whats wrong in this code.

    ivanatilca at 2007-9-3 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
    # 4

    the reason you get the lines under the ones you have highlighted is because you dont have this:

    'Dim theOleDbCommand As New OleDbCommand()

    declared at a global level (at the top of the class, to make it publically/globally accessible)

    the OleDbCommand is a class that allows you to execute/create queries to perform on the database on the OleDb Connection (MS Access for example) DataAdapter is an adapter that works closely with the OleDbCommand in this case, and the dataset - the dataset holds records retrieved from the database for example and are modified by the datagridview for example, when bound as a datasource.

    the dataadapter allows us to fill a dataset with records (command created using the OleDbCommand) and perform update on the database itself by using the rows modified in the dataset, when we edit records for example.

    this is also wrong:

    Me.theOleDbCommand = New OleDbCommand("INSERT INTO [Personal.db] (contra) VALUES (contra.string)")

    Me.theOleDbCommand.Parameters.Add(new OleDbParameter("?p1", OleDbType.fieldType).Value = encriptado

    it should be:

    Me.theOleDbCommand = New OleDbCommand("INSERT INTO [Personal.db] (contra) VALUES (@p1)")

    Me.theOleDbCommand.Parameters.Add(new OleDbParameter("@p1", OleDbType.fieldType).Value = encriptado

    the fieldType is what you have to put in, press the period after the OleDbType syntax and you will get a list of fields, select one which is relevant for this field in the database. So if this field was Text in the database, select Text from the drop down list

    ahmedilyas at 2007-9-3 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
    # 5

    Me.theOleDbCommand.Parameters.Add(new OleDbParameter("@p1", OleDbType.LongVarChar).Value = encriptado)

    I''ve put LONGVARCHAR the code doesnt identify "String" (thats the type i am using) Now...i run it..and the program gives me this error

    The OleDbParameterCollection only accepts non-null OleDbParameter type objects, not Boolean objects

    i've changed the line for this one (i read about it in help panel)

    Me.theOleDbCommand.Parameters.Add("@contra", OleDbType.VarChar, 20).Value = encriptado

    now the problem arrives in the next line

    Me.theOleDbCommand.Connection.Open() "error= Object reference not set to an instance of an object."

    ivanatilca at 2007-9-3 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
    # 6
    perhaps your Connection object is non existant....check to see what the state value of Connection is, if its nothing then you need to make an instance of it
    ahmedilyas at 2007-9-3 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...