none
How i can insert null in datetime field

    Question

  • Hi,

     

    How i can insert null into the DateTime field in Table?it's possible or not .when  i insert a value into date textbox then i Convert it into the Datetime its not a problem but if i didn't insert any value into text box then it take current date ( my class property default value).How i can avoid this problem

    person.SignatureDate=Convert.ToDateTime(txtSignatureOnFile.Value).Date;

    __SignatureDate = DateTime.Now;

    Wednesday, March 15, 2006 10:37 AM

All replies

  • In .NET 2.0 you can use Nullable Types, but when you use .NET 1.x you can't.

    When you can set the value to DateTime.MinValue or something to indicate it is not set.

    Wednesday, March 15, 2006 10:53 AM
  • Hi,

    Thanks for replying but i use .NET 1.x  any other suggestion

     

     

    Thanks

    Jignesh

    Wednesday, March 15, 2006 11:45 AM
  • Then you only can use a value like DateTime.MinValue and handle that as a null-value. So when you value equals DateTime.MinValue you handle it like it is never set.
    Wednesday, March 15, 2006 12:07 PM
  • Make a second textbox and set it's visibility to false.  When the change event fires for the date picker, write the date value to the new textbox.  When you write the date value, use the new textbox instead of the date control.  If the date isn't set, then the textbox is empty and you can insert the Null value.
    Wednesday, March 15, 2006 3:53 PM
  • Why should you use a second Textbox for this?
    Wednesday, March 15, 2006 4:11 PM
  • It doesn't have to be a textbox, it could be any string variable.  The point is that the value of the variable (or textbox) is null unless a specific date is selected.  If you were to make your comparison on the MinDate of the date picker, then the user wouldn't be able to select the minimum date.  Also, a hardcoded comparison against the MinDate would break if the MinDate property was ever changed.  By using a seperate textbox or variable, the developer doesn't have to worry about breaking the logic by changing the date picker in any way.
    Wednesday, March 15, 2006 4:23 PM
  • Hi,

    But the actual problem created by my property ( SignatureDate) its having a default value of current date [__SignatureDate = DateTime.Now; and i can't remove this. its a needed] if the textbox  should have a value then no problem its ok.but when the textbox should have no value then my below condition is not satisfied and person.SignatureDate take a current date value.

    if(person.SignatureDate.ToString() != null && person.SignatureDate.ToString().Trim().Length > 0)

    {

    person.SignatureDate=Convert.ToDateTime(txtSignatureOnFile.Value).Date;

    }

     

    Thursday, March 16, 2006 11:47 AM
  • Your condition needs to either test if the date is the current date or test the textbox (which ever way you want to handle it).  Since the control you're using has a defualt value, your condition will never be false.
    Thursday, March 16, 2006 5:12 PM
  • Another option is not actually work with DateTime object. You can wrap it inside a class, for example:

    class DateTimeWrapper

    {

    DateTime _dateTime = DateTime.Now;

    //do something..

    }

     

    Now, this class is nullable.

    Another option is to work with long variables that represents the number of ticks. You can convert in and from by using the TimeSpan object.

    It is up to you to think what is more suitable for your application.

    Saturday, March 18, 2006 12:37 PM