Forums - C# Corner

Forum guidelines
abra man

abra man

  • 1.7k
  • 45
  • 433

visual basic window Login Form to sql Server Database

Jul 9 2018 11:43 AM
This is my code and I dont know where should the code go on the login button or the login form. please help
 
 
 -------------------------------------------------------------------------------------
 
Public Class frmLogin
Dim con As New SqlClient.SqlConnection
Dim da As New SqlClient.SqlDataAdapter
Dim ds As New DataSet
Dim sqlquery As String
Private Sub frmLogin_Load(sender As Object, e As EventArgs) Handles MyBase.Load
con.ConnectionString = "Data Source=ABRAM-PC\SQLEXPRESS;Initial Catalog=Login;Integrated Security=True"

End Sub

Private Sub btnLogin_Click(sender As Object, e As EventArgs) Handles btnLogin.Click
Try
'checking if the username and password field is null

If Len(Trim(txtUserName.Text)) = 0 Then
MessageBox.Show("Enter the user name", "Input Error !", MessageBoxButtons.OK, MessageBoxIcon.Error)
Exit Sub
txtUserName.Focus()
End If
If Len(Trim(txtPass.Text)) = 0 Then
MessageBox.Show("Enter the password", "Input Error !", MessageBoxButtons.OK, MessageBoxIcon.Error)
Exit Sub
txtPass.Focus()
End If

'executing SQL Query for retrieving the username and password from the database table

con.Open()
sqlquery = "SELECT * FROM LoginTable WHERE user_name='" & txtUserName.Text & "' and password='" & txtPass.Text & "' "
da = New SqlClient.SqlDataAdapter(sqlquery, con)
da.Fill(ds, "LoginTable")
If ds.Tables("LoginTable").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)
End If
con.Close()

clear() 'calling clear method here

Catch ex As Exception
MessageBox.Show(ex.Message, "Exception generated", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub

' declaring a method for clearing the controls
Public Sub clear()
txtPass.Clear()
txtUserName.Clear()
End Sub

' code for show password
Private Sub chkshowpass_CheckedChanged(sender As Object, e As EventArgs) Handles chkshowpass.CheckedChanged
If chkshowpass.Checked = True Then
txtPass.UseSystemPasswordChar = False
Else
txtPass.UseSystemPasswordChar = True
End If
End Sub
End Class

Answers (1)