Error Rendering Control if nullable property is used

Discussion in 'ASP .Net' started by Benton, Apr 17, 2007.

  1. Benton

    Benton Guest

    Hi there,

    I'm creating a custom server control, inheriting from TextBox. It has this
    AsDateTime property that returns the textbox contents converted to the
    nullable DateTime data type, as follows:

    public class TextBoxWeb : System.Web.UI.WebControls.TextBox
    {

    [Browsable(false),
    Bindable(false),
    Category("Behavior"),
    DefaultValue(""),
    Description("Contents converted to \"DateTime\".")]
    public DateTime? AsDateTime
    {
    get
    {
    DateTime validDate;

    if (DateTime.TryParse(this.Text, out validDate))
    return validDate;
    else
    return null;
    }
    set
    {
    this.Text = (value == null) ? string.Empty :
    value.Value.ToShortDateString();
    }
    }
    }


    Well if I drop this control from the toolbox to a Web Form, the control is
    good to go and the property works as expected. So far so good.

    But if drop this control inside a Web User Control and then drop the Web
    User Control onto a Web Form, my server control does not get rendered on the
    designer. Instead I get this error:

    Error Rendering Control - TextBoxWeb1

    An unhandled exception has ocurred.

    Cannot create an object of type 'System.Nullable'1[[System.DateTime,
    mscorlib, Version=2.0.0.0., Culture=neutral, PublicKeyToken=b77a5...etc]]'
    from its string representation '' for the 'AsDateTime' property.

    I struggled with this for hours until I decided to try with the AsDateTime
    property as not nullable:

    public DateTime AsDateTime
    {
    get
    {
    DateTime validDate;

    if (DateTime.TryParse(this.Text, out validDate))
    return validDate;
    else
    return DateTime.MinValue;
    }
    set
    {
    this.Text = (value == null) ? string.Empty :
    value.ToShortDateString();
    }
    }

    And it worked fine both in a web form and in a web user control.

    So... any ideas on what might be happening here? Why can't I use a nullable
    property? Well okay, I can use it if the control goes straight to a web
    form, but I got errors if the control goes to a web user control first and
    then the user control goes to a web form.

    Thanks in advance,

    -Benton
     
    Benton, Apr 17, 2007
    #1
    1. Advertising

  2. Benton

    bruce barker Guest

    DateTime is a value type (storage only - unboxed), not an object, so a
    DateTime property can not return null. you will find the same with other
    value types: int, double, etc.

    -- bruce (sqlwork.com)

    Benton wrote:
    > Hi there,
    >
    > I'm creating a custom server control, inheriting from TextBox. It has this
    > AsDateTime property that returns the textbox contents converted to the
    > nullable DateTime data type, as follows:
    >
    > public class TextBoxWeb : System.Web.UI.WebControls.TextBox
    > {
    >
    > [Browsable(false),
    > Bindable(false),
    > Category("Behavior"),
    > DefaultValue(""),
    > Description("Contents converted to \"DateTime\".")]
    > public DateTime? AsDateTime
    > {
    > get
    > {
    > DateTime validDate;
    >
    > if (DateTime.TryParse(this.Text, out validDate))
    > return validDate;
    > else
    > return null;
    > }
    > set
    > {
    > this.Text = (value == null) ? string.Empty :
    > value.Value.ToShortDateString();
    > }
    > }
    > }
    >
    >
    > Well if I drop this control from the toolbox to a Web Form, the control is
    > good to go and the property works as expected. So far so good.
    >
    > But if drop this control inside a Web User Control and then drop the Web
    > User Control onto a Web Form, my server control does not get rendered on
    > the
    > designer. Instead I get this error:
    >
    > Error Rendering Control - TextBoxWeb1
    >
    > An unhandled exception has ocurred.
    >
    > Cannot create an object of type 'System.Nullable'1[[System.DateTime,
    > mscorlib, Version=2.0.0.0., Culture=neutral, PublicKeyToken=b77a5...etc]]'
    > from its string representation '' for the 'AsDateTime' property.
    >
    > I struggled with this for hours until I decided to try with the AsDateTime
    > property as not nullable:
    >
    > public DateTime AsDateTime
    > {
    > get
    > {
    > DateTime validDate;
    >
    > if (DateTime.TryParse(this.Text, out validDate))
    > return validDate;
    > else
    > return DateTime.MinValue;
    > }
    > set
    > {
    > this.Text = (value == null) ? string.Empty :
    > value.ToShortDateString();
    > }
    > }
    >
    > And it worked fine both in a web form and in a web user control.
    >
    > So... any ideas on what might be happening here? Why can't I use a nullable
    > property? Well okay, I can use it if the control goes straight to a web
    > form, but I got errors if the control goes to a web user control first and
    > then the user control goes to a web form.
    >
    > Thanks in advance,
    >
    > -Benton
     
    bruce barker, Apr 18, 2007
    #2
    1. Advertising

  3. Benton

    Benton Guest

    "bruce barker" <> escribió en el mensaje
    news:%...
    > DateTime is a value type (storage only - unboxed), not an object, so a
    > DateTime property can not return null. you will find the same with other
    > value types: int, double, etc.
    >
    > -- bruce (sqlwork.com)


    The property is DateTime? (nullable type) so it should be able to return
    null.

    Any other ideas?

    Thanks!
     
    Benton, Apr 18, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. buran

    nullable sqlparameter

    buran, Jul 17, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    534
    buran
    Jul 17, 2003
  2. Jacques Wentworth

    Property 'Keycode' is non-nullable

    Jacques Wentworth, Mar 11, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    3,645
    Jacques Wentworth
    Mar 11, 2005
  3. Rafa³ Ziêba

    Nullable property in property grid

    Rafa³ Ziêba, Jul 3, 2008, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    790
    Rafa³ Ziêba
    Jul 3, 2008
  4. Bob Jones

    Composite control - sub control as a property not rendering

    Bob Jones, Apr 6, 2007, in forum: ASP .Net Web Controls
    Replies:
    4
    Views:
    259
    Bob Jones
    Apr 9, 2007
  5. Paul Eaton

    Cannot Get Field Nullable Property

    Paul Eaton, Jul 14, 2003, in forum: ASP General
    Replies:
    2
    Views:
    166
    Aaron Bertrand - MVP
    Jul 14, 2003
Loading...

Share This Page