4
<%# Eval("Description") == DBNull.Value ? "empty" : "notempty"%>

is showing always 'notempty' even there is null in that field in DB (type of varchar(), null) ... Tried also checking for empty string:

<%# Eval("Description") == "" ? "empty" : "notempty"%>

and it always displays notempty... what's wrong here??

Stewie Griffin
  • 9,257
  • 22
  • 70
  • 97

3 Answers3

16

There is a difference between DBNull.Value and null. It is possible the field is returning null.

Try

<%# Eval("Description") == null ? "empty" : "notempty"%>

Also if the field value type is supposed to be string you could do something along the lines of..

<%# (Eval("Description") as string) ?? "empty" %>
Quintin Robinson
  • 81,193
  • 14
  • 123
  • 132
  • this worked, can you explain me shortly what's the difference ? Thanks !! – Stewie Griffin Mar 07 '11 at 19:36
  • 2
    @Stewie Griffin I think the most basic way to explain it is db values vs .net values. Basically `DBNull.Value` indicates missing database values, where as `null` indicates data that is `null`. – Quintin Robinson Mar 07 '11 at 19:41
  • @QuintinRobinson there is a difference between Null and DBNull. Remember that most SQL engines have at minimum a 3-state value system; where as .Net is based on a 2-state value system. Even the Nullable Generic is more of a wrapper than anything else. – GoldBishop May 31 '17 at 12:52
5

Have you tried using this method:

<%# Convert.IsDBNull(Eval("Description") ? "empty" : "notempty"%>
Abe Miessler
  • 82,532
  • 99
  • 305
  • 486
4

It is not actually storing DBNull at this level. You need to look for null or an empty string which string.IsNullOrEmpty should be enough and will capture both states of null and empty.

<%# string.IsNullOrEmpty(Eval("Description").ToString()) ? "empty" : "notempty"%>  
Kelsey
  • 47,246
  • 16
  • 124
  • 162