Rather than creating your new form inside your login method, keep your login method purely for validating the user then create the form based on its return value:
private void button1_Click(object sender, EventArgs e)
{
bool Validated = login(txt_username.Text, txt_password.Text);
if(Validated)
{
//show form
}
else
{
//notify user of invalid credentials
}
}
public Boolean login(string user,string pass)
{
SqlConnection con = new SqlConnection("Data Source=SNSS1\\SQLEXPRESS;Initial Catalog=Employee;User ID=sa;Password=eLog!234");
con.Open();
SqlCommand cmd=new SqlCommand ("select * from Tbl_password where UserName='"+user+"' and Password='"+pass+"'",con);
SqlDataReader dr = cmd.ExecuteReader();
while(dr.Read())
{
if ((dr["UserName"].ToString() == user) && (dr["Password"].ToString() == pass))
{
return true;
}
}
return false;
}
You may also want to reconsider the while(dr.Read()) section. If you call dr.Read() when no records have been returned you will throw an exception. Take a look at the dr.HasRows property and see if you can streamline the logic in that section.
Post your changes and let us know if you get stuck :)
Ryshad
Nearly a Posting Virtuoso
1,307 posts since Aug 2009
Reputation Points: 512
Solved Threads: 247
Skill Endorsements: 10
Ryshad
Nearly a Posting Virtuoso
1,307 posts since Aug 2009
Reputation Points: 512
Solved Threads: 247
Skill Endorsements: 10