-3
<%# Eval("FeeStatus") == DBNull.Value OR 0 ? "UnPaid" : "Paid" %>

I simply want to say IF FeeStatus is null or 0 than print Unpaid .. what is syntax? and what this condition is called i mean I am searching on net but don't know what to write ?

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
miyan chau
  • 15
  • 3
  • 1
    `Ternary Operator in C#` – Rahul Singh Jan 10 '15 at 17:31
  • Fairly certain you're doing comparison wrong. In an `or` statement you have to repeat the left-hand expression. This isn't C#'s `or` operator either, but I don't know if that might just be an ASP notation. – Jeroen Vannevel Jan 10 '15 at 17:33
  • @RahulSingh I really don't understand what you're trying to say with that comment, and I'd be surprised if the OP does get it. –  Jan 10 '15 at 17:34
  • Re-opening, because as far as I can tell, the question isn't what `?` and `:` mean in general, but how they can be used here. –  Jan 10 '15 at 17:37
  • @hvd - Obviously that `OR 0` statement is wrong, probably OP is not aware of `ternary operator` so commented that. – Rahul Singh Jan 10 '15 at 17:38
  • 1
    @JeroenVannevel its not ASP. Besides everything inside `<%# %>` needs to be valid C#. – Aron Jan 10 '15 at 17:38
  • @Aron: can you clarify what kind of code I'm looking at? What environment do these `<%# %>` tags indicate? – Jeroen Vannevel Jan 10 '15 at 17:41
  • 2
    @JeroenVannevel That's used in ASP.NET's ASPX pages for data binding, and the bits between `<%#` and `%>` must be a valid C# (or whatever language is used) expression. –  Jan 10 '15 at 17:41
  • Right okay. Too many ASP/ASP.NET/ASP.NET MVC/ variants to keep track off. Thanks for clarifying. – Jeroen Vannevel Jan 10 '15 at 17:43
  • @JeroenVannevel sorry. My bad. I meant to say, `OR` is not a part of the `ASP` nor `ASPX` view engine specification (since you said "but I don't know if that might just be an ASP notation"). In fact the server tags requires that the expression inside is a valid C# expression. – Aron Jan 10 '15 at 17:44

2 Answers2

0

This is probably kept most readable by creating a helper function:

public bool IsPaid(object feeStatus) {
  return feeStatus != DBNull.Value && !(bool)feeStatus;
}

Then you can write:

<%# !IsPaid(Eval("FeeStatus")) ? "UnPaid" : "Paid" %>

C# doesn't have any native x == (y or z) form that translates to x == y || x == z except only evaluating x once, you need either a helper function or a helper variable for that.

You could write it out in full, if you don't mind calling Eval twice:

<%# Eval("FeeStatus") == DBNull.Value || !(bool)Eval("FeeStatus")? "UnPaid" : "Paid" %>

but this is harder to understand.

But if your FeeStatus is known to be one of DBNull.Value, false, or true, instead of comparing to DBNull.Value and false, you could just compare to true:

<%# Eval("FeeStatus").Equals(true) ? "Paid" : "UnPaid" %>
  • Thanks hvd .. but I dont want to use function it can't be done using ternary operators ? .. – miyan chau Jan 10 '15 at 17:42
  • @miyanchau Already added that to my answer: it's harder to understand, and pointlessly repeats an operation, so I don't recommend it, but yes, you can. –  Jan 10 '15 at 17:44
  • `<%# new object[]{ DBNull.Value, 0 }.Contains(Eval("FeeStatus")) ? "UnPaid" : "Paid" %>` will work but its ugly as sin. – Aron Jan 10 '15 at 17:47
  • @Aron Ha, yeah, you can use BCL functions instead of creating your own. You might even avoid LINQ and use the old-fashioned `Array.IndexOf(new object[] { DBNull.Value, 0 }, Eval("FeeStatus")) >= 0 ? "Unpaid" : "Paid"` :) –  Jan 10 '15 at 17:53
  • @hvd OMG Thats such a great idea! I think you need to add XML and a `.ToString()` :D – Aron Jan 10 '15 at 17:55
  • @hvd OMG...Turns out this is what OP wants `<%# Eval("FeeStatus") as bool? ?? false ? "Paid" : "UnPaid" %>` – Aron Jan 10 '15 at 18:04
  • @Aron Thanks for pointing to the comment saying that the type is `bool`. I've used `Eval("...") as T?` conversions in the past when the result would be known to be either `DBNull.Value`, or an instance of `T`, but stopped using that when this silently did the wrong thing when `Eval("...")` would start returning `short` instead of `int` due to database changes: no exception would be thrown, but it would evaluate to `null`. I personally would continue comparing to `DBNull.Value`, and if unequal, cast to `bool`. If the database changes, this causes an exception. –  Jan 10 '15 at 18:07
  • Honestly I always felt that `Eval` was an insanely bad API. Heck `ASP.Net WebForms` is just terrible. – Aron Jan 10 '15 at 18:09
0

Close but no cigar.

<%# Eval("FeeStatus") == DBNull.Value || (int) Eval("FeeStatus") == 0 ? "UnPaid" : "Paid" %>

OR

<%# new object[]{ DBNull.Value, 0 }.Contains(Eval("FeeStatus")) ? "UnPaid" : "Paid" %>

There is no OR operator in C#. Not to mention its not clear how you expect the OR operator would work in boolean logic.

Oh man...okay turns out you want this abomination.

<%# Eval("FeeStatus") as bool? ?? false ? "Paid" : "UnPaid" %>
Aron
  • 15,464
  • 3
  • 31
  • 64
  • its not checking the condition simply printing the paid – miyan chau Jan 10 '15 at 17:52
  • @miyanchau Probably because its the wrong type of zero. Try variations of 0I, Od, 0m etc... Is it a decimal? – Aron Jan 10 '15 at 17:53
  • it is a bit boolean type – miyan chau Jan 10 '15 at 18:00
  • @miyanchau You are messing with me right? Anyways, try `new object[]{ DBNull.Value, false }`. Actually you want this...`<%# Eval("FeeStatus") as bool? ?? false ? "Paid" : "UnPaid" %>` – Aron Jan 10 '15 at 18:01
  • why should I mess with you im just completing the characters to meet the minimum req of characters to post comment let me try this one – miyan chau Jan 10 '15 at 18:08
  • @miyanchau because the OP has `Eval("FeeStatus") == DBNull.Value OR 0` in it...never mind >_< I guess you don't understand... – Aron Jan 10 '15 at 18:11
  • <%# Eval("FeeStatus") as bool? ?? false ? "Paid" : "UnPaid" %> this works thanks @Aron handling NULL too .. awesome but how ? ? I need to study more about it – miyan chau Jan 10 '15 at 18:11
  • @miyanchau check hvd's comment on changing SQL schema and this silently failing... – Aron Jan 10 '15 at 18:12