check this sample code....i hope it work for you....
private void btnLogin_Click(object sender, EventArgs e)
{
counter = counter + 1;
try
{
Program.Connection2Server();
string query = "select * from Users_Password where UserName='" + cboUser.Text + "'";
SqlCommand cmd = new SqlCommand(query,Program.con);
SqlDataReader dr = cmd.ExecuteReader();
dr.Read();
string s1 = dr[0].ToString().Trim();
string s2 = dr[1].ToString().Trim();
if ((s1 == cboUser.Text) && (s2 == txtpassword.Text))
{
MessageBox.Show("Login Succesfull");
if (cboUser.Text == "Admin")
{
Admin ad = new Admin();
this.Hide();
ad.Show();
}
else if (cboUser.Text == "Faculty")
{
Faculty fac = new Faculty();
this.Hide();
fac.Show();
}
else if (cboUser.Text == "Accounts")
{
Accounts acc = new Accounts();
this.Hide();
acc.Show();
}
else if (cboUser.Text == "Students")
{
Students st = new Students();
this.Hide();
st.Show();
}
Program.con.Close();
}
else
{
MessageBox.Show("Login Failed");
txtpassword.Clear();
txtpassword.Focus();
}
Program.con.Close();
}
catch (SqlException se)
{
MessageBox.Show("Error in Sql Connection" + se.ToString());
}
}