Solved

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

Posted on 2007-03-27
6
283 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
Comment Utility
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
Comment Utility
Or...

returnValue = Convert.IsDBNull(value)
0
 

Author Comment

by:mousemat24
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 13

Expert Comment

by:KarinLoos
Comment Utility
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
Comment Utility
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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Allow users to select all checkboxes 6 59
Linq help 1 16
C# remove trailing commas from csv file 10 44
Duplicate a row 2 29
Article by: Ivo
Anonymous Types in C# by Ivo Stoykov Anonymous Types are useful when  we do not need to follow usual work-flow -- creating object of some type, assign some read-only values and then doing something with them. Instead we can encapsulate this read…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

772 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

10 Experts available now in Live!

Get 1:1 Help Now