Solved

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

Posted on 2007-03-27
6
287 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

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!
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

710 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