473,326 Members | 2,125 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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
2 11111
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Michelle Keys | last post by:
Subject: DataBinder.Eval Error! Server Error in '/MSPOS' Application. ------------------------------------------------------------------------ -------- DataBinder.Eval:...
2
by: dm_dal | last post by:
I have a control on my webform that I am binding to a dataset. The issue is, the field value in the dataset is encrypted and I am trying to decrypt it during the binding process: Example: ...
5
by: bg | last post by:
Hi! How do I check if "date" exists before using that code? I've built a RSSreader and sometimes there's a date in it and sometimes not. How can I check if it exists to avoid crash...
4
by: Søren Lund | last post by:
Hello, I am trying to bind a DataGrid with some data from a DataSet which contains fields in the form "group.fieldname". I am certain that my data source contains the field but I cannot get my...
1
by: Mike Lerch | last post by:
Pretty much, TSIA, but I'll expand a bit: I'm still trying to get my head around the n-tier approach to web design. It seems to me that when you use DataBinder.Eval in the ASPX that your tiers...
2
by: jack | last post by:
Hello, I have a date in a SQL Database and want to use the dateformat tools to format the output. I am now using the Month, Day, and Year to do it now. I want to change: Released Date:...
3
by: Eric Newton | last post by:
Given databinding an array of System.Version types: Given that "SomeObject" type has a Version property: public class SomeObject { public Version Version { get; } public string Description {...
4
by: CharlesA | last post by:
a general question... I'm a bit mystified by Databinder.eval(object, Colname, ) if controls can be bound to their individual data sources within the load event of the page... why would we ever...
2
by: pvong | last post by:
Doing this in VB.NET I have a gridview RowDataBound. I want to do a test on each row column (12). Some of the rows have NULL value. This is the error message I'm getting. Object cannot be...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.