Solved

Using IsDBNull for this line

Posted on 2008-10-23
8
791 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:xDJR1875
xDJR1875 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
 
LVL 22

Assisted Solution

by:p_davis
p_davis earned 100 total points
ID: 22790271
you might have to use DBNull.Value
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

708 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

15 Experts available now in Live!

Get 1:1 Help Now