Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Error Rendering Control if nullable property is used

Reply
Thread Tools

Error Rendering Control if nullable property is used

 
 
Benton
Guest
Posts: n/a
 
      04-17-2007
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

 
Reply With Quote
 
 
 
 
bruce barker
Guest
Posts: n/a
 
      04-17-2007
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

 
Reply With Quote
 
 
 
 
Benton
Guest
Posts: n/a
 
      04-17-2007

"bruce barker" <(E-Mail Removed)> escribió en el mensaje
news:%(E-Mail Removed)...
> 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!


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Nullable property in property grid Rafał Zięba ASP .Net Building Controls 0 07-03-2008 11:32 AM
Composite control - sub control as a property not rendering Bob Jones ASP .Net Web Controls 4 04-09-2007 07:47 PM
IE6 SP1 rendering vs IE6 SP2 rendering Peter Mount HTML 4 01-31-2006 08:01 AM
Property 'Keycode' is non-nullable Jacques Wentworth ASP .Net 0 03-11-2005 11:08 AM
Cannot Get Field Nullable Property Paul Eaton ASP General 2 07-14-2003 05:32 PM



Advertisments