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

[10052 byte] By [JamesWHarrington] at [2008-1-9]
# 1

James, a few question... Could you explain a little better what you mean by

however no matter what I do it will not load the mainscreen form when I enter the correct credentials

Does the program exit at this point? (it may indicate you have the login form set as the startup form for the project and when it closes the program closes).

Do you get any error when trying to read the dr.item() value? If so then you could be retrieving a value set to DbNull and comparing a string to DbNull will throw an exception. If not, if you're concerned about connecting to SqlExpress and you are not getting an error then it appears by your code that your problem is occurring after the connection is completed (in the validation part of the code), so it would seem that it is not the root of your error. Try placing a Debug.Write(dr.item("password")) line right after your dr.Read line and see what it has for a value.

I assume you want frmMain to appear after the login form closes, Is frmMain instantiated yet?

DigBoy2000 at 2007-10-2 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 2

Basically by not working I mean this:

If I enter the username wrong, it appears to successfully query the database and chucks the error Invalid Username.

If enter tge wrong password it returns the error Invalid Password

If I enter the incorrect credentials three times it exits the program as per the icount = 3 if statement

However when I enter the correct username and the correct password according to the database entries it still throws the invalid password error.

I honestly dont know if I get an error when it tries to read the dr.item lines. This is the first time I am doing anything like this programming, and my reason for asking about dr.item is I dont know what it does. My basic interpretation is its mapping field names.

I hope this makes sense

Thanks

James

JamesWHarrington at 2007-10-2 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 3

I found the answer. My password field has field type nchar(10). A friend advised me to modify the password string to below and now it works.

If Strpassword = Trim(dr.Item("password")) Then

However if someone can explain what the dr.item, dr.read command is doing I would appreciate it

Thanks

James

JamesWHarrington at 2007-10-2 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 4

Your instinct was correct about the dr.Item property -- it mapping to a field. I don't know the exact structure of the DataReader collection but it can generally be understood as accessing a single column from the currently queued record of the returned table of results from your ExecuteReader call. With each Read call you are moving to the next record (as the results were ordered in your original query) of your query results. The Item property allows you to reference individual columns/fields within that record either by index or by column name.

DigBoy2000 at 2007-10-2 > top of Msdn Tech,Visual Basic,Visual Basic Language...