Sorry I really wasn't sure which forum to post this in.
But since the below code correctly process data for my gridview on my dev machine but not my server I thought the gridview forum would be best.
I am simply trying to add hours to a time parameter that appears in the gridview. It works fine on my dev PC, but posts this error on server....
String was not recognized as a valid DateTime.
Description:
An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.FormatException: String was not recognized as a valid DateTime.
Source Error:
Line 71: </EditItemTemplate>
Line 72: <ItemTemplate>
Line 73: <asp:Label ID="Label1" runat="server" Text='<%# String.Format("{0:hh:mm:ss tt}", Convert.ToDateTime(Eval("eventTime").ToString).AddHours(-5)) %>' ></asp:Label>
Line 74: </ItemTemplate>
Line 75: </asp:TemplateField>
Here is the code for the template field in my gridview...
Based on the exception being thrown it would appear that the value you are casting to a DateTime is really not a datetime. I'd double check the value coming out of the database.
march11
Contributor
2981 Points
1350 Posts
code works on dev pc but not server
Dec 17, 2012 04:23 PM|LINK
Sorry I really wasn't sure which forum to post this in.
But since the below code correctly process data for my gridview on my dev machine but not my server I thought the gridview forum would be best.
I am simply trying to add hours to a time parameter that appears in the gridview. It works fine on my dev PC, but posts this error on server....
String was not recognized as a valid DateTime.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.FormatException: String was not recognized as a valid DateTime.
Source Error:
Line 71: </EditItemTemplate> Line 72: <ItemTemplate> Line 73: <asp:Label ID="Label1" runat="server" Text='<%# String.Format("{0:hh:mm:ss tt}", Convert.ToDateTime(Eval("eventTime").ToString).AddHours(-5)) %>' ></asp:Label>Here is the code for the template field in my gridview...
<ItemTemplate> <asp:Label ID="Label1" runat="server" Text='<%# String.Format("{0:hh:mm:ss tt}", Convert.ToDateTime(Eval("eventTime").ToString).AddHours(-5)) %>' ></asp:Label> </ItemTemplate>aptbid2002
Member
244 Points
52 Posts
Re: code works on dev pc but not server
Dec 17, 2012 07:06 PM|LINK
Based on the exception being thrown it would appear that the value you are casting to a DateTime is really not a datetime. I'd double check the value coming out of the database.
march11
Contributor
2981 Points
1350 Posts
Re: code works on dev pc but not server
Dec 17, 2012 07:19 PM|LINK
Well, there are NULLs but the dev set up handles them, Web server does not. Most other records are time in 24 hour format
oned_gk
All-Star
30917 Points
6327 Posts
Re: code works on dev pc but not server
Dec 18, 2012 01:31 AM|LINK
Ceck culture in your dev pc by :
Label1.Text = UICulture.ToString(); Label2.Text = Culture.ToString();and set page culture and uiculture same with the results above to web.config or your current page.
sarathi125
Star
13599 Points
2691 Posts
Re: code works on dev pc but not server
Dec 18, 2012 05:23 AM|LINK
Hi,
Please check out the following links about DateTime conversion and culture settings for whole application
link1
link2
link3
Try by following Code
IFormatProvider provider = new System.Globalization.CultureInfo("en-CA", true);
string datetime = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox1")).Text.Trim(); cmd.Parameters.Add("@waiverDate", SqlDbType.DateTime).Value = DateTime.Parse(datetime,provider,System.Globalization.DateTimeStyles.NoCurrentDateDefault);
Remember to click Mark as Answer on the post that helps to others.
My Blog :MyAspSnippets
march11
Contributor
2981 Points
1350 Posts
Re: code works on dev pc but not server
Dec 18, 2012 07:14 PM|LINK
Not sure why, but server side NULLs were causing the issue jsut added this code to fix it....
If Not IsDBNull(eventTime) Then
' Do something
else
return ""
end if