views:

419

answers:

2

I have an ASP.Net 3.5 page that contains a FormView control, bound to a Business Object using an ObjectDataSource.

One of the properties of the business object is a DateTime type, and I want to do 2 way databinding for this object, including the DateTime property.

I use a custom format for displaying the DateTime property, as shown here:

<asp:TextBox ID="TextBoxDate" runat="server" Text='<%# Bind("Date", "{0:d MMM yyyy HHmm}") %>' />

It displays just fine. The problem is when I attempt to perform an Update. I get the following exception:

String was not recognized as a valid DateTime.

My ObjectDataSource contains an explicitly set UpdateParameter for this Property, but it doesn't appear to make any difference.

    <UpdateParameters>
        <asp:Parameter Name="Date" Type="DateTime" />
    </UpdateParameters>

What am I doing wrong?

UPDATE:

It turns out that if I change my format string in my Bind expression to

{0:d MMM yyyy HH:mm}

(Notice the colon between the HH and the mm)

... then the two way data-binding works as expected. Sadly, this isn't exactly what I wanted. I was hoping to use the 24 hour clock without a colon, hence my original format string. This is still not working, and I would love to know why? And even better, I'd love to know how I can work-around this shortcoming in the framework, but still do declarative databinding.

Thanks.

+1  A: 

Bind() method, actually provide two way data binding and Eval provide one way data binding
In your scenario, you have to modify date in your formview databound event when you are showing data to user rather setting date format in bind property.

In this way you will get not error when update your fields.

Muhammad Akhtar
Muhammad, are you suggesting that I'm unable to do a declarative Bind by using this Bind expression: <%# Bind("Date", "{0:d MMM yyyy HHmm}") %>And that instead, I should set my format in code on the DataBound event?I have upvoted your answer, because it is helpful, but I was really hoping that I would be able to do a declarative bind, rather than having to write code on the DataBound event...
Scott Ferguson
what you are trying is, you are setting format to datetime, When it is displayed then it is ok but when same formatted parameter is sent for update then it is creating problem.
Muhammad Akhtar
Issue can be resolved in 2 way, 1 that I have mentioned in my answer to change it databound when you are displaying and other is send this parameter in reformated shape in formview updating event.
Muhammad Akhtar
so I think first one is better, we mostly used the first one
Muhammad Akhtar
A: 

You could override the ItemUpdating event of the FormView control and then modify the value for that parameter to make sure it's in the right format. Most likely this means:

  1. get the value from e.NewValues("Date")
  2. Parse the string value into a DateTime object
  3. assign the value back to e.NewValues("Date")

I've had to do something like this in the past with currency fields where people might enter a dollar sign which would cause errors if left alone.

patmortech