using parameters in Access insert query

I have the following code that isn't working right.  Its inserting the text @code instead of the value of codevalue.text.tostring()
OdbcConnection con = new OdbcConnection("DSN=ShipData");
            string sqlQuery = "INSERT INTO Address ([Code],[Company],[Name],[Phone],[Address],[Address2],[City],[State],[ZIP],[Country],[E-mail],[Fdx_accnt]) VALUES"
                            + " ('@code','@company','@contact','@phone','@addy','@addy2','@city','@state','@zip','@country','@email','@fdx_accnt')";
            OdbcCommand dbcommand = new OdbcCommand(sqlQuery, con);
            dbcommand.Parameters.Add("@code", OdbcType.VarChar,40).Value = codevalue.Text.ToString();
            dbcommand.Parameters.Add("@company", OdbcType.VarChar,40).Value = compvalue.Text.ToString();
            dbcommand.Parameters.Add("@contact", OdbcType.VarChar,40).Value = namevalue.Text.ToString();
            dbcommand.Parameters.Add("@phone", OdbcType.VarChar,40).Value = phonevalue.Text.ToString();
            dbcommand.Parameters.Add("@addy", OdbcType.VarChar,40).Value = addy1value.Text.ToString();
            dbcommand.Parameters.Add("@addy2", OdbcType.VarChar,40).Value = addy2value.Text.ToString();
            dbcommand.Parameters.Add("@city", OdbcType.VarChar,40).Value = cityvalue.Text.ToString();
            dbcommand.Parameters.Add("@state", OdbcType.VarChar,40).Value = statesdropdown.Text.ToString();
            dbcommand.Parameters.Add("@zip", OdbcType.VarChar,40).Value = zipvalue.Text.ToString();
            dbcommand.Parameters.Add("@country", OdbcType.VarChar,40).Value = countryvalue.Text.ToString();
            dbcommand.Parameters.Add("@email", OdbcType.VarChar,40).Value = emailvalue.Text.ToString();
            dbcommand.Parameters.Add("@fdx_accnt", OdbcType.VarChar,9).Value = fdx_accntvalue.Text.ToString();
                int count = dbcommand.ExecuteNonQuery();
            catch (OdbcException ex)

Open in new window

Who is Participating?
drichardsConnect With a Mentor Commented:
I don't think you have the parameter syntax correct in your query.  I believe you do not use quotes around the parameter names.
            string sqlQuery = "INSERT INTO Address ([Code],[Company],[Name],[Phone],[Address],[Address2],[City],[State],[ZIP],[Country],[E-mail],[Fdx_accnt]) VALUES"
         + " (@code,@company,@contact,@phone,@addy,@addy2,@city,@state,@zip,@country,@email,@fdx_accnt)";

Open in new window

casitAuthor Commented:
Now its telling me 12 parameters are expected.
So the syntax of the query is correct, and I don't see anything else blatantly incorrect.  I assume you validated that the command parameter collection has 12 items prior to ExecuteNonQuery?
casitAuthor Commented:
Yeah.  I even debugged it and then looked at the output of the paramters to make sure they had values and they did.  so I just put the variables right into the sql query.  Will have to figure it out later.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.