Multiple IF Problem

Problems using multiple IF in a login form.

Page 1 of 1

2 Replies - 510 Views - Last Post: 07 June 2009 - 08:21 PM Rate Topic: -----

#1 daydr3am3r  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 101
  • Joined: 10-April 09

Multiple IF Problem

Posted 07 June 2009 - 05:57 PM

Hi all!

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???

Is This A Good Question/Topic? 0
  • +

Replies To: Multiple IF Problem

#2 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 945
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Re: Multiple IF Problem

Posted 07 June 2009 - 07:25 PM

No, there is nothing wrong with C#. There is an error in your programming logic. When you do your select statement on the database, you are querying for a specific user name and password at the same time. If both the user name and password are not in the same record you will not get any data back and the first if will always fail. For example, suppose these are the records are in your database:

Username		   Password
SixOfEleven		BorgRule
daydreamer		YourPassword



If in your select statement, you pass:

select usrlg_ID, username, password from login where username = 'daydreamer' AND password = 'BorgRule';



You will not get anything back because daydreamer and BorgRule are not in the same record. Do you follow?
Was This Post Helpful? 0
  • +
  • -

#3 daydr3am3r  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 101
  • Joined: 10-April 09

Re: Multiple IF Problem

Posted 07 June 2009 - 08:21 PM

Yeah. I think I get it :).
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1