Friday, February 15, 2013

VB CODE FOR DESKTOP APPLICATION

IN LOGIN FORM


Imports System.Data.SqlClient
Imports System.Data
Imports System.Configuration
Public Class frmLogin

    Dim con As SqlConnection
    Dim cmd As SqlCommand
    Dim s As String
    Dim dr As SqlDataReader
    Dim objcon As New ConnectionString


    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        Application.Exit()
        Me.DialogResult = DialogResult.Cancel
    End Sub

    Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click
        Static count As Integer
        If count >= 2 Then
            MsgBox("Unauthorized Login!")
            Me.DialogResult = DialogResult.Cancel
        Else
            con = New SqlConnection(objCon.constr)
            If con.State = ConnectionState.Closed Then
                con.Open()
            End If

            Dim strpassword As String = Nothing

            s = "Select * from tblLogin where LoginID = '" & txtLoginID.Text & "'"
            cmd = New SqlCommand(s, con)
            dr = cmd.ExecuteReader()
            If dr.HasRows() Then
                dr.Read()
                'txtLoginID.Text = dr.GetString(0)
                strpassword = dr.GetString(1)


            Else
                MsgBox("Invalid Login ID/ Password")
                count = count + 1
                Exit Sub
                ' Me.DialogResult = DialogResult.Cancel

            End If
            If txtPassword.Text <> strpassword Then
                MsgBox("Invalid Login ID / Password")
                count = count + 1
                'Me.DialogResult = DialogResult.Cancel
                'Got login
            Else
                Me.DialogResult = DialogResult.OK
                Me.Close()

            End If

            con.Close()
        End If
       


    End Sub

    Private Sub frmLogin_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        txtLoginID.Text = "admin"
        txtPassword.Text = "1234"
    End Sub
End Class

No comments:

Post a Comment