Asp.net date binding error with zeros

I have a null date in my database. I am connecting to it using LinqDataSource and bind to FormView. It allows you to put dates in order, but if you remove the date, I need to insert a null value into the db. Instead, you are throwing an exception.

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

      

Works great if you put a date in it, but if you remove the date and keep it, 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?

+1


source to share


1 answer


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



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

      

+2


source







All Articles