Databinding Dates in ASP .Net 2.0

G

Guest

Is there a good way to databind a textbox to a date field that will not throw
an error when a null is encountered. I tried changing the Null Value in the
dataset , but when I do I get the following message:

For columns not defined as System.String, the only valid value is (Throw
exception).

Normally when a date is not set, I leave it null. Even if I used a value to
represent not set (1/1/1900), I wouldn't want this to display on the form.
This wouldn't be an issue in 1.1, since I could just bind to a method passing
the field, and access the dataset in the code behind. In 2.0, you have to
use the objectdatasource control, and I do not have access to the underlying
dataset.

If anyone has any ideas, I would appreciate it.
 
G

Guest

So since the SqlDateTime is not supported in a dataset, you basically can't
do this if your objectdatasource is using a dataset. Is that correct?
 
B

Brock Allen

So since the SqlDateTime is not supported in a dataset, you basically
can't do this if your objectdatasource is using a dataset. Is that
correct?

Why do you say that? It works fine for me. It probabaly doesn't display exactly
how you'd like when it's null, since that's exactly what it does display:
"Null". The other idea it to either create your own class to hold the DateTime
and override ToString() returning the empoty string when null, or handle
the GridView's (or DataGrid's) RowDataBound (or ItemDataBound) and then going
into the cell that has the null value and changing it to contain an empty
string when the DateTime is null.
 
B

Brock Allen

So since the SqlDateTime is not supported in a dataset, you basically
Why do you say that? It works fine for me.

Oops, I completely misread that you had said DataSet. I read it as DataGrid.
In that case from your ObjectDataSource instead of returning DataSets, consider
returning a collection of custom classes. Sorry for the confusion.
 
G

Guest

Okay, so instead of ASP.Net reducing the amount of time required for me to
develop this page, it is increasing it by 4 - 6 hours while I develop this
class that will basically wrap the dataset.

This is also forcing me to move the formating of my dates from the ui (where
it belongs) to the business class.
 
B

Brock Allen

Well, like I said before as another idea would be to handle the Item/RowDataBound
event and do all your formatting in the page.
 
G

Guest

There is no such event in the formview, but there is a databound and
databinding event. The problem is that there is no way to access the
ObjectDataSource underlying data object to retrieve the data. I can
implement a custom binding method, but without access to the underlying data
it is useless.

Actually I just figured it out. Apparently, you can access the current data
item through the formview instead of the objectdatasource. Using this
information, I can properly bind the date. Now all I have to do is figure
out how to make this work with two way binding.
 
Joined
Jan 27, 2010
Messages
27
Reaction score
0
Binding ObjectDataSourceControl to a collection in ASP.Net 3.5

ObjectDataSource Control has five mostly used properties:
1.TypeName :the name of the type of object the control representing
2.SelectMethod :the method name the ObjectDataSource calls for selecting data
3.UpdateMethod :the method name the ObjectDataSource calls for updating data
4.InsertMethod :the method name the ObjectDataSource calls for inserting data
5.DeleteMethod :the method name the ObjectDataSource calls for deleting data
 

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

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top