0

I want to update a field based on a condition
if age <25 then class = 10 else class = 20;
so far I have tried these

 cmd = new OleDbCommand("update workClass Set Class = 10  when " + ds.Tables["no"].Rows[i][1] + " < 25   else Set Class =  20 ", con);
 cmd.ExecuteNonQuery();

second

cmd = new OleDbCommand("update workClass Set Class = 10  where" + ds.Tables["no"].Rows[i][1] + " < 25   else Set Class =  20 ", con);
     cmd.ExecuteNonQuery();

I get this error message
note the ds.tables return 33in the first iteration so that bit works fine

Syntax error (missing operator) in query expression '10 when 33 < 25 else Class = 20'.

4
  • What does ds.Tables["no"].Rows[i][1] return ? Commented May 23, 2018 at 12:54
  • it returns an int 33 @Igor Commented May 23, 2018 at 12:55
  • Sorry, this does not make sense. Why are you essentially hard coding 33 and comparing it to 25? Is this actually coming from each record and are you doing this is some sort of loop? Commented May 23, 2018 at 12:57
  • Maybe you are trying to do this across the board for all records and for each record compare the records value in column X to 25? It would help if you included the code around these 2 lines. Commented May 23, 2018 at 13:00

2 Answers 2

1

There is no WHEN clause in the JET engine that powers the Microsoft Access Database.
You need to use the IIF function like this

cmd = new OleDbCommand(@"update workClass Set Class = IIF(" + 
    ds.Tables["no"].Rows[i][1] + " < 25, 10, 20)", con);

However I suggest you to use a parameterized query to avoid possible parsing error and sql Injection

cmd = new OleDbCommand(@"update workClass Set Class = IF(@value < 25, 10, 20)", con);
cmd.Parameters.Add("@value", OleDbType.Integer).Value = 
    Convert.ToInt32(ds.Tables["no"].Rows[i][1]);
Sign up to request clarification or add additional context in comments.

3 Comments

I have a feeling the OP is using this in a loop and reading the value in a data table and then applying it to an update. I think there is much more wrong with the code but that code is not included in the question....
@Igor perhaps you are correct but, as it stands, his problem (the syntax error) is caused by an unknown (to access) keyword WHEN. Probably we could optimize the code if we know more.
yes I did an awful mistake by using a loop I was working with insert earlier and that confused me.. never the less the problem wasn't that.. @Igor
1

I have a feeling this is what you are after.

cmd = new OleDbCommand("UPDATE workClass SET [Class] = IIF([Age] < 25, 10, 20)", con);
cmd.ExecuteNonQuery();

What is missing though is a descriminator so you only update a specific record or set of records based on a filter. As written the query you have and this one updates all records each time it is executed. To add the where clause be sure to add the filter using a parameter and do not use string concatenation.

Example:

cmd = new OleDbCommand("UPDATE workClass SET [Class] = IIF([Age] < 25, 10, 20) WHERE [someId] = ?", con);
cmd.Parameters.Add("@someId", OleDbType.Integer).Value = yourIdValue;
cmd.ExecuteNonQuery();

It is important to note that parameters are ordered in Ms Access and not named. The first parameter encountered in the query must correspond to the first parameter in the parameter collection.

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.