0

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);
        }
}

2 Answers 2

3

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;

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

2 Comments

I was using Add initially but then it kept underlining my code in green and said it was obsolete or something and to use AddWithValue and it went away.
@Alex Only Add(String, Object) overload is obsolete. You can use Add(String, SqlDbType, Int32) overload instead.
0

Try this

query="INSERT INTO [House] (id, city, state, country, beds, baths, price) VALUES ('@id', '@city', '@state', '@country', '@beds', '@baths', '@price')";

and replace your try with this

 connection.Open();
            SqlCeCommand commandInsert = new SqlCeCommand(query, 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();

Comments

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.