Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am creating a user login for my software which is connected to Access, I keep on getting the same error which highlights the line

'OleDbDataReader reader = command.ExecuteReader();' saying...

'An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll

Additional information: Syntax error (missing operator) in query expression 'Username= 'MGRjs' and Password'Candy')'.'


private void LoginButton_Click(object sender, EventArgs e)
{
        connection.Open();
        OleDbCommand command = new OleDbCommand();
        command.Connection = connection;
        command.CommandText = "select * from NewUser where Username= '" + IDtbx.Text + "' and Password'" + PSWtbx.Text + "')";

        OleDbDataReader reader = command.ExecuteReader();
        int count = 0;
        while (reader.Read())
        {
            count = count + 1;
        }

        if (count == 1)
        {

            MainSystem mainForm = new MainSystem();
            mainForm.FormClosed += new FormClosedEventHandler(Login_FormClosed);
            mainForm.Show();
            this.Hide();
            LoginError.Visible = false;
            connection.Close();
            connection.Dispose();
        }

        else
        {
            LoginError.Visible = true;
        }
        connection.Close();


Any suggestions?

What I have tried:

Reconnecting access database, attaching different database
Posted
Updated 9-Nov-17 4:18am

Two things, both important:
1) Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

2) Never store passwords in clear text - it is a major security risk. There is some information on how to do it here: Password Storage: How to do it.[^]

Fix those, and your error will be apparent: there would have been an "=" between "Password" and the quote around the user entered text.
 
Share this answer
 
Examine the sql help in your command

command.CommandText = "select * from NewUser where Username= '" + IDtbx.Text + "' and Password'" + PSWtbx.Text + "')";


Does it look like valid SQL? Is

SQL
and Password'Candy'


valid SQL?

Change it to

command.CommandText = "select * from NewUser where Username= '" + IDtbx.Text + "' and Password='" + PSWtbx.Text + "')";


However you really need to ditch this way of executing SQL and use parameterised queries instead.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900