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
Solved

Using IsDBNull for this line

Posted on 2008-10-23
8
794 Views
Last Modified: 2008-11-02
I have this line of code but i get a null reference when i run the code. I need to ..for example, say if it's null..replace with a default value or leave blank..how can I do this?

 cmd.Parameters.Add("@ShipDate", SqlDbType.DateTime).Value = item.Shipment.CreatedOn;

I get a null for item.Shipment.Createon...
0
Comment
Question by:Camillia
8 Comments
 
LVL 17

Assisted Solution

by:Daniel Reynolds
Daniel Reynolds earned 100 total points
ID: 22789919
if (null == item.Shipment.CreatedOn)
{cmd.Parameters.Add("@ShipDate", SqlDbType.DateTime).Value = "defaultvalue";}
else
{cmd.Parameters.Add("@ShipDate", SqlDbType.DateTime).Value = item.Shipment.CreatedOn;}
0
 
LVL 7

Author Comment

by:Camillia
ID: 22790075
thanks, let me try
0
 
LVL 23

Accepted Solution

by:
adathelad earned 300 total points
ID: 22790098
Equivalent, in one line:

cmd.Parameters.Add("@ShipDate", SqlDbType.DateTime).Value = (item.Shipment.CreatedOn == null) ? "defaultvalue" :  item.Shipment.CreatedOn;

You might not find it as readable, but just to offer an alternative.
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 22

Assisted Solution

by:p_davis
p_davis earned 100 total points
ID: 22790271
you might have to use DBNull.Value
0
 
LVL 7

Author Comment

by:Camillia
ID: 22790279
that's like the IIF in VB.net, i think. Let me try.
0
 
LVL 7

Author Comment

by:Camillia
ID: 22790389
DBNull.Value for "defaultvalue"?
0
 
LVL 22

Expert Comment

by:p_davis
ID: 22790397
no for the null check
0
 
LVL 13

Expert Comment

by:sm394
ID: 22791015
with minor change depending if Nulls allowed in DB Field
cmd.Parameters.Add("@ShipDate", SqlDbType.DateTime).Value = (item.Shipment.CreatedOn == null)  ? (object)DBNull.Value : item.Shipment.CreatedOn;
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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…
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…
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

860 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