check below code you need to create a stored procedure for this purpose as below
create procedure Validateuser
@username nvarchar(100),
@password nvarchar(100),
@result int output
AS
begin
if exists(select * from tblUser where USERNAME=@username and PASSWORD=@password)
BEGIN
SET @result=1
END
else
BEGIN
if exists(select * from tblUser where USERNAME=@username)
BEGIN
SET @result=2
END
else
BEGIN
if exists(select * from tblUser where PASSWORD=@password)
BEGIN
SET @result=3
END
else
BEGIN
SET @result=4
END
END
END
end
now in your button click event write like this
private void btnLogin_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(
"Data Source=GITTWO-PC\\sql2008R2;Initial Catalog=Application;Integrated Security=SSPI");
con.Open();
SqlCommand cmd = new SqlCommand("validateuser", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@username", TextBox1.Text);
cmd.Parameters.AddWithValue("@password", TextBox2.Text);
cmd.Parameters.Add("@result", SqlDbType.Int).Direction = ParameterDirection.Output;
cmd.ExecuteNonQuery();
int result=int.Parse(cmd.Parameters["@result"].Value.ToString())
if(result==1)
{
Form2 frm = new Form2();
frm.Show();
}
else if (result==2)
{
MessageBox.Show("Invalid Password", "Invalid Password");
}
else if (result==3)
{
MessageBox.Show("Invalid User Name", "Invalid User Name");
}
else if (result==4)
{
MessageBox.Show("Invalid User Name & Password", "Invalid User Name & Password");
}
con.Close();
}