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)AsStringDim shaAsNew SHA1ManagedDim passwordBytesAsByte() = Encoding.UTF8.GetBytes(password)'compute the hashDim hashAsByte() = sha.ComputeHash(passwordBytes)Return Convert.ToBase64String(hash)EndFunctionthis 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
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 = MySQLUpdateEnd Withcnxn.Open()Dim RecordsUpdated As Integer = MyCmd.ExecuteNonQueryThanks 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.ClickDim password As String = contra.Text ' "contra" Is a ToolStripTextBoxDim 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 recordMe.theOleDbCommand = New OleDbCommand("INSERT INTO [Personal.db] (contra) VALUES (contra.string)")Me.theOleDbCommand.Parameters.Add(new OleDbParameter("?p1", OleDbType.fieldType).Value = encriptadoMe.theOleDbCommand.Connection.Open()Me.theOleDbCommand.ExecuteNonQuery()Me.theOleDbCommand.Connection.Close()End SubAs 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 SubI 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.
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
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 = encriptadonow the problem arrives in the next line
Me.theOleDbCommand.Connection.Open() "error= Object reference not set to an instance of an object."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