A long time ago when I was building a windows form application, that is now being made into a asp.net web application, I only had MySQL and had to use MySQL as a login system. So here is a small tutorial with how to make a login system with MySQL through vb.net
I' had some help with this from coldfiretech, who is now Code Ninja, from vbcity. He showed me how to use a Select Case.
1. Make a table with the following below, and download and install mySQL Dot Net Connector 5.1 from here.
2. Create 2 textboxes and a button. Name one textbox txtUsername and the other txtPassword. Then name the button cmdAuthenticate.
3. Copy what is below. Make sure you only copy what you need.
I' had some help with this from coldfiretech, who is now Code Ninja, from vbcity. He showed me how to use a Select Case.
1. Make a table with the following below, and download and install mySQL Dot Net Connector 5.1 from here.
Code:
CREATE TABLE `userlist` (
`ID` int(10) unsigned NOT NULL auto_increment,
`UserName` varchar(45) NOT NULL,
`Password` varchar(128) NOT NULL,
`EmailAddress` varchar(128) NOT NULL,
`Status` varchar(10) NOT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=latin1;
2. Create 2 textboxes and a button. Name one textbox txtUsername and the other txtPassword. Then name the button cmdAuthenticate.
3. Copy what is below. Make sure you only copy what you need.
Code:
Imports MySql.Data.MySqlClient
Public Class Form1
Const AppName = "MySQL Login System"
Private Sub cmdAuthenticate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAuthenticate.Click
If txtUserName.Text = "" Or txtPassword.Text = "" Then
MessageBox.Show("Please enter the required information to login.", AppName, MessageBoxButtons.OK, MessageBoxIcon.Information)
Else
Dim mMySQLConnectionString As String = "server=localhost; user id=myroot; password=mypwd; database=login"
'You will need to change the connection string above to yours
Dim conn As MySqlConnection
Dim dr As MySqlDataReader
Dim cmd As New MySqlCommand
conn = New MySqlConnection()
conn.ConnectionString = mMySQLConnectionString
Try
conn.Open() 'Always a good idea to open the connection in a try/catch block
'Before you were selecting the username and password..
'I changed that to the Status field being that its the field we need.
cmd.CommandText = "SELECT Status FROM members WHERE UserName = ?UserName AND Password = ?Password"
cmd.Parameters.Add(New MySqlParameter("?UserName", txtUserName.Text))
cmd.Parameters.Add(New MySqlParameter("?Password", txtPassword.Text))
cmd.Connection = conn
dr = cmd.ExecuteReader
If dr.HasRows() Then
'I used a datareader to hold the result of the Select Statement
'which will be the status field if the username AND password are CORRECT
'If the UserName and Password are wrong, the datareader will not hold any rows.
dr.Read()
Select Case UCase(dr(0).ToString)
Case Is = "ADMIN"
Dim F2 As New Form2
F2.Show()
Me.Close()
Case Is = "MOD"
Dim F3 As New Form3
F3.Show()
Me.Close()
Case Is = "USER"
Dim F3 As New Form3
F3.Show()
Me.Close()
End Select
Else
MsgBox("Either the User Name or Password are Incorrect. Please try again.", MsgBoxStyle.Information, AppName)
txtPassword.ResetText()
txtUserName.Focus()
End If
Catch myerror As MySqlException
MessageBox.Show("Database Error: " & myerror.Message, AppName, MessageBoxButtons.OK, MessageBoxIcon.Information)
Catch ex As Exception
MessageBox.Show("Error: " & ex.Message, AppName, MessageBoxButtons.OK, MessageBoxIcon.Information)
Finally
'The Finally Section of a Try Catch block will always fire, even when it runs into an exception.
'That makes it a good place to clean up
cmd.Parameters.Clear() 'I always clear my parameters when im done with them just to be safe
conn.Close()
End Try
End If
End Sub
End Class