Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I correctly check DBNull in VB?

Why does the following code:

  A = not IsDBNull(CurRow("BuyBook")) AndAlso CType(CurRow("BuyBook"), string) = "Yes"

results in the following error:

 Conversion from type 'DBNull' to type 'String' is not valid.

When AndAlso is supposed to short-circuit according to this article:

http://support.microsoft.com/kb/817250

like image 271
merlin2011 Avatar asked Jan 13 '12 07:01

merlin2011


People also ask

How do you check for DBNull?

The only way to test for a DbNull value is to use IsDbNull.

How do you use DBNull value?

If a database field has missing data, you can use the DBNull. Value property to explicitly assign a DBNull object value to the field. However, most data providers do this automatically. To evaluate database fields to determine whether their values are DBNull, you can pass the field value to the DBNull.

How assign DBNull value to string in VB net?

There is no way to assign a string variable the value of DbNull, so you need to do the translation yourself manually.

What is DBNull value?

The DBNull class represents a nonexistent value. In a database, for example, a column in a row of a table might not contain any data whatsoever. That is, the column is considered to not exist at all instead of merely not having a value. A DBNull object represents the nonexistent column.


2 Answers

Have you tried comparing like this:

If CurRow("BuyBook") Is DBNull.Value Then
     '...
End If
like image 133
Pondidum Avatar answered Oct 19 '22 23:10

Pondidum


Instead of not IsDBNull(CurRow("BuyBook")), use NOT (CurRow("BuyBook")) is System.Dbnull.Value). Try this:

A = (NOT (CurRow("BuyBook")) is System.Dbnull.Value) AndAlso CType(CurRow("BuyBook"), string) = "Yes"

OR

A = not string.IsNullOrEmpty(CurRow("BuyBook")) AndAlso CType(CurRow("BuyBook"), string) = "Yes"
like image 26
Harsh Avatar answered Oct 20 '22 00:10

Harsh