Solved

DBNULL

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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

685 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