VB.Net : how to create login window and authenticate through MySQL database

vb_1

 

Code  behind Page

Imports MySql.Data
Imports MySql.Data.MySqlClient
Public Class LoginForm1
    Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
        Dim flag As Boolean = False
        If (String.IsNullOrEmpty(UsernameTextBox.Text) Or String.IsNullOrEmpty(PasswordTextBox.Text)) Then
            MessageBox.Show("username or passord missing")
            Exit Sub
        End If
        flag = CheckUser(UsernameTextBox.Text, PasswordTextBox.Text)
        If flag = True Then
            MessageBox.Show("Sucessful Login")
        Else
            MessageBox.Show("invalid user/password")
        End If
    End Sub

    Private Sub Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel.Click
        Me.Close()
    End Sub

    Private Function CheckUser(ByVal u As String, ByVal p As String) As Boolean
        Dim flag As Boolean = False
        Dim cnn As New MySqlConnection
        Dim cmd As New MySqlCommand
        Dim reader As MySqlDataReader

        cnn.ConnectionString = "Server=127.0.0.1;Database=library;Uid='root';Pwd='';"
        cmd.CommandText = "select * from appusers where uname='" + u.Trim() + "' and upass='" + p.Trim() + "'"
        cmd.Connection = cnn
        cnn.Open()
        reader = cmd.ExecuteReader()
        If (reader.HasRows) Then
            flag = True

        End If
        cnn.Close()

        Return flag
    End Function

End Class

Output

vb_2


MySQL database


vb_sql

 


download MySQL/.Net connector!

click  on  MySQL/.Net connector


 

1. After  Successful download install on your machine

vb4

2. Add the above   reference to Visual Studio 2010

vb3