Inderjeet Kaur

Inderjeet Kaur

  • NA
  • 24
  • 3.2k

Why code return false value when username and password are correct?

Apr 8 2021 4:11 AM
class Student : DbConnection
{
public string stud_Username { set; get; }
public string stud_Password { set; get; }
public bool verify_User()
{
connectDb.Open();
SqlDataReader rd;
bool check = false;
using (var cmd = new SqlCommand())
{
cmd.CommandText = "SELECT * FROM stud_Login_Table WHERE Username=@user AND Password=@pass";
cmd.CommandType = CommandType.Text;
cmd.Connection = connectDb;
cmd.Parameters.Add("@user", SqlDbType.VarChar).Value = stud_Username;
cmd.Parameters.Add("@pass", SqlDbType.VarChar).Value = stud_Password;
rd = cmd.ExecuteReader();
while (rd.Read())
{
MessageBox.Show("Connected to database..");
}
connectDb.Close();
}
return check;
}
 
 
 
DbConnection conn = new DbConnection();
Student stud = new Student();
 
 
 
private void loginButton_Click(object sender, EventArgs e)
{
stud.stud_Username= studUser.Text;
stud.stud_Password = studPass.Text;
bool verify = stud.verify_User();
if (verify==true)
{
MessageBox.Show(" Logged in ");
Home home = new Home();
home.Show();
this.Hide();
}
else
{
MessageBox.Show("Check your Username and password !!");
}
 
 

Answers (2)