C# window login form application
I want to create c# window login form application and I want that login form to connect to my SQL database and open the second form. I tried most tutorials but is failing to connect to my database. Please help
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Vincent Maverick DuranoRank #238 · 10.3K reputationPosted Jul 10, 2018, 3:50 PM EDT
Ranjit PowarRank #553 · 4.4K reputationPosted Jul 8, 2018, 6:27 AM EDT
Rafnas T PRank #280 · 8.8K reputationPosted Jul 8, 2018, 6:42 PM EDT
rajendra singhRank #1,521 · 1K reputationPosted Jul 8, 2018, 7:18 PM EDT
abra man44 reputationPosted Jul 9, 2018, 7:47 AM EDT
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
Vincent Maverick DuranoRank #238 · 10.3K reputationPosted Jul 9, 2018, 1:23 PM EDT
Rafnas T PRank #280 · 8.8K reputationPosted Jul 10, 2018, 6:09 AM EDT
abra man44 reputationPosted Jul 10, 2018, 6:44 AM EDT
abra man44 reputationPosted Jul 10, 2018, 7:36 AM EDT
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
End Sub
End Class