Hi rani,
Refer below sample.
Namespaces
C#
using System.Data.SqlClient;
VB.Net
Imports System.Data.SqlClient
Code
C#
private void button1_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(txtUserName.Text) == true || string.IsNullOrEmpty(txtPassword.Text) == true)
{
MessageBox.Show("User Name and Password required.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
using (SqlConnection con = new SqlConnection(constr))
{
SqlCommand cmd = new SqlCommand("SELECT Id FROM TestUser WHERE Name=@UserName and Password=@UserPass", con);
cmd.Parameters.AddWithValue("@UserName", this.txtUserName.Text.Trim());
cmd.Parameters.AddWithValue("@UserPass", this.txtPassword.Text.Trim());
con.Open();
int result = Convert.ToInt32(cmd.ExecuteScalar());
con.Close();
if (result > 0)
{
this.Hide();
frmMain frm = new frmMain();
frm.Show();
}
else
{
MessageBox.Show(this, "Invalid User Name or Password.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
VB.Net
Private Sub button1_Click(sender As System.Object, e As System.EventArgs) Handles button1.Click
If String.IsNullOrEmpty(txtUserName.Text) = True OrElse String.IsNullOrEmpty(txtPassword.Text) = True Then
MessageBox.Show("User Name and Password required.", "Error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
Return
End If
Using con As SqlConnection = New SqlConnection(constr)
Dim cmd As SqlCommand = New SqlCommand("SELECT Id FROM TestUser WHERE Name=@UserName and Password=@UserPass", con)
cmd.Parameters.AddWithValue("@UserName", Me.txtUserName.Text.Trim())
cmd.Parameters.AddWithValue("@UserPass", Me.txtPassword.Text.Trim())
con.Open()
Dim result As Integer = Convert.ToInt32(cmd.ExecuteScalar())
con.Close()
If result > 0 Then
Me.Hide()
Dim frm As frmMain = New frmMain()
frm.Show()
Else
MessageBox.Show(Me, "Invalid User Name or Password.", "Error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
End If
End Using
End Sub
Screenshot