asp.net gridview bind dateformat not working with update

Posted by Brabbeldas on Stack Overflow See other posts from Stack Overflow or by Brabbeldas
Published on 2013-01-31T11:51:15Z Indexed on 2013/11/11 3:54 UTC
Read the original article Hit count: 167

Filed under:
|
|
|

I have a GridView with a TemplateField column which shows a DateTime from a DataSource.

<Columns>
    <asp:CommandField ShowEditButton="True" />

    <asp:TemplateField HeaderText="Start Date">
        <EditItemTemplate>
            <asp:TextBox ID="txtDateStart" runat="server" 
                Text='<%# Bind("dtDateStart", "{0:dd/MM/yyyy}") %>'</asp:TextBox>

        </EditItemTemplate>
        <ItemTemplate>
            <asp:Label ID="Label1" runat="server" 
                Text='<%# Bind("dtDateStart", "{0:dd/MM/yyyy}") %>'></asp:Label>
         </ItemTemplate>

    </asp:TemplateField>
</Columns>

Displaying the date in the correct format works as it should. Note that the format starts with DAY followed by MONTH.

When I switch to edit mode, change the date in the TextBox to '31-01-2013' and press the GridView's update-link i get an error: Cannot convert value of parameter 'dtDateStart' from 'System.String' to 'System.DateTime' The error is generated by the GridView not my own code. It happens before the UpdateMethod of my DataSource is called.

When i type '01-31-2012' the data is processed correctly and the value is updated into the database.

Somehow when the date is displayed it uses format dd-MM-yyyy (just as I need it to) But when it reads the new value form the TextBox it uses MM-dd-yyyy

Can somebody please help me?

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about datetime