Solved

DBNULL

Posted on 2011-09-28
5
369 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

813 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

10 Experts available now in Live!

Get 1:1 Help Now