Solved

DBNULL

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
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.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

930 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

15 Experts available now in Live!

Get 1:1 Help Now