locked
DateTime value RRS feed

  • Question

  • i want to update data to my DB that contain the datetime value. but that column is allow DB null value. and when my customer dont pick a day in datetime picker, there fore it must be the null value. but it get an error when i set it to DB null value. assump that the table is name tmp and i code:
    public class tmp
    {
      int id;
      public int id
      {
        get { return id; }
        set { id = values; }
      }
      DateTime  date1;
      public DateTime date1
      {
        get { return date1; }
        set { date1 = value; }
       }

    }

    and code to update:
    tmp instance = new tmp();
    instance.id = id;
    if(dtpicker.Text.Trim() == "")
     instance.date1 = DBNull;//error here
    else
    {
    try
    {
      // set value here
      instance.date1 = DateTime.Parse(dtpicker.Text);
    }
    catch
    {
      MessageBox.Show("error");
      return;
    }
    }
    // and then update to database here.
    how do i set the value to null?


    mtd
    Sunday, August 2, 2009 2:23 PM

Answers

  • Use DateTime? so it can store a null value:

        DateTime? mDate1;
        public DateTime? Date1 {
          get { return mDate1; }
          set { mDate1 = value; }
        }
    ...
          instance.Date1 = null;


    Hans Passant.
    • Proposed as answer by Sekar Perumal Monday, August 3, 2009 5:14 AM
    • Marked as answer by middlecowboy Monday, August 3, 2009 2:49 PM
    Sunday, August 2, 2009 3:22 PM

All replies

  • Hi

    Try

    instance.date1 = DBNull.Value;

    http://akbarbuneri.blogspot.com/
    Sunday, August 2, 2009 2:50 PM
  • Use DateTime? so it can store a null value:

        DateTime? mDate1;
        public DateTime? Date1 {
          get { return mDate1; }
          set { mDate1 = value; }
        }
    ...
          instance.Date1 = null;


    Hans Passant.
    • Proposed as answer by Sekar Perumal Monday, August 3, 2009 5:14 AM
    • Marked as answer by middlecowboy Monday, August 3, 2009 2:49 PM
    Sunday, August 2, 2009 3:22 PM
  • nobugz, MVP, Moderator - are you really, really, really (!!) sure that DateTime object that is value type can be assigned to null ?  Are you really really sure ?  :)

    P.S. According to error this person obviously using .NET 2 - where this error is most popular between amateurs :)
    Sunday, August 2, 2009 3:26 PM
  • I'm really, really sure that you can't assign null to a DateTime variable.  The OP will be happy with DateTime?, a nullable type.

    Hans Passant.
    Sunday, August 2, 2009 4:14 PM
  • i agree with you. DateTime is class, like pointer in C++, it can't assign to a value. DBnull not a reference, it just a constant. im detecting a program, that program using hibernate technology. my customer want to add a field datetime to manage the date they commit information. i must insert a column in database and same in the class file and xml file. but i can't assign value null to datetime when i update data if my customer doesn't pick a day in datetime picker. if i set it to datetime min, then when my customer review the info of this article, the "1/1/1000" will appear in the datetime picker. how do i manage it :(
    mtd
    Sunday, August 2, 2009 4:43 PM
  • Hi

    You have to keep in mind that it will change the value of datatime object not in the database, you have to set the datetime instance to DataTime.MinValue, and when you make a pramater check if the date is equal to MinValue of DateTime, then set it to DbNull.Value other wise use DateTime value,


    http://akbarbuneri.blogspot.com/
    Monday, August 3, 2009 4:46 AM
  • hey it done. it pretty cool. i can set it to null and compare to null value.
    instance.Date1 = row[date1] is DBNull ? (DateTime?) null : row[date1] as DateTime;
    it cool. thank you.
    mtd
    Monday, August 3, 2009 2:52 PM