Saturday, April 20, 2013

How to insert null value into DateTime column?

How to insert null value into DateTime column?

Please have a look at the below code...

Generally for Parameters you will write the commented code below....to insert the value in the respective parameters.

But If there is Date and having null value to pass you have to do bit different otherwise it will give error
of  "Parameter missing..."

to overcome such error for the perticular scenario you have to write down the below written code.



cmdnon.Parameters.Add("@orderedDate", SqlDbType.SmallDateTime);
//cmdnon.Parameters["@orderedDate"].Value = this.orderedDate;
cmdnon.Parameters["@orderedDate"].IsNullable = true;

if (this.orderedDate == null)
      cmdnon.Parameters["@orderedDate"].Value = getNullDate;
else
      cmdnon.Parameters["@orderedDate"].Value = this.orderedDate;


this will remove your Error and successfully insert your expected null value into the Database.

Happy Coding!!!

No comments:

Post a Comment