Domanda

My app pulls an object via a web service call, puts it in a typed dataset and sends the DataSet.GetXml() to a stored procedure for insert/update on the database.

The problem I'm facing is with two properties of the object : StartTime/EndTime. The web service sends these in UTC format. Eg. sample StartTime-> "2012-11-06T05:00:00Z" The DataSet.GetXml() attempts to convert this UTC value into local time, and my app server is in EST. The resultant value should be reported as "2012-11-06T00:00:00-05:00" but instead it is "2012-11-06T05:00:00-05:00". The offset value is being added but the time component is not changed.

Is there something incorrect with my understanding here? I'm finding it hard to digest that the GetXml() method could have such a bug, and I haven't found anyone else here complain of a similar problem yet.

Here's a stripped down version of the code:

public void SaveOrder(int intOrderID)
{
        OrderDataSet objOrderDS = null;
        OrderDataSet.OrdersRow objOrderRow = null;
        ExternHandler handler = null;
        Order objOrder;

        Order objOrder = handler.GetOrder(intOrderID);

        objOrderRow = objOrderDS.Orders.NewOrdersRow();
        objOrderRow.OrderID = objOrder.OrderID;
        objOrderRow.StartTime = objOrder.StartTime;
        objOrderRow.EndTime = objOrder.EndTime;
        objOrderDS.Orders.AddOrdersRow(objOrderRow);

        if (objOrderDS.Orders.Rows.Count > 0)
        {
            objOrderDS.Namespace = string.Empty;
            objMappingObjects.Add(new MappingObject("Table", "Orders"));
            objSqlParams.Add(new SqlParameter("@pOrdersXml", objOrderDS.GetXml()));
            objOrderDS.Clear();
            objOrderDS.Merge(SqlHelper.ExecuteDataset(ConfigConnectionDB.Trim(), CommandType.StoredProcedure, "usp_InsertOrderMetaData", objMappingObjects.ToArray(), objSqlParams.ToArray()));
        }
}
È stato utile?

Soluzione

OK, I found why the UTC-time was being converted to "incorrect local time". The fields 'Startime'/'EndTime' in the typed dataset are of type 'DateTime'. There is a property associated with DataColumn of DateTime type called 'DateTimeMode' which is set to 'UnspecifiedLocal' by default. http://msdn.microsoft.com/en-us/library/system.data.datasetdatetime.aspx

Basically, this option adds the offset to the datetime value without converting it. As a test, I changed the 'DateTimeMode' to 'Utc' and retried. The XML received from DataSet.GetXml() was able to convert the value correctly: "2012-11-06T05:00:00Z"

As a solution to my problem, I'm keeping the property to 'UnspecifiedLocal' as it is. Instead, while adding the value to the dataset I convert it to local time.

    objOrderRow.EndTime = objOrder.EndTime.ToLocalTime();

(Note that this works for me because my app server and database server are in the same timezone and conversion to UTC is uncomplicated.)

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top