Vigs
Vigs

Reputation: 351

Query Database from Mobile Service with Respect to Current DateTime

I am successfully querying an azure sql database from an azure mobile service with the following line of code:

var targetUri = new Uri("https://studytoolmobile.azure-mobile.net/tables/TodoItem?$filter=(complete%20eq%20true)");

I figured I could modify this to fit my specific objective. My objective is to query the database to return the record whose value in the Notification1 column matches the current Datetime:

DateTime currentTime = DateTime.Now;
currentTime = currentTime.AddMilliseconds(-currentTime.Millisecond);
string timeString = currentTime.ToString();

var targetUri = new Uri("https://studytoolmobile.azure-mobile.net/tables/TodoItem?$filter=(Notification1%20eq%20" + timeString + ")");

The problem is that it is giving me an error:

'The remote server returned an error: (400) Bad Request.'.

This leads me to believe it is caused by the new Uri I am using.

Does anyone know what the correct uri to accomplish this would be? Or is there a different cause to my error? Thank you.

Upvotes: 0

Views: 64

Answers (1)

mathewc
mathewc

Reputation: 13568

You need to use the correct OData DateTime literal syntax. For example:

$filter=(Notification1 eq datetime'2010-01-25T02:13:40.1374695Z')

You can get the required ISO format value from your DateTime instance by using currentTime.ToString("o").

Upvotes: 1

Related Questions