2

Hey guys, a table in my database has a field "Description" that Allow's Nulls'. This causes a problem when rendering the page in ASP.NET since I want to make sure I'm decoding the string as its coming out of the database as I'm encoding it on the way in. Naturally there are some NULL's in the Description field, which I would like to keep.

So my ASP.NET page has this code

<asp:TextBox ID="DescInfo" Text='<%# HttpUtility.HtmlDecode((string)Eval("Description")) %>' />

So when I render the page, I will get:

Unable to cast object of type 'System.DBNull' to type 'System.String'.

The way I look at it, I have two options:

  • bind during load

OR

  • make the table a non NULL table and use empty values instead

Anyone have a better idea perhaps?

Tomasz Iniewicz
  • 4,379
  • 6
  • 42
  • 47

4 Answers4

5

While DBNull cannot be cast to a string, it does have a ToString() method that returns an empty string. I might do it like this:

<%# HttpUtility.HtmlDecode(Eval("Description").ToString()) %>

That way you don't have to test for DBNull or anything. String.ToString() just returns the string, and DBNull.ToString() returns an empty string. This also has the advantage of only calling Eval one time per row instead of twice. The only thing that could trip you up is if your data contains both null and DBNull (you can't call ToString on null but you can on DBNull) but I don't think that's actually possible in this case.

Joel Mueller
  • 28,324
  • 9
  • 63
  • 88
1

You could replace (string)Eval("Description") with:

(Eval("Description") is System.DBNull) ? String.Empty : Eval("Description")

JeffSahol
  • 971
  • 8
  • 19
  • Hi, I actually tried this initially, and then again today as you pointed it out and could not get it to work. :( – Tomasz Iniewicz May 24 '11 at 23:39
  • I think this would work, but you need to cast the second `Eval` call to a string. Right now you've got one branch of the `?:` operator returning a string, and the other returning an object, and C# doesn't like that. – Joel Mueller May 24 '11 at 23:54
1

You need to first check if the value is a null. You can use IsDBNull() to check for nulls.

You could use

IIf(IsDBNull(DataRow("Test")), "Do if true", String.Empty)
Frazell Thomas
  • 6,031
  • 1
  • 20
  • 21
  • 1
    Looks like Tomaszewski is using C# (you can tell by the string casting) so a VB-only answer isn't going to help him much. But I refuse to downvote you because I don't want to spoil your 666 score. – Joel Mueller May 24 '11 at 23:26
  • 1
    The question wasn't flagged C# so I answered. Although the other answers are more direct (isDBNull is mearly a wrapper for System.DBNull) this can be useful for a VB dev when it comes up in a search :) – Frazell Thomas May 24 '11 at 23:28
  • I haven't looked at the source code, but I suspect that the VB IsDBNull is actually a wrapper for [Convert.IsDBNull](http://msdn.microsoft.com/en-us/library/system.convert.isdbnull.aspx). – Joel Mueller May 24 '11 at 23:35
  • @Joel check the MSDN link cited in my answer. It is System.DBNull. – Frazell Thomas May 24 '11 at 23:39
  • You're saying that the Information.IsDBNull() method is a wrapper for the System.DBNull class? That doesn't make any sense. Both versions of IsDBNull() check to see if an object is of the type System.DBNull. They either both do the type check directly, or one of them calls the other. – Joel Mueller May 24 '11 at 23:50
  • @Joel Information.isDBNull is a wrapper as it provides backward syntax support for VB6 while using the same underlying class as the rest of the .NET Framework (System.DBNull). It wraps functionality from System.DBNull... A Wraper class calls the underlying class. – Frazell Thomas May 25 '11 at 00:10
  • I understand that. What I'm saying is that a class can wrap another class, or a method can wrap another method, but a method can't wrap a class. It could wrap a method on a class, but System.DBNull contains no methods that determine if an object is of type System.DBNull, so there is nothing on that class to wrap for this particular functionality. – Joel Mueller May 25 '11 at 00:29
  • http://stackoverflow.com/questions/27237555/why-empty-cell-throws-an-error-during-sql-stored-procedure-execution – SearchForKnowledge Dec 01 '14 at 22:00
0

Something like this should work. I haven't tested it for you.

<%# HttpUtility.HtmlDecode(Eval("Description") == System.DBNull ? String.Empty : (string(Eval("Description")) %>
TheGeekYouNeed
  • 7,509
  • 2
  • 26
  • 43
  • `System.DBNull` is a type, and you can't compare a value to a type; it won't compile. You need to either do `foo == DBNull.Value` or `Convert.IsDBNull(foo)` – Joel Mueller May 24 '11 at 23:36
  • HI, i have actually initially tried this, but it wasn't working for me. I guess I should have put in answer. Furthermore, you can't check for equality as System.DBNull is a type. So you would have to use "is" as @JeffSahol pointed out above. – Tomasz Iniewicz May 24 '11 at 23:38
  • Yes, forgot the .Value on the DBNull. Thanks guys. – TheGeekYouNeed May 24 '11 at 23:42