maxs

I am creating Login screen in vb.net and I want to verify username and password when user click on login button, and I am using Microsoft access database as datasource. Can some one tell me how to code Login button . Please some one help me or give me some sample for login button.

thank you

maxs



Re: Visual Basic Express Edition vb.net Login screen , Code for Login button

ahmedilyas

sure

basically you need to perform a select query to select the username and password and see if that matches the user inputs. Simple connect to the database, perform a query and use a DataReader (OleDbDataReader) to see if there were any results returned - if they did then you know that the authentication was valid. Here is a quick example and hope it helps:



Dim connection as new OleDbConnection(ConnectionStringHere)
Dim command as new OleDbCommand("SELECT [ID] FROM [Table] WHERE [usernameField] = @username AND [passwordField] = @password", connection)

Dim usernameParam as new OleDbParameter("@username", Me.txtUsername.Text)
Dim passwordParam as new OleDbParameter("@password", Me.txtPassword.Text)

command.Parameters.Add(usernameParam)
command.Parameters.Add(passwordParam)

command.Connection.Open()
Dim reader as OleDbDataReader = command.ExecuteReader()
if reader.HasRows then
MessageBox.Show("you are authenticated")
else
MessageBox.Show("username and or password are not found")
end if

command.Connection.Close()

Be sure to import the System.Data.OleDb namespace to access the OleDb classes and replace the text inputs to obtain the username/password correctly in addition to replacing the column/field names in the query to your database column/field names.

Basically it is simply creating the command to execute on the database level and giving it the correct statement and parameters - the parameters values are obtained from the input source (username/password in this case from the user) to see if they exist in the database. It then executes a data reader to see if any rows have been returned when executing that query, and if there are the HasRows property in the datareader will return true, else false.

Does this help






Re: Visual Basic Express Edition vb.net Login screen , Code for Login button

maxs

Thank you for your quick response. I really appriciate that.

I am using following code

If TextBox1.Text = ds.Tables(0).Rows(0).Item("userid").ToString And TextBox2.Text = ds.Tables(0).Rows(1).Item("password").ToString Then

MsgBox("login successful")

Else

MsgBox("invalid userid and password")

End If

Is above coding is resonable to you or would you recommend that , please let me know.

Dim usernameParam as new OleDbParameter("@username", Me.txtUsername.Text)
Dim passwordParam as new OleDbParameter("@password", Me
.txtPassword.Text)

why did you declare variable above varialble

and one line below


command.Parameters.Add(usernameParam)
command.Parameters.Add(passwordParam)

please respond me

thank you

maxs





Re: Visual Basic Express Edition vb.net Login screen , Code for Login button

ahmedilyas

it is better to use the code I had supplied earlier as datasets are expensive and should be used when dealing with a larger number of records. In addition to this, there is no comparison to be done as the query we have performed will simply return us a record, if found, or not - the query does the hard work for us instead of going through the dataset/datatable and seeing if the value there matches the value we are wanting to match (username/password)

Could I also ask what you mean by "why did you declare variable above variable" I don't quite follow that :-)






Re: Visual Basic Express Edition vb.net Login screen , Code for Login button

maxs

I asked because I did not get that what that varialble is doing, but I got it now.

sorry for confusion. you have solve my problem.

the code you have provided will it go throgh 100's and 1000's row of records, or Do I have to write loop so Login button can go through the every single row.

also tell me why you are using @ infront of username.

again thanks for your quick response

maxs





Re: Visual Basic Express Edition vb.net Login screen , Code for Login button

ahmedilyas

no worries.

no, you don't need to loop through at all - the database does this for us, we are just querying the database with a given condition and if it evalutes to true it will return back the resultset we told it to give back to us (the columns).

"@" is used for the Parameters to give to the command object. using this approach (parameterized queries) is safer/securer and prevent database attacks as well as having cleaner code than say string concatinations. This is the standard way of giving parameters to the command object (OleDb/SQL). It tells the database that it should be expecting a parameter and the indication of that is generally using the @ infront of the variable name, in this case @username or @password






Re: Visual Basic Express Edition vb.net Login screen , Code for Login button

maxs

I tried to compiled code you have supplied and it works fine

thank you for helping me

maxs





Re: Visual Basic Express Edition vb.net Login screen , Code for Login button

myKing_20m

hi,

i tried your code to connect to sql server, but their is no HasRows property for the reader.

how can i do this using sql server connection

thanks





Re: Visual Basic Express Edition vb.net Login screen , Code for Login button

kokomo

Hi, can this be use for the ADO connection