Lee Robinson 2007

On my application that i am currently developing i want the user to be able to sign in and as a means of verification i have the following code

Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click

If UsernameTextBox.Text = "abc" And PasswordTextBox.Text = "123" Then

Me.Close()

Dim myForm2 As New Manager

myForm2.Show()

Else

MsgBox("Incorrect username or password")

End If

End Sub

Now in my database i have binded to the project. i have a Table called manager. and two feilds

username

password

now what i want to do is compare the input string to these values that recide in my table.

Any idea's

any help would be greatly appreciated



Re: Visual Basic Language Accepting a username and password as a string and compering it to them stored in a DataBase

Dick Donny

Can I ask what database you are using and whether you intend to encrypt the password data

Richard





Re: Visual Basic Language Accepting a username and password as a string and compering it to them stored in a DataBase

Lee Robinson 2007

of course

its SQL Server 2005, im not planning on encrypting it as it will just be sitting on the local machine

i just want a stamtent that will look to a table in the database to see if the password and username are there





Re: Visual Basic Language Accepting a username and password as a string and compering it to them stored in a DataBase

Lee Robinson 2007

Any idea's anyone im still struggling with this concept



Re: Visual Basic Language Accepting a username and password as a string and compering it to them stored in a DataBase

Dick Donny

SELECT COUNT(*) FROM dbo.tbl_Users WHERE username = '' AND password = '';

If the above statement returns one row then the username/password combination is correct (obviously change the statement to refer to the correct table and columns)

Shout if you need further information.

Richard





Re: Visual Basic Language Accepting a username and password as a string and compering it to them stored in a DataBase

Lee Robinson 2007

cheers for that but how would i go about incoperating that into my if statment as above, so if that line of code does return avalue then load if a page

if not chuck out an eror message

would i need to assign it to a varible





Re: Visual Basic Language Accepting a username and password as a string and compering it to them stored in a DataBase

Lee Robinson 2007

This is what i got so far.

Dim x = SELECT COUNT(*) FROM dbo.tbl_Manager WHERE username = '' AND password = ''; as new

If x = True Then

Dim myForm2 As New Manager

myForm2.Show()

Else

MsgBox("Incorrect username or password")

End If

am i thinking along the right line's i mea i am getting some syntatical error's





Re: Visual Basic Language Accepting a username and password as a string and compering it to them stored in a DataBase

PEng1

Well you are thinking along the right lines but you will need to make just a few changes.

You will need to Import System.Data.SqlClient then this code should get you started.

Code Snippet

Dim ValidUser As Integer

Dim ValidateUser As New SqlCommand( _

"SELECT Count(*) FROM dbo.tbl_Manager WHERE UserName ='" _

& UserName.Text & "' AND Password = '" _

& Password.Text & "'")

Using Conn As New SqlConnection( _

"Your Connection String Goes Here")

Try

Conn.Open()

ValidUser = ValidateUser.ExecuteNonQuery()

If ValidUser = 1 Then

'UserName and Password have been validated

'Do whatever

Else

MsgBox("Incorrect UserName and Password!", _

MsgBoxStyle.Exclamation, "Unable to Login")

End If

Catch ex As Exception

MsgBox(ex.Message)

Finally

Conn.Close()

End Try

End Using

Hope this helps.






Re: Visual Basic Language Accepting a username and password as a string and compering it to them stored in a DataBase

Lee Robinson 2007

Thanks that was a big help

im only having problems with one line of code in that

Code Snippet
Using Conn As New SqlConnection( _

its saying that it isnt defined, thought that was valid tho

any ideas





Re: Visual Basic Language Accepting a username and password as a string and compering it to them stored in a DataBase

PEng1

Did you import the System.Data.SqlClient into the class

At the top of your class you need to put

Code Snippet

Imports System.Data.SqlClient






Re: Visual Basic Language Accepting a username and password as a string and compering it to them stored in a DataBase

Lee Robinson 2007

Thanks, thats where i was going wron, ive got rid of that problem, sorry im still a bit of a noob at this

the only issues i have now is:

its not liking the under scores _ Keep's giving me identifier expected i.e

Code Snippet

Dim ValidateUser As New SqlCommand( _"SELECT Count(*) FROM dbo.tbl_Manager WHERE UserName ='" _& UserName.Text & "' AND Password = '" _& Password.Text & "'")

also it keeps saying that ValidateUser is not declared, but it is. why is that

This is wha ive got so far

Code Snippet

' trying to read it from a database

Dim ValidUser As Integer

Dim ValidateUser As New SqlCommand( _"SELECT Count(*) FROM dbo.tbl_Manager WHERE UserName ='" _& UserName.Text & "' AND Password = '" _& Password.Text & "'")

Using Conn As New SqlConnection( _".\SQLEXPRESS;AttachDbFilename=C:\Documents and Settings\Afroboy\My Documents\Visual Studio 2005\Projects\Odeon Till Managment System\Odeon Till Managment System\OTMS.mdf;")

Try

Conn.Open()

ValidUser = ValidateUser.ExecuteNonQuery()

If ValidUser = 1 Then

Me.Close()

Dim myForm2 As New Manager

myForm2.Show()

Else

MsgBox("Incorrect UserName and Password!")

End If

Catch ex As Exception

MsgBox(ex.Message)

Finally

Conn.Close()

End Try

End Using

End Sub





Re: Visual Basic Language Accepting a username and password as a string and compering it to them stored in a DataBase

Aw Ali

You almost got it, but you did not try your solution. Yes remove that underscore and modify the ValidUser declaration like this. There is another unwanted underscore just before the SELECT string.

Dim ValidateUser As New SqlCommand( "SELECT Count(*) FROM dbo.tbl_Manager WHERE UserName ='" & UserName.Text & "' AND Password = '" & Password.Text & "'")

It should be in one line of code.

For your information, the underscore is for breaking lines in VB so as to increase readability. If you would hit ENTER while the cursor is after the underscore, it would work too.






Re: Visual Basic Language Accepting a username and password as a string and compering it to them stored in a DataBase

Lee Robinson 2007

This code now appears to work, however, when i run the code i get a message box displaying on the screen.

ExecutionNonQuery: Connection property has not been initalized

what does this mean exactly, how do i initilize the connection string. Any Idea's





Re: Visual Basic Language Accepting a username and password as a string and compering it to them stored in a DataBase

decyclone

You have to assign a connection to a command before executing it.

Example :

[C#] :
SqlConnection Conn = new SqlConnection("<Connection String>");

SqlCommand Command = new SqlCommand();

Command.CommandText = "Select * From.....";
Command.Connection = Conn;

Command.ExecuteNonQuery();


[VB] :
Dim Conn As SqlConnection = new SqlConnection("<Connection String>")

Dim Command As SqlCommand = new SqlCommand()

Command.CommandText = "Select * From....."
Command.Connection = Conn

Command.ExecuteNonQuery()






Re: Visual Basic Language Accepting a username and password as a string and compering it to them stored in a DataBase

Dick Donny

Lee Robinson 2007 wrote:

This code now appears to work, however, when i run the code i get a message box displaying on the screen.

ExecutionNonQuery: Connection property has not been initalized

what does this mean exactly, how do i initilize the connection string. Any Idea's

Conn.Open()

ValidateUser.Connection = Conn

ValidUser = ValidateUser.ExecuteNonQuery()