How to store an empty date into a column of a SQl server table

Last post 07-26-2009 12:01 AM by anonymouswrites. 2 replies.

Sort Posts:

  • How to store an empty date into a column of a SQl server table

    07-23-2009, 1:51 PM
    • Member
      690 point Member
    • AppDevForMe
    • Member since 12-31-2005, 6:19 PM
    • Posts 852

     Is their a way to store a null value, or a null date (If such a thing exists) into a sql server table? My data table has a date column that is specified to allow nulls. Sometimes the value

    from my web form entered for the date is empty, as entered into the text box for the date. It is crashing though if I try to pass in an emptry string. So how do I pass in a null value for the date?

    The code below is vb.net but I can always convert it to C# 

     

    sprocComm.Parameters.Add(New SqlParameter("@dateOfExpire", Data.SqlDbType.DateTime))
            sprocComm.Parameters("@dateOfExpire").Value = dateToSave

  • Re: How to store an empty date into a column of a SQl server table

    07-23-2009, 2:02 PM
    Answer

    Try something like this (it's C#, sorry):

    sprocComm.Parameters["@dateOfExpire"].Value = String.IsNullOrEmpty(dateToSave) ? DBNull.Value : dateToSave;


    I'm assuming dateToSave is a string.

  • Re: How to store an empty date into a column of a SQl server table

    07-26-2009, 12:01 AM
    Answer

    If your dateToSave is DateTime then you can mark it as nullable

    DateTime? dateToSave;

    sprocComm.Parameters["@dateOfExpire"].Value = dateToSave == null ? DBNull.Value : dateToSave.Value;

Page 1 of 1 (3 items)