locked
vb.net Login screen , Code for Login button

    Question

  • 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

    Saturday, July 07, 2007 10:57 PM

Answers

  • 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?

    Saturday, July 07, 2007 11:07 PM
  •  

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

     

    thank you for helping me

     

    maxs

     

    Saturday, July 07, 2007 11:55 PM

All replies

  • 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?

    Saturday, July 07, 2007 11:07 PM
  • 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

    Saturday, July 07, 2007 11:21 PM
  • 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 :-)

    Saturday, July 07, 2007 11:23 PM
  •  

    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

    Saturday, July 07, 2007 11:28 PM
  • 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

     

     

    Saturday, July 07, 2007 11:41 PM
  •  

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

     

    thank you for helping me

     

    maxs

     

    Saturday, July 07, 2007 11:55 PM
  • 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

    Friday, July 27, 2007 3:04 AM
  •  

    Hi, can this be use for the ADO connection?
    Wednesday, August 15, 2007 7:13 AM
  • Sorry to thread dig, but what does ahm means by "connection string here"?

    Tuesday, April 01, 2008 7:28 PM
  •  ahmedilyas wrote:

    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?



    I try to do this code but I've had error on command.Connection.Open()? I use visual studio(vb 2005)

    Thursday, November 13, 2008 6:24 AM
  • Hi I am very interested in getting this to work and but I can't seem to be able to do it.

    This is part of my code (derived from your example above)

    Dim mssql_connection as New OleDbConnection(Provider=sqloledb;Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;)

     Dim command As New OleDbCommand("Select [name] from [master..syslogins] where [name] = @username AND [password] = @password", mssql_connection )

    I've highlighted mssql_connection because I get the message saying that it is not declared. Where have I gone wrong?

    I understand as well that the concept to connet to other databases remains the same (or so I'm told) but I am having a great amount of difficulty say connecting to Sybase. I am not certain if (in the example I've given) I am using the right connection string.

    I've even got to msdn and tried to follow the example given there (where i supposedly can just drag and drop the database connection on to my form) BUT in VS 2005 I can't seem to drag the suppose database connection unto my form /design. I've been searching google groups and other groups for ages.

    Please your input would be much appreciated. Thank you.

    Wednesday, May 27, 2009 5:52 AM
  • Hi This is vennila.Im a beginner to VB.net.can u guide me to create a employee details form with the controls and connect with database thru Stored Procedure and display the details with first,delete,add and update buttons

     

    Thanks In advance

     

    I am in need

    Thursday, May 12, 2011 6:20 AM
  • hey, what if i wan to check the user position while they login?

    how should i code it? can provide me some sample to do it?

    Thank you!

    Friday, June 24, 2011 4:01 AM