question

fiq avatar image
0 Votes"
fiq asked ·

INSERT INTO (OLEDB) RAISED NO ERROR BUT NO NEW RECORD INSERTED

Hello.

I'm new to WPF migrating from Windows Form. I'm creating a non-profit system for my village management to store data related to the residents. I need to add new user for login before adding new residents data and accessing the sensitive data.

Everything went normal on debuging the application including when registering a new user. Return no error at all but still no new data is added. Already search the entire google.com but not found any solution.


         Public Sub New(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
                     '1st column is ID with Autonumber. UserID is 2nd column. UserCredential have 7 columns including ID from ID until LastLogin.
                     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() 'return 1 but no data is inserted.
                             MessageBox.Show(i)
                         End If
                         reader.Close()
                     End Using
                 Catch ex As Exception
                     MessageBox.Show(ex.Message)
                 End Try
             End Using
         End Sub


I'm using VS 2019 on Windows 10 Pro with target Framework 4.7.2. Also using latest Microsoft Office Access from Office 365.


Thanks for any help.


windows-wpf
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

rebin avatar image
0 Votes"
rebin answered ·

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


· 1 · Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hello sir.

Thanks for helping me.

My database path is

D:\Project\Resident Database\ResDB.accdb

For after publish path is

C:\Program Files (x86)\Res DB

Thanks.










0 Votes 0 · ·