How to create a vb.net login application

In this guide, I’ll be showing you how to create a login application in visual studio using the language vb.net and the database mysql.

Be sure you already know how to connect your vb.net application to your mysql database before proceeding.

First thing that you’ll gonna need to do is to create a subroutine in which you will put these lines of code:

cmd.CommandText = "SELECT * FROM testdb WHERE Name='" & name & "' AND Password='" & course & "'"
 
   Try
 
       Dim uname As String
       Dim pword As String
 
       rdr = cmd.ExecuteReader
       If rdr.HasRows Then
           rdr.Read()
           uname = rdr("Name")
           pword = rdr("Password")
           If (name = uname And password = pword) Then form2.show()
 
 
       Else
           MsgBox("Incorrect Admin Information, please try again", MsgBoxStyle.Information)
 
      End If
   
 
 Catch
       MsgBox("Something went wrong..")
End Try

Breaking it down:

First we declared the command or the query to be used to get information from the database:

cmd.CommandText = "SELECT * FROM testdb WHERE Name='" & name & "' AND Password='" & course & "'"

Then we placed a try catch statement to catch whatever errors we might get so that it won’t make the program crash like IE.

Try
 
Catch ex As Exception
 
End Try

Then inside the try-catch, declare the variables that we will use to store the value that were going to read from the database.

Dim uname as String
Dim pword as String

Then go outside the subroutine block. And declare the data reader

Dim rdr as odbcDataReader

Go inside the subroutine block again. And set the data reader that you have just declared to use a command.

rdr = cmd.ExecuteReader

If you haven’t declared a command yet. Then declare it outside the subroutine.

Dim cmd as odbcCommand

All you have to do now is to read the data from the specific fields in the database. Set it to a variable. Then compare the values that the user inputted. In this case the public string name and password.

If rdr.HasRows Then
      rdr.Read()
      uname = rdr("Name")
      pword = rdr("Password")
      If (name = uname And password = pword) Then Form2.show
 
Else
      MsgBox("Incorrect Admin Information, please try again", MsgBoxStyle.Information)
 
End If

Then go to your login form designer view. And create an object for the class where you have made a subroutine.

Dim x as new NameOfClass

Then use this object to call the subroutines from the button click event.

x.name=textbox1.text
x.password=textbox2.text
x.loginsub()

That’s it, now you have created a vb.net login application that connects to the mysql database.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s