Practical 27 User Login Form
Practical 27 User Login Form VB.NET Code
Imports System.Data
Imports System.Data.SqlClient
Public Class Form3
Dim con As New SqlConnection
Dim da As New SqlDataAdapter
Dim ds As New DataSet
Dim sqlquery As String
Dim i As Integer = 0
Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
con = New SqlConnection("Your Database Connection String") 'See in Your PC Database Property
con.Open()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Try
If Len(Trim(TextBox1.Text)) = 0 Then
MessageBox.Show("Enter the user name", "Input Error !", MessageBoxButtons.OK, MessageBoxIcon.Error)
Exit Sub
TextBox1.Focus()
End If
If Len(Trim(TextBox2.Text)) = 0 Then
MessageBox.Show("Enter the password", "Input Error !", MessageBoxButtons.OK, MessageBoxIcon.Error)
Exit Sub
TextBox2.Focus()
End If
con.Open()
sqlquery = "SELECT * FROM Login WHERE username='" & TextBox1.Text & "' and password='" & TextBox2.Text & "' "
da = New SqlClient.SqlDataAdapter(sqlquery, con)
da.Fill(ds, "Login")
If ds.Tables("Login").Rows.Count <> 0 Then
MessageBox.Show("Login succeed", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information)
Else
MessageBox.Show("Invalid user name and password", "Access denied", MessageBoxButtons.OK, MessageBoxIcon.Error)
i += 1
End If
If i > 2 Then
Me.Close()
End If
'calling clear method here
Catch ex As Exception
MessageBox.Show(ex.Message, "Exception generated", MessageBoxButtons.OK, MessageBoxIcon.Error)
Finally
con.Close()
End Try
End Sub
Public Sub clear()
TextBox2.Clear()
TextBox1.Clear()
End Sub
End Class
Comments
Post a Comment