Page 1 of 2

Tutorial - How to create a MySQL Login Application

Posted: Wed Feb 24, 2010 5:03 pm
by Scottie1972
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
Code: Select all
Imports MySql.Data.MySqlClient
This will import the MySQL.Data Library that you Referenced earlier.

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
    Private connStr As String = "Database=DATABASENAME;" & _
        "Data Source=192.168.0.1;" & _
        "User Id=MySQLUser; Password=MySQLUserPassword;" & _
        "Connection Timeout=20"
Now double click on the "Register" button and add this code to it.
Code: Select all
    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
All this does is open the registration form for new users.

OK now on to the Login.
Double click the login button.
Code: Select all
    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
This querys the database for the entered username and password.
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
Code: Select all
Imports MySql.Data.MySqlClient
This will import the MySQL.Data Library that you Referenced earlier.

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
    Private connStr As String = "Database=DATABASENAME;" & _
        "Data Source=192.168.0.1;" & _
        "User Id=MySQLUser; Password=MySQLUserPassword;" & _
        "Connection Timeout=20"
While we at at it, let go ahead and add the UpdateRecord() Function as well.
Code: Select all
    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
So now all that is left is to code in the Submit button click even.
Code: Select all
    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
NOTES - You might want to add some code so that a blank TextBox cant not INSERT blank data into the database.

Now the above function VerifyAccountCreated() is to check that registration was good.
You can do this by using this function.
Code: Select all
    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
All this does is query the database to make sure that the user was able to register.
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.

Re: Tutorial - How to create a MySQL Login Application

Posted: Wed Feb 24, 2010 5:52 pm
by hungryhounduk
nIce One Scottie :)
Well laid out and EASY to Follow...

Excellent

Cheers

Chris

Re: Tutorial - How to create a MySQL Login Application

Posted: Wed Feb 24, 2010 9:44 pm
by tedhead2
Fix it:
Error 1: 'MySqlConnection' is ambiguous in the namespace 'MySql.Data.MySqlClient'. C:\Users\Jordan\documents\visual studio 2010\Projects\Mysql Login1\Mysql Login1\Form1.vb 16 35 Mysql Login1

Re: Tutorial - How to create a MySQL Login Application

Posted: Wed Feb 24, 2010 10:49 pm
by Scottie1972
tedhead2 wrote:
Fix it:
Error 1: 'MySqlConnection' is ambiguous in the namespace 'MySql.Data.MySqlClient'. C:\Users\Jordan\documents\visual studio 2010\Projects\Mysql Login1\Mysql Login1\Form1.vb 16 35 Mysql Login1
I have tested this code on my private server. Other then changing the database settings in the connectionstring, this code works fine. My be you should either check to make sure you have the Connectors Installed
"C:\Program Files\MySQL\MySQL Connector Net 6.2.2\Assemblies"
or
you need to remove the reference from your project and reference the library to fit your filesystem.

Re: Tutorial - How to create a MySQL Login Application

Posted: Wed Feb 24, 2010 11:00 pm
by tedhead2
I tried all of those. It just doesn't work.

Re: Tutorial - How to create a MySQL Login Application

Posted: Fri Feb 26, 2010 2:56 am
by tedhead2
It dioesn't work on VB.NET '08 or '10

Re: Tutorial - How to create a MySQL Login Application

Posted: Fri Feb 26, 2010 3:06 am
by Scottie1972
tedhead2 wrote:
It dioesn't work on VB.NET '08 or '10

Fine..I will make a video, then you can watch it to see that it does work. I now it works!
The only reason my codes do not work is human error.

Sad but true.

Scottie

Re: Tutorial - How to create a MySQL Login Application

Posted: Fri Feb 26, 2010 3:26 am
by tedhead2
Scottie1972 wrote:
tedhead2 wrote:
It dioesn't work on VB.NET '08 or '10

Fine..I will make a video, then you can watch it to see that it does work. I now it works!
The only reason my codes do not work is human error.

Sad but true.

Scottie

Hehe.. I fixed it

I added 4 referances to MySQL. I did realize that, so i removed all but MySQL.Data! It works now!

Re: Tutorial - How to create a MySQL Login Application

Posted: Sun Feb 28, 2010 2:25 pm
by marve25
can you make a tut on how to setup the MySQL, got a lot of errors on it

Re: Tutorial - How to create a MySQL Login Application

Posted: Sun Feb 28, 2010 4:28 pm
by tedhead2
I might be able to help you. I know PHP and MySQL.