How do I handle Conversion from type 'DBNull' to type 'String' is not valid
Asked Answered
H

8

8

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

Hereof answered 11/2, 2009 at 10:16 Comment(1)
What language/environment is that? I'd guess ASP. Could you plese mention that and tag the question accordingly?Flavorful
D
-1

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

Dambro answered 11/2, 2009 at 10:21 Comment(3)
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.Prussiate
That is your opinion. Returning an empty string solves his problem, right?Dambro
"Empty is different than null". And this is not an opinion, is a fact.Withershins
E
13

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.

Erotica answered 11/2, 2009 at 10:23 Comment(0)
R
3

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.

Readymade answered 11/2, 2009 at 10:30 Comment(0)
C
3

Each time you use the Eval, you have to shim-in lazy evaluation, somehow. To do so, replace each instance of:

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

with

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

The OrElse will prevent any attempted conversion of a DbNull to boolean. However, from a more fundamental perspective, bang is most-correct. This should all be done code-behind, probably in the ItemDataBound (or RowDataBound) event handler.

Upon further reflection...

The O.P. might also make use of the Convert.ToString() method in his code, which will convert DBNulls to String.Empty.

Cusack answered 11/10, 2011 at 19:23 Comment(1)
Convert.ToString() saved me!Reamonn
P
0

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.

Penetrate answered 22/6, 2010 at 21:20 Comment(2)
You don't need the Trim if you use "" instead of " ".Thoron
Still use trim because Dynamics uses only fixed size fields which are blank filled.Penetrate
S
0

If you are using data set designer, the easiest way to get rid of this error is to change the properties of respected column.

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

hope this will help you all.

Seamount answered 22/10, 2012 at 5:26 Comment(0)
C
0

For convert DBNull or IsDBNull you can try this ways:

  1. Convert DBNull to an empty string

    (DBNullObj).ToString

  2. Create a DBNull Converter Function

    Public Function ConvertDBNullToString(DBNullObj As Object) as string

    if IsDBNull(DBNullObj) then

    return ""

    end if

    return DBNullObj

    End Function

Chur answered 5/3, 2014 at 3:15 Comment(0)
G
0

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

Glorygloryofthesnow answered 1/3, 2016 at 17:4 Comment(0)
D
-1

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

Dambro answered 11/2, 2009 at 10:21 Comment(3)
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.Prussiate
That is your opinion. Returning an empty string solves his problem, right?Dambro
"Empty is different than null". And this is not an opinion, is a fact.Withershins

© 2022 - 2024 — McMap. All rights reserved.