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

Fiq 26 Reputation points
2020-03-19T17:12:06.037+00:00

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 Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,681 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Rebin Qadir 151 Reputation points
    2020-03-19T18:39:11.43+00:00

    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