Testing for DBNull, causes Invalid Cast Exception

328 views
Skip to first unread message

JaffaB

unread,
Jan 31, 2009, 9:21:03 AM1/31/09
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Hi All,

i am trying to use a variable from a DST in a VS2003 project, and I am
trying to overcome an Invalid Cast Exception of DBnull. But
everything I try to test for DBnull, causes the exception as well.

At first, I used a function to check and convert as follows:

public static string MNS(object s)
{
if(s==System.DBNull.Value)
return string.Empty;
else
return Convert.ToString(s);
}

However, calling this (passing in the column of the table as shown
below) caused the same casting exception. Now I have gone specific
with the test as follows:

DstWastage.AuditRow drAudit = dstWastage.Audit[0];
if(drAudit.ContainerCode.Equals(System.DBNull.Value) == true) sqlains
= SqlCEParamAdd(sqlains,""); else sqlains = SqlCEParamAdd
(sqlains,drAudit.ContainerCode);

the 2nd line, produces the exception error of Invalid Cast Exception :
StrongTyping_CannotAccessDBNull.

I have also tried..

DstWastage.AuditRow drAudit = dstWastage.Audit[0];
if(drAudit.ContainerCode == System.DBNull.Value) sqlains =
SqlCEParamAdd(sqlains,""); else sqlains = SqlCEParamAdd
(sqlains,drAudit.ContainerCode);

However, this wont even compile, as it says it cannot compare the
string (drAudit.ContainerCode) with a type of System.DBNull.Value

Can anybody point me in the right direction for sorting out whether
the variable is a DBnull or not.

jay...@gmail.com

unread,
Feb 1, 2009, 2:37:35 PM2/1/09
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Use the .ToString() method of your database object, then use the
string.IsNullOrEmpty method to test for null

For example, if you are working with a Datarow you would do something
of the sort

string s = dstWastage.Audit[0].ToString();
if(string.IsNullOrEmpty(s))
{
......

Oh, and when working with strings in .net, use the .Equals method
instead of ==

Cerebrus

unread,
Feb 1, 2009, 3:56:51 PM2/1/09
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Under normal conditions, a simple test of Object.Equals
(System.DBNull.Value) should be sufficient. But in this case you are
using typed datasets within which each object is strongly typed. It is
my deduction that Objects such as integers might not be nullable and
therefore cannot be compared to DBNull, resulting in the InvalidCast.
It may be helpful to take a hint from Jayg28's suggestion and use
Convert.ToString() on the object first.

Another method may be :

---
// o is an Object returned by your Database
if (o != null && o != DBNull.Value)
{
// o can be accessed.
}
---

BBetances

unread,
Feb 2, 2009, 12:18:50 PM2/2/09
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
To check if a value is null with DBNull, you can also use
DBNull.Value.Equals(o)

that should handle the casting for you.
Reply all
Reply to author
Forward
0 new messages