?
Solved

Using IsDBNull for this line

Posted on 2008-10-23
8
Medium Priority
?
798 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
[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
8 Comments
 
LVL 17

Assisted Solution

by:Daniel Reynolds
Daniel Reynolds earned 400 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 1200 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 22

Assisted Solution

by:p_davis
p_davis earned 400 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses
Course of the Month9 days, 17 hours left to enroll

762 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