error with my set - “Nullable object must have a value.”

Last post 11-06-2009 3:28 AM by young345. 6 replies.

Sort Posts:

  • error with my set - “Nullable object must have a value.”

    11-05-2009, 4:32 AM
    • Member
      69 point Member
    • young345
    • Member since 02-04-2008, 11:32 AM
    • Posts 405

    Hi,

    I have the following get and set for displaying a date on my webpage. It’s a null value

     

    However, when I go to load the page I get an error on the set part saying the “Nullable object must have a value.”

    Can anyone see where I’m going wrong and how to fix this.

     

    Thank you very much

        public DateTime? Date
            {
                get
                {
                    if (txtDate.Text != "")
                    {
                        return Convert.ToDateTime(txtDate.Text);
                    }
                    return null;
                }
    
                set{ txtDate.Text = value.Value.ToShortDateString(); }               
                
            }
    


  • Re: error with my set - “Nullable object must have a value.”

    11-05-2009, 5:31 AM
    • Star
      8,892 point Star
    • integrasol
    • Member since 06-05-2009, 11:18 AM
    • Denmark & Spain
    • Posts 1,657

    Is the property getting called when the page is being loaded? 

    Thanks

    Carsten

    Please click Mark as Answer if this post is of help to you. :-)



    My Blog
  • Re: error with my set - “Nullable object must have a value.”

    11-05-2009, 6:28 AM
    • Member
      69 point Member
    • young345
    • Member since 02-04-2008, 11:32 AM
    • Posts 405

    Hi,

    Yeah it is. I have a method in the page-load which loads it.

    Any suggestions what’s going wrong?

    Thanks

  • Re: error with my set - “Nullable object must have a value.”

    11-05-2009, 6:32 AM
    • Star
      8,892 point Star
    • integrasol
    • Member since 06-05-2009, 11:18 AM
    • Denmark & Spain
    • Posts 1,657

    Can you show us the code, please? 

    Thanks

    Carsten

    Please click Mark as Answer if this post is of help to you. :-)



    My Blog
  • Re: error with my set - “Nullable object must have a value.”

    11-05-2009, 6:53 AM
    • Member
      188 point Member
    • binli0114
    • Member since 09-26-2006, 12:31 AM
    • Sydney
    • Posts 48
    protected void ButtonClickEvent(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            if (btn.ID == this.btnClickMe.ID)
                MyDateTime = DateTime.Now;
            else
                this.txtDate.Text = MyDateTime.Value.ToString();
            
        }
    
        public DateTime? MyDateTime 
        {
            get
            {
                 if (txtDate.Text != "")  
                 {  
                     return Convert.ToDateTime(txtDate.Text);  
                 }  
                return null;
            }
            set
            {
                this.txtDate.Text = value.Value.ToShortDateString();
            }
        }


    Hi I did try this code snippet and it runs ok.

    would you mind provide some more details of your error.


    ------------------------------------------------------------
    I LOVE THIS GAME
  • Re: error with my set - “Nullable object must have a value.”

    11-05-2009, 4:59 PM
    Answer
    • Contributor
      5,076 point Contributor
    • Paul Linton
    • Member since 04-30-2008, 3:16 AM
    • Posts 883

    I suspect that you are trying to set the value of Date to null during the page load.  The code in the setter is assuming that Date is being set to a non-null value.  Try changing it to

    set {
        if (value.HasValue) {
            txtDate.Text = value.Value.ToShortDateString();
        } else {
            txtDate.Text = string.Empty;
        }
    }


     

    Got a c# problem? Try .NET Book Zero from Charles Petzold, it's a free pdf.
  • Re: error with my set - “Nullable object must have a value.”

    11-06-2009, 3:28 AM
    • Member
      69 point Member
    • young345
    • Member since 02-04-2008, 11:32 AM
    • Posts 405

    Genius, thank you

Page 1 of 1 (7 items)