I am playing with databases and today in my attempt to work with SQLCompactEdition database, I've created a local database named UserDB and have a table in it named, User.
I've logged in into admin using my app(which is another database) and through admin, I am creating a user.
Code I used for the purpose is :
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim connStr As New SqlCeConnection("Data Source=c:\users\babi\documents\visual studio 2012\Projects\ShopManagement\ShopManagement\" & "User\UserDB.sdf; Password =")
Dim name, pass, repass As String
name = TextBox1.Text
If (name.Length = 0) Then
MessageBox.Show("Enter user-name!!")
Exit Sub
End If
pass = TextBox2.Text
If (pass.Length = 0) Then
MessageBox.Show("Enter password!!")
Exit Sub
End If
repass = TextBox3.Text
If (repass.Length = 0) Then
MessageBox.Show("Re-enter password!!")
Exit Sub
End If
If (getMD5Hash(pass) = getMD5Hash(repass)) Then
Dim cmd As New SqlCeCommand("INSERT INTO User(uname, upass)VALUES(" & name & "," & getMD5Hash(pass) & ");", connStr)
connStr.Open()
cmd.ExecuteNonQuery()
connStr.Close()
MessageBox.Show("User Created!")
Exit Sub
Else
MessageBox.Show("Passwords donot match!!")
Exit Sub
End If
End Sub
When I debug, I find the querystring to be :
"INSERT INTO User(uname, upass)VALUES(abcd,827ccb0eea8a706c4c34a16891f84e7b);"
The Exception that occurs is :
An unhandled exception of type 'System.Data.SqlServerCe.SqlCeException' occurred in System.Data.SqlServerCe.dll
I cannot understand what is causing this error. If any more information is required, please ask.
EDIT
Fixed Code :
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim connStr As New SqlCeConnection("Data Source=c:\users\babi\documents\visual studio 2012\Projects\ShopManager\ShopManager\" & "ManagementDB.sdf; Password =")
Dim name, pass, repass As String
name = TextBox1.Text
If (name.Length = 0) Then
MessageBox.Show("Enter user-name!!")
Exit Sub
End If
pass = TextBox2.Text
If (pass.Length = 0) Then
MessageBox.Show("Enter password!!")
Exit Sub
End If
repass = TextBox3.Text
If (repass.Length = 0) Then
MessageBox.Show("Re-enter password!!")
Exit Sub
End If
If (getMD5Hash(pass) = getMD5Hash(repass)) Then
Dim cmd As New SqlCeCommand("INSERT INTO [User](uname, upass) VALUES('" & name & "','" & getMD5Hash(pass) & "');", connStr)
connStr.Open()
cmd.ExecuteNonQuery()
connStr.Close()
MessageBox.Show("User Created!")
Exit Sub
Else
MessageBox.Show("Passwords donot match!!")
Exit Sub
End If
End Sub
The solution to the problem is given by the combination of the two solutions! So thank you to both of you :)
Regards Priyabrata