Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
3.40/5 (2 votes)
See more:
Hi guys I have this line of code in c#
I keep getting this error=>
Syntax error: Missing operand before '&' operator.
Find below is my code thanks for any advice

C#
< ds.Tables[0].Columns.Add("RESULTS").Expression = "Iif(((ActualWeight >= (.96 * TargetWeight)) And (ActualWeight <= (1.04 * TargetWeight))),'GOOD',And (ActualWeight < (0.96 * TargetWeight))),'BAD', And (ActualWeight > (1.04 * TargetWeight))) 'BAD',null)";/pre>
Posted
Updated 30-Mar-15 4:18am
v5
Comments
Maciej Los 30-Mar-15 9:58am    
Check the number of opening brackets ;)
="Iif((condition1) AND (condition2), resultiftrue, resultiffalse)";
Richard MacCutchan 30-Mar-15 10:20am    
Your statement has too many commas and AND clauses; see Maciej's comment above.
CHill60 30-Mar-15 10:21am    
There is at least a comma missing before that final 'BAD' but as Maciej has pointed out - the brackets don't match either

I think the dataset formula uses VBA so you should be using AND rather than &&.

Try

Iif((ActualWeight >= (.96 * TargetWeight)) And (ActualWeight <= (1.04 * TargetWeight)),'GOOD','BAD')
 
Share this answer
 
v2
Comments
Ekona_Pikin 30-Mar-15 10:51am    
Thanks for all your help anyone who posted or try solve this problem I have tested both solution 2 and 3 and they are all on point :)) So for future reference The authors of both solutions 2 and 3 are RIGHT.
Mark Farmiloe 30-Mar-15 11:31am    
I'm feeling unappreciated. :-(
My first answer was the correct answer to your first question and my extended answer removed all the extra unneeded code you added when you edited your first question.
Could you at least accept all three solutions worked for you?
Ekona_Pikin 30-Mar-15 11:38am    
Hello Mark sorry I did not test ,but just did now and is on point too sorry sir :))
Mark Farmiloe 30-Mar-15 12:29pm    
:-)
Looks like you want nested Iif statements. Try:
Iif(((ActualWeight >= (.96 * TargetWeight)) And (ActualWeight <= (1.04 * TargetWeight))), 'GOOD', Iif(ActualWeight < (0.96 * TargetWeight), 'BAD', Iif(ActualWeight > (1.04 * TargetWeight), 'BAD', null)))


Putting the expression on multiple lines might make things clearer:
Iif(
    ((ActualWeight >= (.96 * TargetWeight)) And (ActualWeight <= (1.04 * TargetWeight))), 
    'GOOD', 
    Iif(
        ActualWeight < (0.96 * TargetWeight), 
        'BAD', 
        Iif(
            ActualWeight > (1.04 * TargetWeight), 
            'BAD', 
            null
        )
    )
)

EDIT: As Maciej Los pointed out, you can simplify the formula so that you don't need nested Iifs. See solution #3 for details.
 
Share this answer
 
v2
Comments
Maciej Los 30-Mar-15 10:30am    
Richard, have a look at this formula. There's no need to define nested iif's. Please, see my answer ;)
Richard Deeming 30-Mar-15 10:32am    
Well, if you're going to re-write the formula... :D
Ekona_Pikin 30-Mar-15 10:42am    
Thanks Richy:))
Please, see my comment to the question.
I guess you have to define formula ths way:
VB
="Iif((ActualWeight > .96 * TargetWeight) And (ActualWeight <= 1.04 * TargetWeight),'Good','Bad');


There is no need to define nested iif's. If ActualWeight is Bigger Then ... and LessOrEqual then ... - then condition is true, else false.
 
Share this answer
 
v2
Comments
Richard Deeming 30-Mar-15 10:34am    
You've missed part of the formula at the start:
Iif(ActualWeight >= (.96 * ...
Maciej Los 30-Mar-15 10:35am    
Corrected ;)
Thank you for pointing that ;)
Ekona_Pikin 30-Mar-15 10:35am    
Maciej, what about the other conditions?
Maciej Los 30-Mar-15 10:36am    
See updated answer. Not needed ;)
Ekona_Pikin 30-Mar-15 10:42am    
Thanks Maciej:))

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