0

I have this line of code in c# I keep getting this error=> Syntax error: Missing operand before '&' operator.

Find below is my code

ds.Tables[0].Columns.Add("RESULTS").Expression = "Iif(((ActualWeight >= (.96 * TargetWeight)) && (ActualWeight <= (1.04 * TargetWeight))),[GOOD] )";
Cœur
  • 37,241
  • 25
  • 195
  • 267
Sheldon
  • 39
  • 2
  • 8
  • are you sure its with that line of code? and post more code. What is Expression? is it a string, Expression? – Alex Anderson Mar 30 '15 at 13:35
  • possible duplicate of [iif equivalent in c#](http://stackoverflow.com/questions/822810/iif-equivalent-in-c-sharp) – Sayse Mar 30 '15 at 13:37
  • Yes @Alex ,I had used without knowing it was not valid in c# , what can do to tweak it in c# mode – Sheldon Mar 30 '15 at 13:37

2 Answers2

0

You are missing the false part of the expression as well as Iif is not syntactically correct. so try something like this

ds.Tables[0].Columns.Add("RESULTS").Expression = "IIF(((ActualWeight >= (.96 * TargetWeight)) And (ActualWeight <= (1.04 * TargetWeight))),[GOOD], [BAD])"
Sachin
  • 40,216
  • 7
  • 90
  • 102
0

DataTable Expressions are more like VB than C#. The && is not supported by And is:

ds.Tables[0]
  .Columns
  .Add("RESULTS")
  .Expression = "Iif(((ActualWeight >= (.96 * TargetWeight)) And (ActualWeight <= (1.04 * TargetWeight))),[GOOD],null)"; 

Note that I also added a false condition to complete the expression.

D Stanley
  • 149,601
  • 11
  • 178
  • 240