2

I am getting a syntax error on my insert statement but I cannot find the error.

Here is my method

String sql = "insert into receivers (receiver_id, receiver_name, salary, team,     forty_yard_dash, position, offense) " + 
                                "values" + 
                                "(@id, @name, @salary, @team, @forty, @position, @offense)";
        com = new OleDbCommand(sql, con);
        com.Parameters.AddWithValue("@id", rec.id);
        com.Parameters.AddWithValue("@name", rec.name);
        com.Parameters.AddWithValue("@salary", rec.salary);
        com.Parameters.AddWithValue("@team", rec.team);
        com.Parameters.AddWithValue("@forty", rec.fortyYardDash);
        com.Parameters.AddWithValue("@offense", rec.offenseDefense);
        con.Open();
        com.ExecuteNonQuery();
        con.Close();

My database looks like this:
receiver_id number
name text
salary number
team text
forty_yard_dash number
position text
offense Yes/No

Any help would be appreciated!

4
  • 5
    looks like you missed the parameter @position Commented Nov 17, 2013 at 15:39
  • 1
    For text type column i think you have to enclosed values with ' or " in the sql request Commented Nov 17, 2013 at 15:44
  • @user2196728 No, with a parameterized query you don't need to delimit any values in the SQL string. (In fact, it is a common mistake for people to put such delimiters in.) Commented Nov 17, 2013 at 15:50
  • Any time you ask a question about an error, you should always tell us what the error is. Commented Nov 17, 2013 at 16:08

2 Answers 2

1

King King has already pointed it out in the comments:

You need to add the parameter for position

String sql = "insert into receivers (receiver_id, receiver_name, salary, team, forty_yard_dash, position, offense) " + 
             "values" + 
             "(@id, @name, @salary, @team, @forty, @position, @offense)";
    com = new OleDbCommand(sql, con);
    com.Parameters.AddWithValue("@id", rec.id);
    com.Parameters.AddWithValue("@name", rec.name);
    com.Parameters.AddWithValue("@salary", rec.salary);
    com.Parameters.AddWithValue("@team", rec.team);
    com.Parameters.AddWithValue("@forty", rec.fortyYardDash);
    //new --> I am assuming the value for position comes from rec.position; change to correct source
    com.Parameters.AddWithValue("@position", rec.position); 
    com.Parameters.AddWithValue("@offense", rec.offenseDefense);
    con.Open();
    com.ExecuteNonQuery();
    con.Close();

This should do the trick, if there is no other error you have not told us about ;)

Sign up to request clarification or add additional context in comments.

Comments

1

In addition to the missing .Parameters.AddWithValue statement for position, the other issue is that position is a reserved word in Access SQL and must be enclosed in square brackets in the CommandText. I just tried the following and it worked for me:

using (var con = new OleDbConnection())
{
    con.ConnectionString = myConnectionString;
    con.Open();

    using (var com = new OleDbCommand())
    {
        int rec_id = 1;                         //
        string rec_name = "Bubba";              //
        int rec_salary = 2000000;               //
        string rec_team = "Springfield Atoms";  // test data
        int rec_fortyYardDash = 99;             //
        string rec_position = "linebacker";     //
        bool rec_offense = false;               //

        string sql =
                "INSERT INTO receivers (" +
                        "receiver_id, " +
                        "receiver_name, " +
                        "salary, " +
                        "team, " +
                        "forty_yard_dash, " +
                        "[position], " +
                        "offense " +
                    ") VALUES (?,?,?,?,?,?,?)";
        com.Connection = con;
        com.CommandText = sql;
        com.Parameters.AddWithValue("?", rec_id);
        com.Parameters.AddWithValue("?", rec_name);
        com.Parameters.AddWithValue("?", rec_salary);
        com.Parameters.AddWithValue("?", rec_team);
        com.Parameters.AddWithValue("?", rec_fortyYardDash);
        com.Parameters.AddWithValue("?", rec_position);
        com.Parameters.AddWithValue("?", rec_offense);
        com.ExecuteNonQuery();
    }
    con.Close();
}

1 Comment

That was it!!! Thank you both! When I added the field in the DB usually it will tell me if it is a reserved word or not...Oh well

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.