StrataFrame Forum
Home      Members   Calendar   Who's On
Welcome Guest ( Login | Register )
      


12»»

Null Dates in Webforms, How?Expand / Collapse
Author
Message
Posted 01/03/2008 3:37:32 PM
StrataFrame Beginner

StrataFrame BeginnerStrataFrame BeginnerStrataFrame BeginnerStrataFrame BeginnerStrataFrame BeginnerStrataFrame BeginnerStrataFrame BeginnerStrataFrame Beginner

Group: StrataFrame Users
Last Login: 08/26/2008 2:14:48 PM
Posts: 20, Visits: 80
How do I properly use null dates in a webforms application?  When working with null dates I get data binding errors on postbacks.  We use nulls to at least have a nominally empty date field but can't seem to get it to work in webforms.  I searched mutiple times and can't find any references to what the best way to do it so that customers won't see a date when entering data if the field is not supposed to have a date in it yet.  I've tried the return alternate using #1/1/1800# and then that shows in the textbox on the screen, I could live with the return alternate concept if it just didn't show in textboxes and listviews.

Is there an easy way to do this or do I need to not bind the data and handle it myself? (which is what I'm trying to avoid).

Post #13228
Posted 01/03/2008 9:28:25 PM
StrataFrame User

StrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame User

Group: StrataFrame Users
Last Login: 08/26/2008 9:35:35 PM
Posts: 222, Visits: 1,093
Hi Cyrus,

You may have already done this but, if not, try a search for "null date" in all forum posts. From memory this has come up quite a number of time in the past - maybe the answer you want is in the archives.

Cheers, Peter

Post #13232
Posted 01/04/2008 8:45:05 AM


StrataFrame Developer

StrataFrame Developer

Group: StrataFrame Developers
Last Login: 08/01/2008 8:53:41 AM
Posts: 2,671, Visits: 1,879
Since a DBNull.Value cannot be cast to a DataTime, you must return an alternate date through the property.  If you are binding the value to a textbox or displaying it in a listview, then that alternate date is going to display.  In order for the alternate date to not display within a textbox, you will need to create your own inherited textbox and change the display when the alternate value is returned.  For the value within a listview, you will need to change the column displaying the value to PopulatedThroughEvent rather than using FormattedString; this will allow you to programmatically set the value for the column through the RowPopulating event of the list view.


www.bungie.net
Post #13240
Posted 01/04/2008 12:00:12 PM
StrataFrame VIP

StrataFrame VIPStrataFrame VIPStrataFrame VIPStrataFrame VIPStrataFrame VIPStrataFrame VIPStrataFrame VIPStrataFrame VIP

Group: StrataFrame Users
Last Login: Yesterday @ 7:45:03 PM
Posts: 1,210, Visits: 3,074
Ben,

What event should you handle (override) in the inherited textbox to handle this issue?
Post #13252
Posted 01/09/2008 2:49:01 PM
StrataFrame Beginner

StrataFrame BeginnerStrataFrame BeginnerStrataFrame BeginnerStrataFrame BeginnerStrataFrame BeginnerStrataFrame BeginnerStrataFrame BeginnerStrataFrame Beginner

Group: StrataFrame Users
Last Login: 08/26/2008 2:14:48 PM
Posts: 20, Visits: 80
Greg McGuffey (01/04/2008)
Ben,

What event should you handle (override) in the inherited textbox to handle this issue?

Yeah, what he said!  I'm willing to do that, but I do need to know which event would be best to do this.  I can't have the users seeing the 1/1/1800 value.  I know it may take a little more work to handle the situation for the .net gridview which we use because we get paging and such.

I guess what your saying is that the value involved both ways must be a valid date or there are problems?  I can live with that I guess as long as I can figure out how to inherit the textbox and change it both ways on the fly.

Post #13336
Posted 01/11/2008 8:28:08 AM


StrataFrame Developer

StrataFrame Developer

Group: StrataFrame Developers
Last Login: 08/01/2008 8:53:41 AM
Posts: 2,671, Visits: 1,879
You'll want to create your own property on the TextBox, say "Value" or "MyText" (something other than Text since it's already taken).  Make that property a DateTime property and that property will then be used for your binding (so, bind the business object to that property, not the Text property. 

In the Get of that property, if the Text is empty, then return 1/1/1800, if the text has a date, then use DateTime.Parse() to get the DateTime value from the Text and return it. 

Then, in the Set of the property, if the value is 1/1/1800, set the text to String.Empty.  Otherwise, set the Text to value.ToString("MM/dd/yyyy") (where the MM/dd/yyyy is the format you want the dates to appear in). 

Luckally, WebForms binding is much simpler than WinForms binding, so you won't have to worry about creating all of those changed and changing events to get the data to bind properly.


www.bungie.net
Post #13355
Posted 01/11/2008 11:18:25 AM
StrataFrame VIP

StrataFrame VIPStrataFrame VIPStrataFrame VIPStrataFrame VIPStrataFrame VIPStrataFrame VIPStrataFrame VIPStrataFrame VIP

Group: StrataFrame Users
Last Login: Yesterday @ 7:45:03 PM
Posts: 1,210, Visits: 3,074
So, in WinForms, you'd also need to add the changing/changed events to get this to work. Anything else to get to work with a WinForms control?
Post #13365
Posted 01/11/2008 1:03:06 PM


StrataFrame Developer

StrataFrame Developer

Group: StrataFrame Developers
Last Login: 08/01/2008 8:53:41 AM
Posts: 2,671, Visits: 1,879
Ah, for WinForms, when you add a bindable property, you also have to add a "Changed" event that corresponds to the bindable property and it has to have the same signature as EventHandler:

Public Event MyTextChanged As System.EventHandler '-- Your property is named "MyText"

You then raise this event when the property changes so that the binding knows to copy the new data back to the data source.  The "Changing" event isn't needed by the binding, but sometimes it's nice to have; I would only add it when you need it.


www.bungie.net
Post #13368
Posted 01/11/2008 3:46:00 PM
StrataFrame VIP

StrataFrame VIPStrataFrame VIPStrataFrame VIPStrataFrame VIPStrataFrame VIPStrataFrame VIPStrataFrame VIPStrataFrame VIP

Group: StrataFrame Users
Last Login: Yesterday @ 7:45:03 PM
Posts: 1,210, Visits: 3,074
Got it, thanks!
Post #13391