Handling DBNull and DataBinder.Eval

O

Oleg Ogurok

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.
 
C

Craig Deelsnyder

Oleg said:
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/2003/July/ConditionalFormatDataListt.asp
 
Joined
Sep 1, 2006
Messages
1
Reaction score
0
you can also do a small trick by adding an empty string
DataBinder.Eval(Container, "DataItem.MyDateField") & ""
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top