Saturday, 17 August 2013

C# login using sql database not working

C# login using sql database not working

I need to open a new form if the user name and the password is correct but
I can't get this code to work, if I enter a correct username or a password
it does nothing.
try {
string connection = @"Data Source=DX-PC;Initial
Catalog=login;Integrated Security=True";
SqlConnection cn = new SqlConnection(connection);
cn.Open();
string userText = user.Text;
string passText = pass.Text;
SqlCommand cmd = new SqlCommand("SELECT ISNULL(Username, '') AS
Username, ISNULL(Password,'') AS Password FROM log WHERE Username =
@username and Password = @password", cn);
cmd.Parameters.Add(new SqlParameter("username", userText));
cmd.Parameters.Add(new SqlParameter("password", passText));
SqlDataReader dr = cmd.ExecuteReader();
try {
dr.Read();
if (dr["Username"].ToString().Trim() == userText &&
dr["Password"].ToString().Trim() == passText) {
MessageBox.Show("This message won't Display");
}
}
catch {
MessageBox.Show("Invalid Username or Password");
}
dr.Close();
cn.Close();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}

No comments:

Post a Comment