0

I am working with SqlServerCe. The insert query is working properly but the update query gives an exception.

Here is my code:

SqlCeConnection _connection _connection = new SqlCeConnection(@"Data Source=MyDatabase#1.sdf;Password=xxxxx;");
_connection.Open();

cmd.Connection = _connection;
cmd.CommandText = " UPDATE [Solve_Student_question] 
                       SET Answ= '" + ans + "' ,
                           Start_time='" + sTime + "',
                           End_time='" + eTime + "' 
                     WHERE Qno='" + Qno + "' AND 
                           User_id='" + userid + "' AND
                           Exame_id='" + examid + "' sectionname='" + sectionname + "'";
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();

_connection.Close();

It gives an error at the line cmd.ExecuteNonQuery();

An exception of type 'System.Data.SqlServerCe.SqlCeException' occurred in 
System.Data.SqlServerCe.dll but was not handled in user code
2
  • You should use prepared statement to avoid all the mixed variable Commented Mar 19, 2014 at 11:21
  • post your exception too Commented Mar 19, 2014 at 11:23

2 Answers 2

3

You are missing an AND before sectionname. Corrected SQL below:

cmd.CommandText = " UPDATE [Solve_Student_question] 
                   SET Answ= '" + ans + "' ,
                       Start_time='" + sTime + "',
                       End_time='" + eTime + "' 
                 WHERE Qno='" + Qno + "' AND 
                       User_id='" + userid + "' AND
                       Exame_id='" + examid + "' AND sectionname='" + sectionname + "'";
Sign up to request clarification or add additional context in comments.

Comments

0
cmd.CommandText = " Update [Solve_Student_question] set Answ= '" + ans + "' ,Start_time='" + sTime + "',End_time='" + eTime + "' where Qno='" + Qno + "' AND User_id='" + userid + "' and Exame_id='" + examid + "'and sectionname='" + sectionname + "'";

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.