Results 1 to 2 of 2

Thread: Error about converting when not converting

  1. #1

    Thread Starter
    Banned
    Join Date
    May 2006
    Posts
    161

    Error about converting when not converting

    In my backend db I have a field in the table of type datetime. At times this field could recieve a datetime or a null value. So I need to display inside of a datagrid asp.net field the date and time if it is not null. But I need to display it so that the date is on one line and the time right under it. No problem I can do that...however here is the code I have:

    Code:
    <asp:TemplateColumn SortExpression="3" HeaderText="Target">			<HeaderStyle HorizontalAlign="Center"></HeaderStyle>
    <ItemStyle Wrap="False" HorizontalAlign="Center" Width="8%"></ItemStyle>
    <ItemTemplate>
    <asp:Hyperlink runat="server" CssClass="dgHyperlinks" Text='<%# IIF(Container.DataItem("TargetDate") IS DbNull.Value, DataBinder.Eval(Container.DataItem, "TargetDate", "{0:dd-MMM-yy}"), DataBinder.Eval(Container.DataItem, "TargetDate", "{0:dd-MMM-yy}")  + "<br>" + (DataBinder.Eval(Container.DataItem, "OpenDate")).ToShortTimeString())  %>' NavigateUrl='<%# "TicketActions.aspx?TicketID=" & Server.UrlEncode(Container.DataItem("TicketID"))%>' ID="hlTarget"/>
    </ItemTemplate>
    </asp:TemplateColumn>
    But in some cases I get an error on the page stating:

    Code:
    An Error Occurred: System.InvalidCastException: Cast from string "21-Jul-06 9:33 AM" to type 'Date' is not valid. at Microsoft.VisualBasic.CompilerServices.DateType.FromString(String Value, CultureInfo culture) at Microsoft.VisualBasic.CompilerServices.DateType.FromString(String Value) at quikfix.jakah.com.facilityhome.dgTickets_ItemDataBound(Object sender, DataGridItemEventArgs e) in \\jakah-iis-2\QuikFix\facilityhome.aspx.vb:line 803 at System.Web.UI.WebControls.DataGrid.OnItemDataBound(DataGridItemEventArgs e) at System.Web.UI.WebControls.DataGrid.CreateItem(Int32 itemIndex, Int32 dataSourceIndex, ListItemType ...
    I am not sure why it is trying to convert it to a date? Its supposed to be a template column..How do I go about fixing this ?

    Thanks,
    Jon

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Error about converting when not converting

    It's a template column, couldn't you populate the grid from the ItemDataBound event? All this inline code makes me dizzy (and is bad practice anyways).

    Use DateTime.Parse

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width