Solved

DBNULL

Posted on 2011-09-28
5
367 Views
Last Modified: 2012-05-12
Hi,

I keep getting the error message

Unable to cast object of type 'System.DBNull' to type 'System.String'.

when i do the following code.
if (e.Row.Cells[j] != null && ((string)e.Row.Cells[j].Data.DataItem).Contains("text"))

I think its to do with this part ((string)e.Row.Cells[j].Data.DataItem).Contains("text"))

Can someone please help.

Thanks
0
Comment
Question by:cjJosephj
5 Comments
 
LVL 23

Accepted Solution

by:
wdosanjos earned 125 total points
ID: 36719730
Please try the following:

if (e.Row.Cells[j] != null && e.Row.Cells[j].Data.DataItem is string && ((string)e.Row.Cells[j].Data.DataItem).Contains("text"))
0
 
LVL 4

Expert Comment

by:theHollow
ID: 36719768
Hello
e.Row.Cells[j] will never be "null".
But it might be "System.DBNull", which is a "database null value".

You can try the following:

if ((e.Row.Cells[j] != DBNull.Value) && e.Row.Cells[j].Data.DataItem.ToString().Contains("text"))
{
    // do stuff
}

Open in new window


Hope it helps :-)
0
 
LVL 6

Expert Comment

by:effes
ID: 36719875
Just to add my two cents: I think it should be either
if (!(e.Row.Cells[j].Value is DBNull) && ((string)e.Row.Cells[j].Data.DataItem).Contains("text"))

Open in new window

or
if (e.Row.Cells[j].Value != DBNull.Value && ((string)e.Row.Cells[j].Data.DataItem).Contains("text"))

Open in new window

.
0
 
LVL 4

Expert Comment

by:theHollow
ID: 36720073
Hi
To specify my answer further:
Because you use "&&" operand (which exuals the VB.NET "AndAlso"), the second check will be igonred if the first was "true".

Therefore, there is nothing wrong with the second check in the "if".
However...e.Row.Cells[j] is not "null". It is System.DBNull. So it crash on the second test.

So in your if-statement, the symptom is the second check. The real problem is the first check, which should not check with "null", but "DBNull.Value" :-)
0
 

Author Comment

by:cjJosephj
ID: 36813662
Thank you all for your quick replies


0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now