private void btnLogin_Click(object sender, EventArgs e)
{
try
{
if (txtUsername.Text.Length > 0 && txtUsername.Text != null && txtPassword.Text != null && txtPassword.Text.Length > 0)
{
btnClose.Enabled = false;
btnLogin.Enabled = false;
int id = CheckUserId(txtUsername.Text.ToString(), txtPassword.Text.ToString());
if (id > 0)
{
frmMain objfrmMain = new frmMain();
objfrmMain.Show();
this.Hide();
}
else
{
MessageBox.Show("Invalid User.");
txtPassword.Text = "";
txtUsername.Text = "";
txtUsername.Focus();
}
}
else
{
if (txtPassword.Text.Length <= 0)
{
MessageBox.Show("Please Enter Password.");
txtPassword.Focus();
}
else
{
MessageBox.Show("User Name Should Not Be Blank");
txtUsername.Focus();
}
}
}
catch (Exception objexp)
{
MessageBox.Show(objexp.ToString());
}
finally
{
btnClose.Enabled = true;
btnLogin.Enabled = true;
}
}
}
internal int CheckUserId(string UserName, string Password)
{
SqlConnection objSqlConnection;
SqlCommand objSqlCommand;
clsConnection objclsConnection = new clsConnection();
int result = 0;
objSqlConnection = //put your connection string;
try
{
objSqlConnection.Open();
objSqlCommand = objclsConnection.GetCommand("your_procedure", ref objSqlConnection);
objSqlCommand.CommandType = CommandType.StoredProcedure;
//UserName
objSqlCommand.Parameters.Add("@Username", SqlDbType.VarChar);
objSqlCommand.Parameters["@Username"].Value = UserName;
//Password
objSqlCommand.Parameters.Add("@Password", SqlDbType.VarChar);
objSqlCommand.Parameters["@Password"].Value = Password;
//Found
objSqlCommand.Parameters.Add("@Found", SqlDbType.Int);
objSqlCommand.Parameters["@Found"].Direction = ParameterDirection.Output;
objSqlCommand.ExecuteNonQuery();
result = Convert.ToInt32(objSqlCommand.Parameters["@Found"].Value.ToString());
}
catch (Exception objexcp)
{
MessageBox.Show(objexcp.ToString());
}
finally
{
objclsConnection.CloseConnection(ref objSqlConnection);
}
return result;
}