467,927 Members | 1,837 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,927 developers. It's quick & easy.

Handling DBNull and DataBinder.Eval

Hi all,

I have a DatePicker ASP.NET control that allows users to enter date (e.g. a
calendar control). The control has the following property

public DateTime Value { get; set; }

I've placed the control into a datagrid now I'm trying to bind values from
database to the datagrid.

<ctl:DatePicker id="Datepicker1" runat="server" Value='<%#
DataBinder.Eval(Container, "DataItem.MyDateField") %>'>
</ctl:DatePicker>

The problem arises where the value in DB is NULL. I get
"System.InvalidCastException: Specified cast is not valid"

I've looked into the .cs file generated from .aspx under "Temporary ASP.NET
Files" and found this code:

#line 15 "N:\WebStaging\DatePickerTest.aspx"
target.Value = ((System.DateTime)(DataBinder.Eval(Container,
"DataItem.MyDateField")));
At runtime the value from DataBinder.Eval() evaluates into DBNull.Value, and
then it's casted into System.DateTime causing the error.
I am wondering if there was any way to control the generated C# code.
Basically, I want DBNull values to be translated into DateTime.MinValue for
instance.

Thanks,
Oleg.


Nov 19 '05 #1
  • viewed: 10665
Share:
2 Replies
Oleg Ogurok wrote:
Hi all,

I have a DatePicker ASP.NET control that allows users to enter date (e.g. a
calendar control). The control has the following property

public DateTime Value { get; set; }

I've placed the control into a datagrid now I'm trying to bind values from
database to the datagrid.

<ctl:DatePicker id="Datepicker1" runat="server" Value='<%#
DataBinder.Eval(Container, "DataItem.MyDateField") %>'>
</ctl:DatePicker>

The problem arises where the value in DB is NULL. I get
"System.InvalidCastException: Specified cast is not valid"

I've looked into the .cs file generated from .aspx under "Temporary ASP.NET
Files" and found this code:

#line 15 "N:\WebStaging\DatePickerTest.aspx"
target.Value = ((System.DateTime)(DataBinder.Eval(Container,
"DataItem.MyDateField")));
At runtime the value from DataBinder.Eval() evaluates into DBNull.Value, and
then it's casted into System.DateTime causing the error.
I am wondering if there was any way to control the generated C# code.
Basically, I want DBNull values to be translated into DateTime.MinValue for
instance.

Thanks,
Oleg.



Basically you just need to wrap up what you have defined in a 'helper
method' call. Declare a protected method in your code-behind, pass the
existing binding expression as its argument. Something like

Value='<%# myHelper(DataBinder.Eval(Container, "DataItem.MyDateField"))%>'

where myHelper is the method I assume in the code-behind. Then you can
massage the data if needed in the function, as it's called for each item....

An in-depth example is here:

http://www.c-sharpcorner.com/Code/20...tDataListt.asp

--
Craig Deelsnyder
Microsoft MVP - ASP/ASP.NET
Nov 19 '05 #2
Dont forget to use a variable type of Object in the Helper function
parameter. Otherwise you will have conversion problems.

--
Sonu Kapoor - [MCP]
ASP.NET Moderator
WebSite: http://www.Kapoorsolutions.com
Blog: http://www.Kapoorsolutions.com/blog/
ASP.NET News: http://www.Kapoorsolutions.com/reblogger/
"Craig Deelsnyder" wrote:
Oleg Ogurok wrote:
Hi all,

I have a DatePicker ASP.NET control that allows users to enter date (e.g. a
calendar control). The control has the following property

public DateTime Value { get; set; }

I've placed the control into a datagrid now I'm trying to bind values from
database to the datagrid.

<ctl:DatePicker id="Datepicker1" runat="server" Value='<%#
DataBinder.Eval(Container, "DataItem.MyDateField") %>'>
</ctl:DatePicker>

The problem arises where the value in DB is NULL. I get
"System.InvalidCastException: Specified cast is not valid"

I've looked into the .cs file generated from .aspx under "Temporary ASP.NET
Files" and found this code:

#line 15 "N:\WebStaging\DatePickerTest.aspx"
target.Value = ((System.DateTime)(DataBinder.Eval(Container,
"DataItem.MyDateField")));
At runtime the value from DataBinder.Eval() evaluates into DBNull.Value, and
then it's casted into System.DateTime causing the error.
I am wondering if there was any way to control the generated C# code.
Basically, I want DBNull values to be translated into DateTime.MinValue for
instance.

Thanks,
Oleg.



Basically you just need to wrap up what you have defined in a 'helper
method' call. Declare a protected method in your code-behind, pass the
existing binding expression as its argument. Something like

Value='<%# myHelper(DataBinder.Eval(Container, "DataItem.MyDateField"))%>'

where myHelper is the method I assume in the code-behind. Then you can
massage the data if needed in the function, as it's called for each item....

An in-depth example is here:

http://www.c-sharpcorner.com/Code/20...tDataListt.asp

--
Craig Deelsnyder
Microsoft MVP - ASP/ASP.NET

Nov 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Michelle Keys | last post: by
2 posts views Thread by dm_dal | last post: by
4 posts views Thread by SÝren Lund | last post: by
2 posts views Thread by jack | last post: by
4 posts views Thread by CharlesA | last post: by
2 posts views Thread by pvong | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.