Evening,
Sorry twice in one night. I have a form that allows new students to add their details and create themselves a password, this is then added to my access database. I added a new student then went to use the username and password credentials to sign into the returning student page. However, a few weeks ago when I wrote the returning students page I wrote an IF ElSE statement, that worked at the time, but now I ask it to sign in with any credentials and the message box that I assigned to the else statement comes up. But (weird part) if I click the okay button to dismiss the message box repeatedly it re-appears, until I click it (about five times) it disappears completely and allows me to sign in.
Now, I know this is odd and I would post code but I wouldn't know where to start with it. But is there any reason this would happen? It worked fine before I added the new credentials in but I had no need to change the IF ELSE statement and no error messages appear (aside from the one I coded myself into) :D.
Any help or advice would be great, I cant understand why it's doing it.
K
Loading
Riddhi ValechaPosted Apr 16, 2013, 12:46 AM
--------------------------------------------
public void btnReturningOkay_Click(object sender, EventArgs e)
{
bool result = false;
string connectionString =
"provider=Microsoft.ACE.Oledb.12.0;" +
"data source=C:\\Users\\Botterill\\Documents\\StudentRegistrationDatabase.accdb";
OleDbConnection myOleDbConnection = new OleDbConnection(connectionString);
string qry = "SELECT * FROM StudentDetails WHERE ID = "+int.Parse(txtUsername.Text)+" AND Password = '"+txtPassword.Text+"'";
connection.open();
OleDbCommand myOleDbCommand = new OledbCommand(connection, qry);
OleDbDataReader myOleDbDataReader = myOleDbCommand.ExecuteReader();
while (myOleDbDataReader.Read())
{
if (txtUsername.Text == myOleDbDataReader["columnName"].ToString() && txtPassword.Text == myOleDbDataReader["columnName"].ToString())
{
result = true;
//Dispose();
MessageBox.Show("Welcome");
new NewStudyPlan().ShowDialog();
}
}
myOleDbDataReader.close();
if(result == true) // login success
if(result == false) // login failed.
VulpesPosted Apr 15, 2013, 9:45 AM
myOleDbDataReader.Read();
This line needs to be removed because you're already calling the Read() method in the while loop.
Katie BPosted Apr 15, 2013, 6:02 AM
Slight change, I opened it to work on this morning and suddenly even the IF statement wont work and I cant understand why. The only thing I changed was the page it opens after the message box displays but heres the code:
public void btnReturningOkay_Click(object sender, EventArgs e)
{
string connectionString =
"provider=Microsoft.ACE.Oledb.12.0;" +
"data source=C:\\Users\\Botterill\\Documents\\StudentRegistrationDatabase.accdb";
OleDbConnection myOleDbConnection = new OleDbConnection(connectionString);
OleDbCommand myOleDbCommand = myOleDbConnection.CreateCommand();
myOleDbConnection.Open();
myOleDbCommand.CommandText =
//"SELECT * FROM StudentDetails WHERE ID = "+int.Parse(txtUsername.Text)+" AND Password = '"+txtPassword.Text+"'";
"SELECT ID, Password FROM StudentDetails";
OleDbDataReader myOleDbDataReader = myOleDbCommand.ExecuteReader();
myOleDbDataReader.Read();
//myOleDbDataReader.HasRows;
while (myOleDbDataReader.Read())
{
if (txtUsername.Text == myOleDbDataReader.GetInt32(0).ToString() && txtPassword.Text == myOleDbDataReader.GetString(1))
{
//Dispose();
MessageBox.Show("Welcome");
new NewStudyPlan().ShowDialog();
}
else
{
MessageBox.Show("Please enter a valid user name or password");
}
}
Ive put break points in, but now it doesnt seem to be reading the IF statement at all and I havent changed that. Any help really is appreciated. :D
Update: I just commented out the else statement and Im not quite sure what else I did but it works with all the record in my database...apart from the first one and im not sure why.
Morcos AdelPosted Apr 15, 2013, 5:53 AM
Riddhi ValechaPosted Apr 15, 2013, 4:45 AM
Can you share the code??
If yes, do provide the source code(in zip file).
I will debug and test it.
Is this windows application / web application ?/
Database query-Is it OK if you use SQL Server? or is it necessary that you need to use Access??
I am asking this because, no one today uses access.
Oracle and SQL Server are widely used.
Send me the project... I will correct it.