Mr T
Mr T

Reputation: 524

Where is the mistake with "INSERT INTO STATEMENT"?

I have an Access database that contains a table named user. That table has two fields, "username" of shorttext type and "userpassword" of shorttext type as well. The usernameis the primary key!

I made a windows form application, Visual C#, and connected successfully with the database.

I run at first this sql statement in Access,

"INSERT INTO USER(USERNAME,USERPASSWORD) VALUES('Tom','1234')"

and correctly appends the record into the table.

Now if I try to execute the same sql query through the winform application I get an exception which talks about a syntax error in the "Insert into" statement!

Weird fact is that I have made an application before one week and it worked.

Here is the method that should append the record:

public void insertNewUser()
    {
        String sqlQuery = "INSERT INTO USER(USERNAME,USERPASSWORD) VALUES('malmo','1234')";
        OleDbCommand cmd = new OleDbCommand(sqlQuery, this.conn);
        int rowsAffected = 0;
        try
        {
            rowsAffected = cmd.ExecuteNonQuery();
            System.Windows.Forms.MessageBox.Show("User inserted in database successfully!", "Debug:AppDatabase:insertNewUser");
        }
        catch (System.InvalidOperationException ex)
        {
            System.Windows.Forms.MessageBox.Show("User did not inserted in database", "Debug:AppDatabase:insertNewUser");
            System.Windows.Forms.MessageBox.Show(ex.Message, "Debug:AppDatabase:insertNewUser");
        }
        catch (System.Data.OleDb.OleDbException ex)
        {
            System.Windows.Forms.MessageBox.Show(ex.Message, "Debug:AppDatabase:insertNewUser");
        }
        System.Windows.Forms.MessageBox.Show(rowsAffected.ToString(), "RowsAffected");
    }

I get the Exception in the second catch block!

Upvotes: 0

Views: 113

Answers (1)

Mr T
Mr T

Reputation: 524

USER is a keyword. use [USER] instead. – Reza Aghaei

Upvotes: 1

Related Questions