views:

431

answers:

2

I have a nullable date in my database. I am connecting to it with a LinqDataSource, and binding with a FormView. It allows you to place dates fine, but if you remove the date I need it to insert the null value to the db. It is instead throwing an exception.

<asp:TextBox ID="TxtStartDate" runat="server" 
                    Text='<%# Bind("StartDate", "{0:MM/dd/yyyy}") %>' />

Works fine if you place a date in it, but if you delete the date out of it and save, you get System.Data.SqlTypes.SqlTypeException: SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM. How do I make it send null?

+2  A: 

I don't know if it was the best way, but I fixed it by subscribing to the FormView Updating event and placed the following code:

object o = e.NewValues["StartDate"];
   if (o.ToString() == "")
       e.NewValues["StartDate"] = null;
Matthew Kruskamp
A: 

Cheers Matthew. This sorted out my issues with null dates and also enabled me to use the compare validator for testing that the end date >= start date on my formview. The compare validator acounts for null end dates and doesnt give a warning when the end date is null - which is the behaviour I wanted.

Superstar dude!!

Ben Hargreaves