I have a VS2008 solution containing two projects, a web service (the actual work I'm doing) and a web application (that I'm using to test the service). The latter has a "web reference" to the former. It is also set as the startup project, so that when I start debugging VS sets up two ports on localhost, one for the app (which runs its default.aspx) and one for the service.
The service code in events.asmx.cs has the following:
public class events : System.Web.Services.WebService
{
/* various object classes specific to service */
[WebMethod]
public List<EventItem> GetEvents(DateTime From, DateTime To, string[] EventType)
{
EventsDataContext dc = new EventsDataContext();
List<EventsMain> events = dc.EventsMains
.Where(ev => ev.EventStartDate >= From
&& ev.EventEndDate < To
&& ev.Live == true
&& ev.IsOnWebsite == true
)
.ToList();
}
}
It is intentional that the last parameter (EventType
) is not currently used.
The EventsMain class is from the data context, and is representing a table that has a whole load of fields, of which in this case the LINQ query is assessing the values of a couple of BIT fields and two DATETIME fields. It's supposed to return a list of events based on the provided criteria.
In the web application, I have this code in the default.aspx code-behind to call the above method:
protected void Page_Load(object sender, EventArgs e)
{
events eventService = new events();
DateTime from = new DateTime(2009, 1, 1, 10, 0, 0);
DateTime to = new DateTime(2009, 2, 1, 10, 0, 0);
EventItem[] eventList = eventService.GetEvents(from, to, new string[] { });
}
That last line (which calls the webservice method) fails, with the following error:
System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.Data.SqlTypes.SqlTypeException: SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM.
When debugging, at the point just before the webservice method call, the DateTime variables look fine. Stepping into the method, even before the LINQ statement, the From
and To
parameters appear to have ended up as the C# DateTime.MinValue
, which obviously is well below the SQL Server minimum datetime value.
Changing the from
and to
variables to other dates before calling the method appears to have no effect.
So the question is: What am I doing wrong currently, and what should I be doing for these dates to cross the webservice interface correctly?