Login Form not working
As you will be able to gather from this I am extremely new to Visual Studio most of the experience i have is from access 2003 VB and even that is limited.
I am trying to write and application and starting at the very beginning i am stuck. I have a login script shown below. Originally it connected to an access database and worked fine however I have changed the connection string to connect to a local SQL Express installation.
If I type the username wrong it returns the correct error i.e. username wrong, however no matter what I do it will not load the mainscreen form when I enter the correct credentials. I have a user in my database calledjamesh with a password ofpass.Is anyone able to tell me what I am doing wrong with code?
I also have a secondary question which I would appreciate help with. What is the piece of code dr.item for? I have been searching google and other sources and cannot an expaination of the function.
Any and all help would be appreciated.
PublicClass Login
Inherits System.Windows.Forms.Form
Dim iCountAsInteger
Dim frmMainAsNew MainScreen
PrivateSub btnLogin_Click(ByVal senderAs System.Object,ByVal eAs System.EventArgs)Handles btnLogin.Click
Dim ConStringAsString ="Provider=SQLNCLI;Server=localhost\sqlexpress;Database=memphis;Trusted_Connection=yes;"'Windows Authentication
Dim DBConAsNew OleDb.OleDbConnection(ConString)
g_login =Me.txtUsername.Text
Strpassword =Me.txtPassword.Text
If g_login =""Or strpassword =""Then
MessageBox.Show("You are missing information. Please make sure that both the username and password fields are filled out.","Missing Info")
Me.txtUsername.Focus()
Return
EndIf
Dim strsqlAsString ="SELECT [UserID], [password] FROM Users WHERE [UserID]='" & g_login &"' "
Dim cmAsNew OleDb.OleDbCommand(strsql, DBCon)
Dim drAs OleDb.OleDbDataReader
Dim validAsBoolean =False
Dim HasRowsAsBoolean =False
Try
DBCon.Open()
dr = cm.ExecuteReader
If dr.HasRowsThen
While dr.Read
If Strpassword = dr.Item("password")Then
valid =True
EndIf
EndWhile
HasRows =True
EndIf
dr.Close()
Catch exOAs OleDb.OleDbException
MessageBox.Show(exO.Message)
Catch exAs Exception
MessageBox.Show(ex.Message)
Finally
If DBCon.State = ConnectionState.OpenThen
DBCon.Close()
EndIf
cm =Nothing
dr =Nothing
DBCon.Dispose()
GC.Collect()
EndTry
iCount = iCount + 1
If valid =TrueThen
Me.Hide()
frmMain.Show()
ElseIf iCount = 3Then
MessageBox.Show("You have entered the incorrect 3 times. Your account has been disabled.","Invalid Info")
Me.Close()
ElseIf HasRows =FalseThen
MessageBox.Show("Invalid user name, try again!","Invalid Info")
Me.txtUsername.Focus()
Me.txtUsername.Text =""
Me.txtPassword.Text =""
Else
MessageBox.Show("Invalid password, try again!","Invalid Info")
Me.txtPassword.Focus()
Me.txtPassword.Text =""
EndIf
EndSub
PrivateSub Cancel_Click(ByVal senderAs System.Object,ByVal eAs System.EventArgs)Handles Cancel.Click
Me.Close()
EndSub
EndClass

