Hi ,
I have tested your code every things went fine please share the path your database file !!
Imports System.Data.OleDb
Imports System.Security.Cryptography
Imports System.Text
Class MainWindow
Private Sub Button_Click(sender As Object, e As RoutedEventArgs)
NewRecord(txtuserid.Text, txtpassword.Text, txtfullname.Text, txtemail.Text)
txtuserid.Text = ""
txtpassword.Text = ""
txtfullname.Text = ""
txtemail.Text = ""
Exit Sub
End Sub
Public Sub NewRecord(ByVal UserID As String, ByVal myPassword As String, ByVal FullName As String, ByVal eMail As String)
Using Sql As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\ResDb.accdb")
Dim cData As New OleDbCommand("SELECT * from UserCredential where UserID = ?", Sql)
cData.Parameters.AddWithValue("UserID", UserID)
Try
Using cUser As New OleDbCommand("INSERT INTO UserCredential (UserID,Wordpass,FullName,eMail,CreationData,LastLogin) VALUES (?,?,?,?,?,?)", Sql)
Sql.Open()
Dim reader As OleDbDataReader = cData.ExecuteReader
If reader.Read = False Then
cUser.Parameters.AddWithValue("UserID", UserID)
cUser.Parameters.AddWithValue("Wordpass", SHA.GenerateSHA256String(myPassword))
cUser.Parameters.AddWithValue("FullName", FullName)
cUser.Parameters.AddWithValue("eMail", eMail)
cUser.Parameters.AddWithValue("CreationData", Now.Date)
cUser.Parameters.AddWithValue("LastLogin", Now.Date)
Dim i As Integer = cUser.ExecuteNonQuery()
MessageBox.Show(i)
End If
reader.Close()
End Using
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Using
End Sub
End Class
Public Module SHA
Function GenerateSHA256String(ByVal inputString As String) As String
Dim sha256 As SHA256 = SHA256Managed.Create()
Dim bytes As Byte() = Encoding.UTF8.GetBytes(inputString)
Dim hash As Byte() = sha256.ComputeHash(bytes)
Return GetStringFromHash(hash)
End Function
Function GenerateSHA512String(ByVal inputString As String) As String
Dim sha512 As SHA512 = SHA512Managed.Create()
Dim bytes As Byte() = Encoding.UTF8.GetBytes(inputString)
Dim hash As Byte() = sha512.ComputeHash(bytes)
Return GetStringFromHash(hash)
End Function
Private Function GetStringFromHash(ByVal hash As Byte()) As String
Dim result As StringBuilder = New StringBuilder()
For i As Integer = 0 To hash.Length - 1
result.Append(hash(i).ToString("X2"))
Next
Return result.ToString()
End Function
End Module