tags:

views:

3118

answers:

4

Hi Guys,

I need some expect advice on how to handle the following:- I have a data field misc_text_2 that is of type varchar(25) and allows NULL. 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 on getting an error Exception Details: System.InvalidCastException: Conversion from type 'DBNull' to type 'String' is not valid.

I know I'm missing something, but what...

Thanks in advance

A: 

You could in your sql query use isNull(misc_text_2, '') to return en empty string instead of DBNull.

Sani Huttunen
Never return empty strings in the place of nulls, this reduces the portability of your dataset and is misrepresentative of the data model. If it's a null it's a null. Let the UI layer worry about how to display it.
AdamRalph
That is your opinion.Returning an empty string solves his problem, right?
Sani Huttunen
+6  A: 

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

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

Lasse V. Karlsen
+1  A: 

Not answering your question, but: You should really create a code behind method that does the conversion. That will make the code easier to understand and debug, and will make it possible to reuse the code.

bang
A: 

Since we have a legacy database that was set up for MS-Dynamics (Solomon), our method of handling nulls is to convert them to null strings either in the ASP or VB.NET code. i.e.

Trim$(misc_text_2 & " ")

Gets rid of the problem for any version of VB.

Dave