Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2007-03-27
6
Medium Priority
?
295 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 1000 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 1000 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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 video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

670 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