System.Data.SqlServerCeException (0x80004005): There was an error parsing to the query. [Token line number = 1, Token line offset = 120, Token in error = @price ]
I keep getting this error when running my program and I can't figure out what I'm doing wrong. I've tried changing multiple things around and it keeps giving the same error, just the number after "Token line offset = " changes sometimes.
static public void Insert(string _id, string _city, string _state, string _country, int _beds, int _baths, int _price)
{
try
{
connection.Open();
SqlCeCommand commandInsert = new SqlCeCommand("INSERT INTO [House] (id, city, state, country, beds, baths, price) VALUES (@id, @city, @state, @country, @beds, @baths, @price", connection);
commandInsert.Parameters.AddWithValue("@id", _id);
commandInsert.Parameters.AddWithValue("@city", _city);
commandInsert.Parameters.AddWithValue("@state", _state);
commandInsert.Parameters.AddWithValue("@country", _country);
commandInsert.Parameters.AddWithValue("@beds", _beds);
commandInsert.Parameters.AddWithValue("@baths", _baths);
commandInsert.Parameters.AddWithValue("@price", _price);
commandInsert.ExecuteNonQuery();
}
catch (SqlCeException exception)
{
MessageBox.Show(exception.ToString());
}
finally
{
connection.Close();
}
}
Code within buttonclick
private void btn_insert_Click(object sender, EventArgs e)
{
if (txt_id.Text != "" && txt_city.Text != "" && txt_state.Text != "" && txt_country.Text != "")
{
SQLFunctions.Insert(txt_id.Text, txt_city.Text, txt_state.Text, txt_country.Text, int.Parse(txt_beds.Text), int.Parse(txt_baths.Text), int.Parse(txt_Price.Text));
SQLFunctions.Refresh(this.dataGridView1);
}
}
I think you forget to close your VALUES(
part with )
after your @price
parameter.
VALUES (@id, @city, @state, @country, @beds, @baths, @price)
A few things more;
using
statement to dispose your connection and command automatically instead of calling close method manually.AddWithValue
method as much as possible. It may generate unexpected and surprising results sometimes. Use Add
method overload to specif your parameter type and it's size.