Solved

DBNULL

Posted on 2011-09-28
5
374 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

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…
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!
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

617 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