1

I'm diving head first into both C# and Access databases. This is all brand new to me, so I have a small test database set up to work with a small template. I'm trying to figure out why I keep getting a syntax error that is triggered by the ExecuteNonQuery() method. Any help and insight would be appreciated.

Edit: SOLVED: This is the working code for this situation. All help was greatly appreciated!

    public void addToDb()
    {
        String first = "John";
        String last = "Doe";
        String testPath = GVar.TEST_FILEPATH + GVar.TEST_DATABASE;
        String strCommand = "INSERT INTO ID ([First], [Last]) Values(@First, @Last)";
        OleDbConnection dbTest = null;
        OleDbCommand cmd = null;

        try
        {
            dbTest = new OleDbConnection(GVar.OLE_DB_WRITE + testPath);
            dbTest.Open();

            cmd = new OleDbCommand(strCommand, dbTest);
            cmd.Parameters.AddWithValue("@First", first);
            cmd.Parameters.AddWithValue("@Last", last);
            cmd.ExecuteNonQuery();

            Console.WriteLine("Data Added");
        }
        catch (Exception ex)
        {
            Console.WriteLine("Db Test: " + ex.Message);
        }
        dbTest.Close();
    }

1 Answer 1

1

From OleDbCommand.Parameters property

The OLE DB .NET Provider does not support named parameters for passing parameters to an SQL statement or a stored procedure called by an OleDbCommand when CommandType is set to Text. In this case, the question mark (?) placeholder must be used. For example:

SELECT * FROM Customers WHERE CustomerID = ?

Therefore, the order in which OleDbParameter objects are added to the OleDbParameterCollection must directly correspond to the position of the question mark placeholder for the parameter in the command text.

I don't see anything wrong in your INSERT statement other than this.

cmd.CommandText = "INSERT INTO Identity ([First],[Last]) VALUES(?, ?)";
cmd.Parameters.AddWithValue("@First", first);
cmd.Parameters.AddWithValue("@Last", last);
cmd.ExecuteNonQuery();
Sign up to request clarification or add additional context in comments.

9 Comments

This doesn't seem to make a difference. Is there something else I'm missing? cmd.CommandText = "INSERT INTO Identity ([First],[Last]) VALUES(?, ?)";
@johndoe86x Also you changed your parameters like new OleDbParameter(?, first) right? Actually, I didn't see any syntax error in your INSERT statement. What is your cmd looks like when you debug your code after you add parameter values?
I updated the OP to reflect my current code. I also removed the try/catch statements so the program would break at the appropriate spot. The unhandled exception still occurs at cmd.ExecuteNonQuery();
@johndoe86x Debug your code step by step and watch your cmd before your cmd.ExecuteNonQuery(); line. And your First and Last columns are character type, right?
Something I noticed was that rowArray didn't seem to pull the values properly by using new OleDbParameter("?", first). I changed this to: OleDbParameter[] rowArray = new OleDbParameter[2]; rowArray[0].Value = first; rowArray[1].Value = last; And now I'm getting an "Object reference not set to an instance of an object." Error on rowArray[0].Value = first;
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.