Results 1 to 4 of 4

Thread: [RESOLVED] DateTimePicker - custom DataBinding to return DBNull.Value

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2016
    Location
    Slovenia
    Posts
    575

    Resolved [RESOLVED] DateTimePicker - custom DataBinding to return DBNull.Value

    Hi,

    I've created a custom DateTimePicker which shows empty If Databinded to DBNull.Value, with a trick of setting CustomFormat. Only problem I'm facing is to return DBNull.Value from DateTimePicker when I save changes to DB. Here is my code:

    Code:
      public object MyValue
            {  
                //Binding with DBNull
                get
                {
                    if (this.Format == DateTimePickerFormat.Custom)
                    {
                         return DBNull.Value;
                    }  
                    else
                    {
                        return this.Value;
                    }                  
                   
                }
                set
                {
                    if (value == DBNull.Value)
                    {
                        this.Format = DateTimePickerFormat.Custom;
                        this.CustomFormat = " ";
                        //DateTime? dt =DBNull.Value; //error here
                        //Value = (DateTime)dt;
                    }
                    else
                    {
                        this.Format = DateTimePickerFormat.Short;
                        this.Value = (DateTime)value;
                    }
                }
            }
    I bind to DateTimePicker simply as this: DTP.DataBindings.Add("MyValue", dt, "SOME_COLUMN");

    Error I'm facing is "Nullable object must have a value?".

    Can somebody help me If there is any solution to this ? Currently I just manually set DB.NULL in place where stored procedure is executed, and I want to get rid of these lines.

  2. #2

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2016
    Location
    Slovenia
    Posts
    575

    Re: DateTimePicker - custom DataBinding to return DBNull.Value

    Solved.

    Only problem was that I was referencing "DTP.Value" in my stored procedure, but I should reference "DTP.MyValue" instead. Saving to DB now works, with DBNull.Value At least someone could benefit a little in future when dealing with DateTimePicker like this

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,348

    Re: [RESOLVED] DateTimePicker - custom DataBinding to return DBNull.Value

    Don't use a name like MyValue for a property. Something like BindableValue would be more appropriate.

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2016
    Location
    Slovenia
    Posts
    575

    Re: [RESOLVED] DateTimePicker - custom DataBinding to return DBNull.Value

    Hi jm, thanks for that. It's not MyValue in real actually, It's some word in my language so I'm pretty sure CLR won't mix It with something else

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width