Below is my code for the login form. Etc, If i login as Jack, the next form will display Jack in label1. If login as david, then the next for will display david in label1. Just like using session in a web form.
The Button Login Method
private void btnLogin_Click(object sender, EventArgs e)
{
//retrieve connection information info from App.config
string strConnectionString =
ConfigurationManager.ConnectionStrings["sacpConnection"].ConnectionString;
//STEP 1: Create connection
SqlConnection myConnect = new SqlConnection(strConnectionString);
//STEP 2: Create command
string strCommandtext = "SELECT dUsername, dPassword from DOCTOR";
// Add a WHERE Clause to SQL statement
strCommandtext += " WHERE dUsername=@dname AND dPassword=@dpwd;";
SqlCommand cmd = new SqlCommand(strCommandtext, myConnect);
cmd.Parameters.AddWithValue("@dname", textUsername.Text);
cmd.Parameters.AddWithValue("@dpwd", txtPassword.Text);
try {
myConnect.Open(); // STEP 3: open connection
SqlDataReader reader = cmd.ExecuteReader(); // retrieve data
while (reader.Read()) //For Doctor
{
if (MessageBox.Show("Login Successful") == DialogResult.OK)
{
timer1.Enabled = true;
return;
}
}
//STEP 5: close connection
reader.Close();
MessageBox.Show("Invalid username or password");
}
catch (SqlException ex) { }
finally {
//STEP 5: close connection
myConnect.Close();
}
}
