1

In an ASP.Net DetailsView is a templete field called LateTimeArrivedAtSchool. The data for this field is from a SQL Server Time column. The data in the database in stored as hours, minutes and seconds.

We would like to display it as only hours and minutes.

Here is the markup for the template field:

<asp:TemplateField HeaderText="Late Time Arrived At School:" SortExpression="LateTimeArrivedAtSchool">
    <EditItemTemplate>
        <asp:TextBox ID="TextBoxLateTimeArrivedAtSchool" runat="server" 
            Text='<%# Bind("LateTimeArrivedAtSchool") %>'></asp:TextBox>
    </EditItemTemplate>

    <InsertItemTemplate>
        <asp:TextBox ID="TextBoxLateTimeArrivedAtSchool" runat="server" 
            Text='<%# Bind("LateTimeArrivedAtSchool") %>'></asp:TextBox>
    </InsertItemTemplate>

    <ItemTemplate>
        <asp:Label ID="LabelLateTimeArrivedAtSchool" runat="server" 
            Text='<%# Bind("LateTimeArrivedAtSchool", "{0:hh:mm}") %>'></asp:Label>
    </ItemTemplate>

    <ItemStyle ForeColor="Blue" />
</asp:TemplateField>

using "{0:hh:mm}" produces an error that states the "Input string was not in a correct format."

Can you tell me what we need to use in the Bind statement?

* UPDATE * I changed the SQL Server data type of the column from a Time column to a DateTime column and was able to display the time only portion of the data.

Emad-ud-deen
  • 4,734
  • 21
  • 87
  • 152
  • You may have a look at this [question](http://stackoverflow.com/questions/275194/formatting-databinder-eval-data). It shows how to use a format on Binding in aspxControls – Pilgerstorfer Franz Jan 03 '13 at 20:40

1 Answers1

1

Use eval instead of bind

Text='<%# string.Format("{0:hh:mm}", Eval("LateTimeArrivedAtSchool")) %>'
Sunny
  • 4,765
  • 5
  • 37
  • 72
  • Thanks everyone for the replies. I will try the one from Sundeep and also look at the link in the above comment and let you know how it works out. – Emad-ud-deen Jan 03 '13 at 22:00
  • Trying Eval gave the same error so I will try to see if I can learn how to do it from the link posted by Pilgerstorfer Franz. – Emad-ud-deen Jan 04 '13 at 13:41
  • The link suggested that I use DataBinder.Eval but that also gave the same error. I'm now going to try and change the SQL Server data type of the column from Time to DateTime and see if that makes a difference. – Emad-ud-deen Jan 04 '13 at 13:52
  • That was it. I changed the SQL Server column data type to DateTime and was able to display the time only portion with the existing original markup. – Emad-ud-deen Jan 04 '13 at 13:59
  • Sundeep, your markup also worked after I changed the column data type. :-) – Emad-ud-deen Jan 04 '13 at 14:21