I'm working at a school project and I have to make a login for my app.
Everything works fine when using the corect username and password but when I try to use other login data nothing works anymore.
private void btnLogin_Click(object sender, EventArgs e)
{
string connString =
@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source = |DataDirectory|\SKYNET.mdb;";
//string _usr = "root";
//string _pswd = "trust#1";
string _usr = txtUsername.Text;
string _pswd = txtPassword.Text;
string sql = @"select usrlg_ID, username, password from login where username = '" + _usr + "' AND password = '" + _pswd + "';";
OleDbConnection conn = null;
OleDbDataReader reader = null;
using (conn = new OleDbConnection(connString))
{
conn.Open();
OleDbCommand cmd = new OleDbCommand(sql, conn);
reader = cmd.ExecuteReader();
string dbUser = null;
string dbPass = null;
while (reader.Read())
{
dbUser = reader["username"].ToString();
dbPass = reader["password"].ToString();
}
reader.Close();
if (dbUser == _usr)
{
if (dbPass == _pswd)
{
this.Visible = false;
frmMain frmMain = new frmMain();
frmMain.ShowDialog(this);
//this.Visible = false;
}
else
{
//this.txtPassword.Text = "";
MessageBox.Show("Invalid password!");
this.txtPassword.Text = "";
}
}
else
{
//this.txtUsername.Text = "";
MessageBox.Show("Invalid username!");
this.txtUsername.Text = "";
}
conn.Close();
}
}
The point is that, when I type a wrong username it jumps to the inner else. But when I type a wrong password....it shows the same message: "Invalid username!".
I tried everything that came across my mind.
Switching IF with ELSE didn't work either.
if (dbUser != _usr)
{
//this.txtUsername.Text = "";
MessageBox.Show("Invalid username!");
this.txtUsername.Text = "";
}
else
{
if (dbPass != _pswd)
{
//this.txtPassword.Text = "";
MessageBox.Show("Invalid password!");
this.txtPassword.Text = "";
}
else
{
this.Visible = false;
frmMain frmMain = new frmMain();
frmMain.ShowDialog(this);
//this.Visible = false;
}
}
Moreover, doing this akes the error even more illogical to me. If the login data I use is correct, everything works fine but when the password is wrong, instead of executing the instructons inside {} it jumps BACK ??:|???:| to this:
if (dbUser != _usr)
{
//this.txtUsername.Text = "";
MessageBox.Show("Invalid username!");
this.txtUsername.Text = "";
}
This made me think is some sort of VC# error so I tried running this on my laptop but nothing changed. I even asked some college friend to take a look but same old story.
Now, I'n not an advanced programmer(not even a programmer, since I;m still in college) but this isn't right.
Before reading the data from the database I used some predefined username & password and everything worked fine :|.
What am I missing???

New Topic/Question
Reply




MultiQuote




|