S
sea#
I'm writing an app to keep results of survey in DB. Using MS Access.
This method is writing answers to DB. Each survey has one question.
Each time an answer arrive to method, I check to which survey it
belongs to. Then I update the relevant corresponding column in DB. The
error I get is:
"Syntax error in UPDATE statement"
Marked bellow the line I discover it by debugger.
public void WriteAnswers(string username, byte SrNo, byte ans)
{
try
{
string sqlwrite;
switch (SrNo)
{
case 1:
sqlwrite = "UPDATE ResultsTbl" +
"WHERE ([UserName]='" + username + "' AND survey1 = " + 0 + ")"+
"SET survey1= " + ans + " " ;
break;
case 2:
sqlwrite = "UPDATE ResultsTbl" +
"SET survey2= " + ans + " " +
"WHERE (UserName='" + username + "' AND survey2 = " + 0 + ")";
break;
case 3:
sqlwrite = "UPDATE ResultsTbl" +
"SET survey3= " + ans + " " +
"WHERE (UserName='" + username + "' AND survey3 = " + 0 + ")";
break;
default:
sqlwrite="No answer was chosen";
break;
}
OleDbCommand cmdWrite = new OleDbCommand();
cmdWrite.Connection = cnSurvey;
cmdWrite.CommandType = CommandType.Text;
cmdWrite.CommandText = sqlwrite;
cnSurvey.Open();
cmdWrite.ExecuteNonQuery(); //error happens here
cnSurvey.Close();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Please help me find the cause of the error.
TIA,
sea#
This method is writing answers to DB. Each survey has one question.
Each time an answer arrive to method, I check to which survey it
belongs to. Then I update the relevant corresponding column in DB. The
error I get is:
"Syntax error in UPDATE statement"
Marked bellow the line I discover it by debugger.
public void WriteAnswers(string username, byte SrNo, byte ans)
{
try
{
string sqlwrite;
switch (SrNo)
{
case 1:
sqlwrite = "UPDATE ResultsTbl" +
"WHERE ([UserName]='" + username + "' AND survey1 = " + 0 + ")"+
"SET survey1= " + ans + " " ;
break;
case 2:
sqlwrite = "UPDATE ResultsTbl" +
"SET survey2= " + ans + " " +
"WHERE (UserName='" + username + "' AND survey2 = " + 0 + ")";
break;
case 3:
sqlwrite = "UPDATE ResultsTbl" +
"SET survey3= " + ans + " " +
"WHERE (UserName='" + username + "' AND survey3 = " + 0 + ")";
break;
default:
sqlwrite="No answer was chosen";
break;
}
OleDbCommand cmdWrite = new OleDbCommand();
cmdWrite.Connection = cnSurvey;
cmdWrite.CommandType = CommandType.Text;
cmdWrite.CommandText = sqlwrite;
cnSurvey.Open();
cmdWrite.ExecuteNonQuery(); //error happens here
cnSurvey.Close();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Please help me find the cause of the error.
TIA,
sea#