How can insert a null value into Date column?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vasavivenu
    New Member
    • Feb 2011
    • 37

    How can insert a null value into Date column?

    Hi,

    I have one Date column, it's not a mandatory so when i gave empty to that textbox it's automatically inserts a system start date. how can i solve?

    Thanks,
    vasavivenu.
  • Ravi L
    New Member
    • Oct 2010
    • 12

    #2
    While inserting the value to the column in your procedure or T-SQL statement check if the date value coming from the textbox is empty and insert NULL. If not insert the value as it is.

    As you are sending an empty string SQL server internally inserts the starting date (1900-01-01 00:00:00 000) of the DateTime type range.

    Hope this helps.

    Comment

    • neelmukhopadhya
      New Member
      • Dec 2010
      • 23

      #3
      If you dont mention the date , it will automatically put system date.SO it is better to check whether you have entered the date or not. If you are using datetime type variable then take it as nullable
      like, DateTime? datetime. then only you can put null valus to the datecolumn.

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        You can try using DBNull.Value, that is what is used in columns when NULL is allowed.

        Comment

        • balajiu
          New Member
          • Sep 2007
          • 4

          #5
          I think you are using like textbox1.text as a value for the date object. If so do the below.

          Better to use nullable type.

          Ex:

          DateTime? ordDate;

          ordDate=TextBox OrdDate.Text;

          then use this date1 object to insert into database like this.

          string sqlInsert = "insert into orders(orderDat e) values('"+ordDa te+"');


          I think it will help you.

          Comment

          Working...