472,952 Members | 2,161 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,952 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 11056
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: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

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.