Click here to Skip to main content
15,905,233 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i created a login page using c#.net as frond end and sql server as back end. i want to use it as my system login page.nobody could access windows with out this login authenticatiion..
Posted

1 solution

C#
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;
}
 
Share this answer
 
Comments
geo thomas 27-Apr-13 1:03am    
let me try

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900