casit
asked on
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();
con.Open();
try
{
int count = dbcommand.ExecuteNonQuery();
}
catch (OdbcException ex)
{
MessageBox.Show(ex.Message);
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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?
ASKER
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.
ASKER