Here I have created sample for you that takes Username and Password and Match that credential in database if entered credential is valid then user will logged into the system else he/she will get appropriate error message.
I hope this will help you out.
C#.Net
Namespaces
using System.Data.SqlClient;
using System.Configuration;
C#
private void btnLogin_Click(object sender, EventArgs e)
{
int count;
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT COUNT(*) FROM Login WHERE UserName = @Userame AND [Password] = @Password", con))
{
cmd.Parameters.AddWithValue("@Userame", this.txtUsername.Text.Trim());
cmd.Parameters.AddWithValue("@Password", this.txtPassword.Text);
con.Open();
count = Convert.ToInt32(cmd.ExecuteScalar());
con.Close();
if (count > 0)
{
MessageBox.Show("You have successfully Logged in");
}
else
{
MessageBox.Show(" Invalid Username or Password");
this.txtPassword.Text = "";
this.txtUsername.Focus();
}
}
}
}
VB.Net
Namespaces
Imports System.Configuration
Imports System.Data.SqlClient
VB
Private Sub btnLogin_Click(sender As System.Object, e As System.EventArgs) Handles btnLogin.Click
Dim count As Integer
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As New SqlConnection(constr)
Using cmd As New SqlCommand("SELECT COUNT(*) FROM Login WHERE UserName = @Userame AND [Password] = @Password", con)
cmd.Parameters.AddWithValue("@Userame", Me.txtUsername.Text.Trim())
cmd.Parameters.AddWithValue("@Password", Me.txtPassword.Text)
con.Open()
count = Convert.ToInt32(cmd.ExecuteScalar())
con.Close()
If count > 0 Then
MessageBox.Show("You have successfully Logged in")
Else
MessageBox.Show(" Invalid Username or Password")
Me.txtPassword.Text = ""
Me.txtUsername.Focus()
End If
End Using
End Using
End Sub
Scrrenshot
