Tutorial - How to create a MySQL Login Application
Posted: Wed Feb 24, 2010 5:03 pm
Tutorial:
Name:
How to create a MySQL Login Application.
Description:
In this tutorial I will show how to make 2 forms.
The Login Form (form1) and the Registration Form (form2).
Tools:
You will need to download Mysql Connectors.NET from the SUN MySQL Website.
http://dev.mysql.com/downloads/mysql/
Visual Basic Express Edition 2005/2008
NOTE
You must have you database with tables and columns already made in order to make this Tutorial work.
Lets Begin:
First Fire Up Microsoft Visual Basic 2008 and create a new Windows Form Project.
Name it MySQL Login Form, or what ever you would like.
When the form1 loads:
You need to go ahead and Add a Reference to the Project.
The reference will be to the MySQL.Data library.
Then the Add Reference window will open. On The .NET tab, scroll down until you see
"MySQL.Data"
Choose MySQL.Data then click ok.
Now lets add some controls to the form.
Add:
2 Labels
2 TextBox
2 BUttons
Should look something like this.
![Image]()
Very simple very clean. You can change to your liking after you have learn how to use this tutorial.
Control Properties:
Label1 - Username
Label2 - Password
Rename TextBox1 to txtUsername
Rename TextBox2 to txtPasword and change the PasswordChar to *
Rename Button1 to BtnRegister change Text to Register
Rename Button2 to BtnLogin change Text to Login
OK, at this point your forms a made and we are ready for some code.
Right- Click the editor and choose "View Code"
The first thing you want todo is add the Reference to the form.
On line 1: add this
Now, inside the Public Class you need to add the connectionstring.
The connectionstring is what tells the application what server it is connecting to.
OK now on to the Login.
Double click the login button.
If the user exist it will say hello. IF the user doesnt, it will open the Registration form so
that the user can register an account.
------------------------------------------------------------------------------------
(Register Form)
Now Add a New Form (form2) to the Project.
This will be the Register form.
Add 5 Labels, 5 Textbox, and 1 Button.
Label1 - Firstname
Label2 - Lastname
Label3 - Username
Label4 - Password - PasswordChar = *
Label5 - Email
TextBox1 - txtFirstname
TextBox2 - txtLastname
TextBox3 - txtUsername
TextBox4 - txtPassword
TextBox5 - txtEmail
Button1 - BtnSubmit Text - Submit
should look like this.
![Image]()
OK, at this point your forms a made and we are ready for some code.
Right- Click the editor and choose "View Code"
The first thing you want todo is add the Reference to the form.
On line 1: add this
Now, inside the Public Class you need to add the connectionstring.
The connectionstring is what tells the application what server it is connecting to.
Now the above function VerifyAccountCreated() is to check that registration was good.
You can do this by using this function.
If registration was good, you get a message box telling you that that you have registered.
If registration was bad, you get a message box telling you that something went wrong.
------------------------------------------------------------------------------------
That should be about the basic example of a MySQL Login / Registration for Vb 2008.
Name:
How to create a MySQL Login Application.
Description:
In this tutorial I will show how to make 2 forms.
The Login Form (form1) and the Registration Form (form2).
Tools:
You will need to download Mysql Connectors.NET from the SUN MySQL Website.
http://dev.mysql.com/downloads/mysql/
Visual Basic Express Edition 2005/2008
NOTE
You must have you database with tables and columns already made in order to make this Tutorial work.
Lets Begin:
First Fire Up Microsoft Visual Basic 2008 and create a new Windows Form Project.
Name it MySQL Login Form, or what ever you would like.
When the form1 loads:
You need to go ahead and Add a Reference to the Project.
The reference will be to the MySQL.Data library.
Then the Add Reference window will open. On The .NET tab, scroll down until you see
"MySQL.Data"
Choose MySQL.Data then click ok.
Now lets add some controls to the form.
Add:
2 Labels
2 TextBox
2 BUttons
Should look something like this.

Very simple very clean. You can change to your liking after you have learn how to use this tutorial.
Control Properties:
Label1 - Username
Label2 - Password
Rename TextBox1 to txtUsername
Rename TextBox2 to txtPasword and change the PasswordChar to *
Rename Button1 to BtnRegister change Text to Register
Rename Button2 to BtnLogin change Text to Login
OK, at this point your forms a made and we are ready for some code.
Right- Click the editor and choose "View Code"
The first thing you want todo is add the Reference to the form.
On line 1: add this
Code: Select all
This will import the MySQL.Data Library that you Referenced earlier.Imports MySql.Data.MySqlClient
Now, inside the Public Class you need to add the connectionstring.
The connectionstring is what tells the application what server it is connecting to.
Code: Select all
Now double click on the "Register" button and add this code to it.
Private connStr As String = "Database=DATABASENAME;" & _
"Data Source=192.168.0.1;" & _
"User Id=MySQLUser; Password=MySQLUserPassword;" & _
"Connection Timeout=20"
Code: Select all
All this does is open the registration form for new users. Private Sub BtnRegister_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnRegister.Click
Dim RegForm As New Form2
RegForm.ShowDialog()
End Sub
OK now on to the Login.
Double click the login button.
Code: Select all
This querys the database for the entered username and password. Private Sub BtnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnLogin.Click
Try
Dim query As String = "SELECT * FROM users WHERE username='" + txtUsername.Text + "' AND password=md5('" + txtPassword.Text + "');"
Dim connection As New MySqlConnection(connStr)
Dim cmd As New MySqlCommand(query, connection)
connection.Open()
Dim reader As MySqlDataReader
reader = cmd.ExecuteReader()
reader.Read()
If reader.HasRows = True Then
'If user is register do something
'Open a new form
'msgbox()
MsgBox("Hello Agian " + reader.GetString(3) + "! How are you today?")
Else
'If user hasnt registered
'open the registration form
'or a msgbox()
MsgBox("Sorry! You are not registered!")
Dim RegForm As New Form2
RegForm.ShowDialog()
End If
reader.Close()
connection.Close()
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
End Sub
If the user exist it will say hello. IF the user doesnt, it will open the Registration form so
that the user can register an account.
------------------------------------------------------------------------------------
(Register Form)
Now Add a New Form (form2) to the Project.
This will be the Register form.
Add 5 Labels, 5 Textbox, and 1 Button.
Label1 - Firstname
Label2 - Lastname
Label3 - Username
Label4 - Password - PasswordChar = *
Label5 - Email
TextBox1 - txtFirstname
TextBox2 - txtLastname
TextBox3 - txtUsername
TextBox4 - txtPassword
TextBox5 - txtEmail
Button1 - BtnSubmit Text - Submit
should look like this.

OK, at this point your forms a made and we are ready for some code.
Right- Click the editor and choose "View Code"
The first thing you want todo is add the Reference to the form.
On line 1: add this
Code: Select all
This will import the MySQL.Data Library that you Referenced earlier.Imports MySql.Data.MySqlClient
Now, inside the Public Class you need to add the connectionstring.
The connectionstring is what tells the application what server it is connecting to.
Code: Select all
While we at at it, let go ahead and add the UpdateRecord() Function as well.
Private connStr As String = "Database=DATABASENAME;" & _
"Data Source=192.168.0.1;" & _
"User Id=MySQLUser; Password=MySQLUserPassword;" & _
"Connection Timeout=20"
Code: Select all
So now all that is left is to code in the Submit button click even. Function updateRecord(ByVal query As String) As Integer
Try
Dim rowsEffected As Integer = 0
Dim connection As New MySqlConnection(connStr)
Dim cmd As New MySqlCommand(query, connection)
connection.Open()
rowsEffected = cmd.ExecuteNonQuery()
connection.Close()
Return rowsEffected
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Function
Code: Select all
NOTES - You might want to add some code so that a blank TextBox cant not INSERT blank data into the database. Private Sub BtnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSubmit.Click
Try
Console.WriteLine(updateRecord("INSERT INTO users (firstname, lastname, username, password, email) VALUES ('" + txtFirstname.Text + "','" + txtLastname.Text + "','" + txtUsername.Text + "',md5('" + txtPassword.Text + "'),'" + txtEmail.Text + "')"))
VerifyAccountCreated()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Now the above function VerifyAccountCreated() is to check that registration was good.
You can do this by using this function.
Code: Select all
All this does is query the database to make sure that the user was able to register. Public Sub VerifyAccountCreated()
Try
Dim query As String = "SELECT * FROM users WHERE username='" + txtUsername.Text + "'"
Dim connection As New MySqlConnection(connStr)
Dim cmd As New MySqlCommand(query, connection)
connection.Open()
Dim reader As MySqlDataReader
reader = cmd.ExecuteReader()
reader.Read()
'reader.GetString(3) = reads the username column in database
If reader.HasRows = True Then
MsgBox("Congrates You Are Registered!")
Else
MsgBox("Registration failed for some reason!")
End If
reader.Close()
connection.Close()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
If registration was good, you get a message box telling you that that you have registered.
If registration was bad, you get a message box telling you that something went wrong.
------------------------------------------------------------------------------------
That should be about the basic example of a MySQL Login / Registration for Vb 2008.