How to handle conversion from type "DBNull" to print "String", invalid

I need to learn a little about how to handle the following: - I have a misc_text_2 data field that is of type varchar (25) and is nullable. Now, if I use the following syntax

<asp:Label ID="lblPrinter" runat="server" Text='<%# iif(eval("misc_text_2") is dbnull.value, "", iif(eval("misc_text_2") like "NA", "None", iif(eval("misc_text_2") like "KP1", "Kitchen Printer 1", iif(eval("misc_text_2") like "KP2", "Kitchen Printer 2", iif(eval("misc_text_2") like "KP3", "Kitchen Printer 3", iif(eval("misc_text_2") like "BP1", "Bar Printer 1", iif(eval("misc_text_2") like "BP2", "Bar Printer 2", iif(eval("misc_text_2") like "BP3", "Bar Printer 3", Eval("misc_text_2")))))))))%>'></asp:Label> 

I keep getting the error. Exception Details: System.InvalidCastException: Invalid conversion from DBNull to type 'String'.

I know something is missing for me, but what ...

Thanks in advance

+7
dbnull
source share
8 answers

In your sql query, you can use isNull (misc_text_2, '') to return to an empty string instead of DBNull.

-one
source share

You must explicitly check DBNull.Value and do the conversion yourself.

In other words, create a method that will perform the conversion for you, taking into account DBNull.Value .

+13
source share

Not answering your question, but: You really have to create the code behind the method that performs the conversion. This will make the code more understandable and debugged and allow you to reuse the code.

+3
source share

Every time you use Eval, you have to put off a lazy evaluation somehow. To do this, replace each instance:

iif(eval("misc_text_2") like ...

from

iif(IsDbNull(eval("misc_text_2")) OrElse eval("misc_text_2") like ...

OrElse prevent any attempt to convert DbNull to boolean. However, from a more fundamental point of view, the strike is the most correct. All of this should be done using code, possibly in an ItemDataBound (or RowDataBound) event handler.

After further reflection ...

The OP can also use the Convert.ToString() method in its code, which converts DBNulls to String.Empty.

+2
source share

Since we have an outdated database configured for MS-Dynamics (Solomon), our method of processing zeros is to convert them to zero strings either in ASP or VB.NET code. those.

 Trim$(misc_text_2 & " ") 

Returns a problem for any version of VB.

0
source share

If you are using the dataset constructor, the easiest way to get rid of this error is to change the properties of the reputable column.

NullValue exception to "Empty" instead of "Throw exception."

Hope this helps you all.

0
source share

To convert DBNull or IsDBNull, you can try the following methods:

  • Convert DBNull to an empty string

    (DBNullObj) .ToString

  • Create a DBNull Converter Function

    Public function ConvertDBNullToString (DBNullObj As Object) as a string

    if IsDBNull (DBNullObj) then

    return ""

    end if

    return DBNullObj

    Final function

0
source share

As spiritUMTP suggested, if you use Designer Dataset Designer, change the DataColumn.NullValue from "Throw exception" to "empty" or "Nothing". I chose the latter and he fixed the problem. Now I just check for Nothing (If IsNothing (columnFax), then ...)

0
source share

All Articles