views:

393

answers:

1

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?

+1  A: 

Is it possible that you've changed the interface of your method slightly and not updated the web reference? It looks like you've renamed the From and To parameters but are still using an out of date proxy class.

Try updating the web reference.

David M
Solved! I'd never have spotted that myself (this is the first time I've worked with creation of a webservice). I suspect this will also preempt other questions in the future ;)
Matt Sach
The parameters are serialized into the request XML and then fail to deserialize because of the name mismatch, so the web services framework helpfully inserts default(DateTime) instead. Glad that was all it was...
David M