Solved

Using IsDBNull for this line

Posted on 2008-10-23
8
796 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 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

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

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…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…

705 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