Click here to Skip to main content
15,885,869 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
There was an error parsing the query. [ Token line number = 1,Token line offset = 77,Token in error = S ]

My Code

C#
SqlCeConnection conn = new SqlCeConnection("Data Source=E:\\Project Final_Year\\Project Final\\MyProject\\ERU Implementation\\VirtualPetedit\\VirtualPet\\MyDB.sdf");
           string query = "INSERT INTO Events (event_name,event_descrption,event_type,date) VALUES('" + textBox1.Text + "', '" + textBox2.Text + "','" + comboBox1.SelectedItem.ToString() + "','" + dateTimePicker2.Value + "');";

           //SqlCommand cmd = new SqlCommand();
           try
           {
               conn.Open();
              // SqlDataAdapter adapter = new SqlDataAdapter();
               SqlCeCommand command = new SqlCeCommand(query, conn);

               command.ExecuteNonQuery();
               conn.Close();

               MessageBox.Show("Event added Successfully..!!");
           }
           catch (Exception err)
           {
               MessageBox.Show(err.Message);
           }
Posted
Updated 30-Mar-15 0:15am
v2
Comments
Andy Lanng 30-Mar-15 6:00am    
This query is VERY open to SQL injection. That could be your issue here too.

Please post the query as it appears after being constructed so we can see the whole query as it is when run.
Herman<T>.Instance 30-Mar-15 6:15am    
What are the values you add to the query text?

1 solution

Please, don't do it like that!
Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.

The chances are that that will also cure your problem.
C#
string query = "INSERT INTO Events (event_name,event_descrption,event_type,date) VALUES(@NM, @ED, @ET, @DT);";
try
   {
   conn.Open();
   SqlCeCommand command = new SqlCeCommand(query, conn);
   command.Parameters.AddWithValue("@NM", textBox1.Text);
   command.Parameters.AddWithValue("@ED", textBox2.Text);
   command.Parameters.AddWithValue("@ET", comboBox1.SelectedItem);
   command.Parameters.AddWithValue("@DT", dateTimePicker2.Value);
   command.ExecuteNonQuery();
 
Share this answer
 
Comments
Andy Lanng 30-Mar-15 9:11am    
Agreed - Thanks for reconstructing his code - 5voted :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900