code works on dev pc but not server - asp.net

The below code correctly processes data for my gridview on my dev machine but not my server.
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.
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>
How can I check for Null with inline code?
Any suggestions would be greatly appreciated.

It sounds like a data issue between local dev and the server; I would check what is being pushed to eventTime. I bet it is not a valid DateTime.
EDIT
Per your edit, you can use the ternary operator (?:) to perform an inline null check:
<asp:Label ID="Label1" runat="server" Text='<%# Eval("eventTime") != null ? String.Format("{0:hh:mm:ss tt}", Convert.ToDateTime(Eval("eventTime").ToString).AddHours(-5)) : "No time specified" %>' ></asp:Label>

Related

Trying to pass a parameter in ASP.NET ! Parsing error

I am trying to pass a parameter from a page to another. I am getting parsing errors (Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately. )
This is the code :
<a runat="server" href="~/ProductDetails.aspx?IDProduct=<%# Eval("IDProduct") %>">
<asp:Label Font-Size="16px" ForeColor="Red" runat="server">get specifications</asp:Label>
</a>
I am getting errors at IDProduct=<%# Eval("IDProduct") %>!
How should i write it ?
The proper way to handle such cases is to make the whole attribute value generated inside <%# %>. Also note the updated quoting pattern - single quotes around attribute value, and double quotes inside <%# %>.
href='<%# "~/ProductDetails.aspx?IDProduct=" + Eval("IDProduct") %>'
Try to do this
<a runat="server" href="~/ProductDetails.aspx?IDProduct="<%# Eval("IDProduct") %>>

ASP.Net Listview Eval time format

I can't seem to get the format right for an Eval statement in Label text in a Template on an ASP.Net Listview with VB. I did try escaping the time but that still resulted in an invalid format. The data type in the SQL Server for the bound data is time(0).
Text='<%# Eval("Time", "{0:hh:mm tt}") %>'
Has anyone run into this seemingly simply problem before and found the correct format? I did research what is posted at http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx but did not find a solution to this problem.
This one had me tearing my hair out for a while... but I found the solution:
The second colon character needs to be escaped in your format string with a backslash.
Try this, it should work:
Text='<%# Eval("Time", "{0:hh\:mm}") %>'
Try
"{0:hh.mm tt}")
Does that help?
I hope this solve your problem.
Text='<%#String.Format(System.Globalization.CultureInfo.InvariantCulture,"{0:hh:mm tt}",Eval("Time")) %>'
you can add this to your label :
DataFormatString="{0:dd/MM/yyyy}" //display date only
DataFormatString="{0:hh:mm tt}" //display time only
DataFormatString="{0:dd/MM/yyyy hh:mm tt}" //display both date and time
Text='<%# Eval("Time") %>'
also this link will help you alot for anykind of this problems
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.boundfield.dataformatstring.aspx

Display Local Time in GridView

I have an asp.net gridview bound to the following sql server 2008r2 data source:
<asp:SqlDataSource ID="dsFault" runat="server" DataSourceMode="DataSet"
ConnectionString="<%$ ConnectionStrings:ESBExceptionDb %>"
SelectCommand="select * from Fault order by datetime desc"></asp:SqlDataSource>
In my Fault table there is a column called DateTime of type datetime. The values stored in this column are in UTC format and I need them to be displayed in the local timezone for the browser
I have added a template field to the Columns collection of the grid view as follows:
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblLocalTime" runat="server"
Text='<%# String.Format("{0:f}", Eval("DateTime").ToLocalTime()) %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
When I browse the page I get the following error:
CS1061: 'object' does not contain a definition for 'ToLocalTime' and no
extension method 'ToLocalTime' accepting a first argument of type 'object'
could be found (are you missing a using directive or an assembly reference?)
Can anyone please tell me where I've gone wrong?
Thanks, Rob.
The Eval("DateTime") Value that returns from your database is not a C# DataTime object.
and because the function .ToLocalTime() belongs to the DateTime c# object you can't use it.
You need to convert the object to string and then use the function .ToLocalTime()
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblLocalTime" runat="server"
Text='<%# Convert.ToDateTime(Eval("DateTime")).ToLocalTime() %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
Ones you converted it to DateTime Object, you can use any format available
For Example
Text='<%# Convert.ToDateTime(Eval("DateTime")).ToString("dd/MM/yyyy") %>'
In addition to the other answers, remember that ToLocalTime() will return the SERVER local time not the browser local time as you requested.
Try like this
<%# Eval("DateTime", "{0:T}")%>
<asp:Label ID="lblLocalTime" runat="server"
Text='<%# Eval("DateTime", "{0:T}")%>'>
</asp:Label>
This will do the trick! Give this a try.
<asp:TemplateField HeaderText="Account Created">
<ItemTemplate>
<asp:Label ID="lblLocalTime" runat="server"
Text='<%# Convert.ToDateTime(Eval("CreateDate", "{0:g}")).ToLocalTime() %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
It looks like there is no clean and simple way to do this, strange!!. The answer's mentioned so far try to convert the date to server timezone, not to client/browser's. i.e if the server is in USA and client in India, you won't see the datetime in indian timezone, it will be in USA's.
A duplicate of this has better answers. Basically, you either have to do one of below -
Send client timezone information to the server and do approp conversion there. It is better to send the timezone name rather than timezone offset as timezone offset can differ based on DST. You can use Intl.DateTimeFormat().resolvedOptions().timeZone to send the timezone name.
Send UTC date strings to
the client and let it do the conversion using javascript(new
Date(ISOUTCDateString)). This is what happens in WCF.

w3c validation error in asp.net

I am new in W3c validations, I am trying to fix this error but it's not happening. The error is following:
character "&" is the first character of a delimiter but occurred as data.
I am using DataList Control to bind data and here is the line where the w3c validation error occurs.
<asp:Label ID="lblDescription"
runat="server"
Text='<%#Eval("Decr") %>'>
</asp:Label>
In database, the Decr is stored and this(&) special character is also given in the description field. w3c is not validating this line.
& is a special charater for concat, you need to escape it: make them all & not &.
Here is the solution I came up with:
<asp:Label ID="lblDescription" runat="server" Text='<%# Server.HtmlEncode( (string) Eval("Decr")) %>'></asp:Label>

what is the use of Eval() in asp.net

What is the use of Eval() in ASP.NET?
While binding a databound control, you can evaluate a field of the row in your data source with eval() function.
For example you can add a column to your gridview like that :
<asp:BoundField DataField="YourFieldName" />
And alternatively, this is the way with eval :
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lbl" runat="server" Text='<%# Eval("YourFieldName") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
It seems a little bit complex, but it's flexible, because you can set any property of the control with the eval() function :
<asp:TemplateField>
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server"
NavigateUrl='<%# "ShowDetails.aspx?id="+Eval("Id") %>'
Text='<%# Eval("Text", "{0}") %>'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
Eval is used to bind to an UI item that is setup to be read-only (eg: a label or a read-only text box), i.e., Eval is used for one way binding - for reading from a database into a UI field.
It is generally used for late-bound data (not known from start) and usually bound to the smallest part of the data-bound control that contains a whole record. The Eval method takes the name of a data field and returns a string containing the value of that field from the current record in the data source. You can supply an optional second parameter to specify a format for the returned string. The string format parameter uses the syntax defined for the Format method of the String class.
IrishChieftain didn't really address the question, so here's my take:
eval() is supposed to be used for data that is not known at run time. Whether that be user input (dangerous) or other sources.

Resources