Hey everyone, kinda new to all this...really need some help:
Ok so I want to creat a log in and registration form in VB connecting to a sql database.
Databse Name: Users
Table Name: UserDetails
Table Fields: ID, Username, Password
The ID field was specifed as primary and self incrementing by 1 and the table has one username and password (being Id: 1, Username: peter, Password: admin).
The Login form works, it checks to see if the user exists, if they do, they gain access, if not a message box is displayed.
The Registration form however does not and I have no idea why, its probably something simple but yeah, hence the reason Im here!
When I register I can add a user fine, and it wont allow me to add the same user, but when I look at the table, it is not updated :S and I have no idea why!!
Even after registering the user closing the application and starting again, say I registered the following:
Username: Fred
Password: 123
Then I login with these credentials, it works. When I close the program and reopen the program, it still works and will not allow me to register a new user being Fred, 123.
However after 3 or 4 minutes when I try again, the user no longer exisits and I can re-register with set credentials?!?!!!
Weird :(
Please Help :icon_cry: The code is shown below (1st the login form that works, second the registration that doesnt).
Public Class Login
Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
Dim connection As New SqlClient.SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Users.mdf;Integrated Security=True;User Instance=True")
Dim CheckDetailsCommand As New SqlClient.SqlCommand
Dim adaptor As New SqlClient.SqlDataAdapter
Dim dataset As New DataSet
CheckDetailsCommand.CommandText = "SELECT * FROM [UserDetails] WHERE username='" + txtUsername.Text + "'AND password='" + txtPassword.Text + "';"
connection.Open()
CheckDetailsCommand.Connection = connection
adaptor.SelectCommand = CheckDetailsCommand
adaptor.Fill(dataset, "0")
Dim count = dataset.Tables(0).Rows.Count
If count > 0 Then
Me.Close()
Else
MsgBox("Incorrect Login Details Entered: Please Check Username & Password", MsgBoxStyle.Critical)
txtPassword.Clear()
txtUsername.Clear()
End If
End Sub
Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
Me.Close()
End Sub
End Class
Public Class Register
Private Sub btnAccountOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAccountOK.Click
Dim connection As New SqlClient.SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Users.mdf;Integrated Security=True;User Instance=True")
Dim CheckDetails As New SqlClient.SqlCommand
Dim adaptor As New SqlClient.SqlDataAdapter
Dim dataset As New DataSet
Try
CheckDetails.CommandText = "SELECT * FROM [UserDetails] WHERE username='" + txtUsername.Text + "'AND password='" + txtPassword.Text + "';"
connection.Open()
CheckDetails.Connection = connection
adaptor.SelectCommand = CheckDetails
adaptor.Fill(dataset, "0")
Dim count = dataset.Tables(0).Rows.Count
If count > 0 Then
MsgBox("This user already exisits: Please use different credentials", MsgBoxStyle.Critical)
txtPassword.Clear()
txtUsername.Clear()
Else
Dim AddDetails As New SqlClient.SqlCommand
AddDetails.CommandText = "INSERT INTO [UserDetails] (Username, Password)VALUES ('" + txtUsername.Text + "','" + txtPassword.Text + "')"
Dim data As SqlClient.SqlDataReader
Dim adapter2 As New SqlClient.SqlDataAdapter
AddDetails.Connection = connection
adapter2.InsertCommand = AddDetails
data = AddDetails.ExecuteReader
MsgBox("You've Registered!")
End If
Finally
Me.Close()
End Try
End Sub