views:

428

answers:

1

Was going to call this "Eval won't give me the Time of Day," but thought that too cute.

I'm kind of having the opposite problem from the suggested questions I see for my title, so here goes:

Visual Studio 2008 Pro, SQL Express 2008, Vista. Web Project, where I'm opening records from an Event Table with JOINed info from a Facilities table.

I have a SQL DateTime field in a SQL file that is completely filled out: e.g., 4/30/2009 6:30 PM.

My field in the ListView is straight <%# Eval("EventDate") %>, and my trace, as far as I have been able to tell from watching the Autos window through retrieval from the SQLDataSource, survives intact with the 6:30 PM time of day.

The only instruction, to my knowledge, that comes after the DataSource is populated, is the Eval itself.

Yet, at render time, that datetime comes out on the page as 4/30/2009 12:00:00 AM.

My strong belief, therefore, is that I need additional fu to parse my Eval statement to get the same time I put into the field. I am not yet adept at this, so suggestions would be welcome. Thank you in advance.

Edit: supplying code per Jose.

DataSource:

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:MeetingsConnectionString %>" onselecting="SqlDataSource1_Selecting" SelectCommand="SELECT Events.ID, Events.FacilityID, Events.Room, Events.EventDate, Events.Speaker, Events.Topic, Facilities.ID AS Expr1, Facilities.FacilityName, Facilities.FacilityAddress, Facilities.FacilityCity, Facilities.FacilityState, Facilities.FacilityZip, Facilities.FacilityLat, Facilities.FacilityLong FROM Events INNER JOIN Facilities ON Events.FacilityID = Facilities.ID WHERE (Events.EventDate BETWEEN @EventDate AND @EventDate2) ORDER BY Events.EventDate" ProviderName="System.Data.SqlClient"> <SelectParameters> <asp:Parameter Name="EventDate" /> <asp:Parameter Name="EventDate2" /> </SelectParameters> </asp:SqlDataSource>

ListView:

<asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource1" DataKeyNames="ID,Expr1"> <AlternatingItemTemplate> When:<asp:Label ID="EventDateLabel" runat="server" Text='<%# Eval("EventDate") %>' />

... <ItemTemplate> When: <asp:Label ID="EventDateLabel" runat="server" Text='<%# Eval("EventDate") %>' /><br />

Edit 2: I worked around the problem by separating EventDate into EventDate (a Date) and EventTime (a Time). I now get the proper values, and have moved on to conversion of the time value from military to civilian time (i.e., 6:30pm) in the SQL query using a CONVERT or CAST. Thanks for all of your responses.

+1  A: 

Sql server got a new type, date, that will do that if you cast a datetime to datelike so: select convert(datetime, convert (date, '4/30/2009 6:30 PM'))

Maby you specified the incorrect type somewhere?

Bård
So 'date' does what I'm expecting datetime to do? If so, why isn't datetime coming out of Eval as a shortDateString?
John Dunagan
date will produce the result that you describe. I cannot see anything wrong with you code tho. I hoped to find something fishy there.
Bård
Would it work if I turned the SQL DB field into a Date instead of a DateTime?
John Dunagan
No, that didn't. Date comes back with the short date string, and I can't add any time of day to it.
John Dunagan