Pages

Friday, June 6, 2014

Vb.Net code for Login Function with SQL Server 2008 database

Here is the complete Vb.Net 2010 code for Login Function with SQL Server 2008 database

Design the Login form with Username and Password text fields and Login button and Clear button. Then paste the code given below according to the button or the text field.

For more details visit my Tech Net Article or Tweet me : @MaxRohana

Imports System.Data
Imports System.Data.SqlClient

Public Class Form1

         Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

         End Sub

        'Login Button
    Private Sub btnlogin_Click(sender As System.Object, e As System.EventArgs) Handles btnlogin.Click
    
    ConnectToSQL()

    End Sub

    'Connect to SQL method

    Private Sub ConnectToSQL()

    Dim con As New SqlConnection
    Dim cmd As New SqlCommand
    Dim Password As String
    Dim Passwrd2 As String
    Dim userName As String

    Try
        If
            'change the data source and initial catalog according to your sql server engine and data base
            con.ConnectionString = "Data Source = YOUR-PC; Initial Catalog = YOUR-DB; Integrated Security = True"
            con.Open()

            cmd.Connection = con
            'change the data fields names and table according to your database
            cmd.CommandText = " SELECT  UserName, Password FROM   Table-Name WHERE   (UserName = '" & txtUsername.Text & "' ) AND (Password = '" & txtPassword.Text & "')"

            Dim lrd As SqlDataReader = cmd.ExecuteReader()
            If lrd.HasRows Then
                While lrd.Read()

                    'Do something here
                    Password = lrd("Password").ToString()
                    userName = lrd("UserName").ToString()

                    Password2 = txtPassword.Text()

                    If Password = Password2 And userName = txtUsername.Text Then

                        MessageBox.Show("Logged in successfully as " & userName, "", MessageBoxButtons.OK, MessageBoxIcon.Information
                                        )
                        frmMain.Show()
                        Me.Hide()

                        'Clear all fields
                        txtPassword.Text = ""
                        txtUsername.Text = ""

                    End If

                End While

            Else
                MessageBox.Show("Username and Password do not match..", "Authentication Failure", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                'Clear all fields
                txtPassword.Text = ""
                txtUsername.Text = ""
            End If

        End If

    Catch ex As Exception
        MessageBox.Show("Error while connecting to SQL Server." & ex.Message)

    Finally
        con.Close() 'Whether there is error or not. Close the connection.

    End Try
 
     End Sub

    Private Sub txtUsername_TextChanged(sender As System.Object, e As System.EventArgs) Handles txtUsername.TextChanged
    End Sub
    Private Sub txtPassword_TextChanged(sender As System.Object, e As System.EventArgs) Handles txtPassword.TextChanged
    End Sub
    Private Sub btnClear_Click(sender As System.Object, e As System.EventArgs) Handles btnClear.Click
        'User clicking on cancel button only clears field
        ' and refocus to first field
        txtUsername.Text = ""
        txtPassword.Text = ""
        txtUsername.Focus()
    End Sub
End Class