Solved

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

Posted on 2007-03-27
6
285 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
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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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 142

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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 Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

813 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now