Wednesday, March 07, 2007

Often when creating SQL Compact applications,  I need to record the current date and time into the database.   This can be a bit of an issue if the clock of the mobile device is out of sync with the main database server.

So every 10th time my mobile apps sync with the base database server (its merge replication I usually use), I call a web-service to get the time at the server.  I use a web-service as I don't need to open up any further ports ontop of what I need for merge replication over http/https.

My web-service is very simple and comprises of just the following code -

 

<%@ WebService Language="C#" Class="TimeService" %>

using System;
using System.Web.Services;

public class TimeService : WebService
{
[WebMethod]
public string GetDateTimeString()
{
DateTime utime = DateTime.Now.ToUniversalTime();
string ret=""
ret+=utime.Year+","
ret+=utime.Month+","
ret+=utime.Day+","
ret+=utime.Hour+","
ret+=utime.Minute+","
ret+=utime.Second;

return ret;

}
}

 

I place this web-service in the same IIS virtual share as the sql compact server tools,  to ease setup.

 

The web-method GetDateTimeString() returns a string with the UTC date and time.   I return as a string to avoid any timezone conversion issues.

 

Tomorrow, I'll let you know how I consume the web-service and set the clock of the mobile device...

Comments are closed.

Theme design by Jelle Druyts

Pick a theme: