Solved

Object cannot be cast from DBNull to other types - using SQL 2000/C#

Posted on 2007-03-27
6
291 Views
Last Modified: 2008-01-09
Hi there

Wonder if you can help me, I keep getting the following error:

Object cannot be cast from DBNull to other types.

I have the following code:

dateSend_ADHOC          = myDataReader["DT_SEND"].ToString();

if (dateSend_ADHOC == "")  // I guess I get the error here?
{
  .
  .
  .
  .
}

dateSend_ADHOC could have a date or left blank i.e. in SQL left as null

How do I get this to work?

Many thanks
Mousemat24
0
Comment
Question by:mousemat24
[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
6 Comments
 
LVL 18

Expert Comment

by:DarrenD
ID: 18800181
Hi

not positive but try...

if (dateSend_ADHOC == System.DBNull.Value)  // I guess I get the error here?
{
  .
  .
  .
  .
}
0
 
LVL 18

Expert Comment

by:DarrenD
ID: 18800193
Or...

returnValue = Convert.IsDBNull(value)
0
 

Author Comment

by:mousemat24
ID: 18800216
When I do the following - if (dateSend_ADHOC == System.DBNull.Value)
I get the following error:


Error      1      Operator '==' cannot be applied to operands of type 'string' and 'System.DBNull'      
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 13

Expert Comment

by:KarinLoos
ID: 18800253
or jsut query length of resulting string, if its 0, then input field was empty or null
dateSend_ADHOC          = myDataReader["DT_SEND"].ToString();

if (dateSend_ADHOC.Length == 0)  // I guess I get the error here?
{
 ...
}
0
 
LVL 18

Accepted Solution

by:
DarrenD earned 250 total points
ID: 18800273
You should probably use the function

Convert.ISDBNull above to check if it is a Null and then perform whatever logic.

This returns a boolean which you can then test for

if (Convert.ISDBNull(dateSend_ADHOC)) {
}

Darren
0
 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 250 total points
ID: 18801822
you should work this way:

if (myDataReader["DT_SEND"] = DBNull.Value)  // I guess I get the error here?
{
  .
  .
  .
  .
}
else
{
  dateSend_ADHOC          = myDataReader["DT_SEND"].ToString();

}

or, if you don't need to distinguish between a NULL and an empty string, use the COALESCE() function in the query to replace the null directly by empty string.

SELECT COALESCE( yourfield, '' ) as yourfield FROM yourtable ...
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

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…
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…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

622 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