Solved

Using IsDBNull for this line

Posted on 2008-10-23
8
793 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

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

Title # Comments Views Activity
Video Player 2017 5 20
VB.NET (2008) - Refactoring Question 2 19
Cant save 3D 4 19
Latest thoughts on C# utilizing Mono on Linux vs .NET on Windows? 5 20
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

785 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