Reputation: 1543
I wrote an iOS app that uses a Post HTTP method via a URL and two parameters that checks the validity of the two parameters against records in a table in a mySQL database and returns a JSON response.
If the parameters are valid the response is {"success":1}
, if not the response is {"success":0,"error_message":"The parameters are invalid."}
. The web service that does this was done in PHP. All works fine.
However, now I want to do the same thing against an MSSQL database using a WCF web service. I did some research and I actually have it working whereby I am using a stored procedure in the web service and the response is returned in JSON format.
I am new to WCF web services and C#.NET and I am trying to figure out how to have the response not return the results from the stored procedure but just a JSON response indicating if there are results and another response if there are no results. The code for my test WCF web service (Service1.svc.cs) is below and this brings back all the data in JSON, but all I want is if there is data in the result then respond with {"success":1}
, and if no data in the result, then respond with {"success":0,"error_message":"The parameters are invalid."}
. I am not including the IService1.cs, the web.config or the CustomerOrdersOrders.cs code since I'm not sure they are required to solve my problem. If they are, let me know and I will post them.
Any help is appreciated.
public List<CustomerOrdersOrders> GetCustomerOrdersOrders(string customerID)
{
NorthwindDataContext dc = new NorthwindDataContext();
List<CustomerOrdersOrders> results = new List<CustomerOrdersOrders>();
System.Globalization.CultureInfo ci = System.Globalization.CultureInfo.GetCultureInfo("en-US");
foreach (CustOrdersOrdersResult oneOrder in dc.CustOrdersOrders(customerID))
{
results.Add(new CustomerOrdersOrders()
{
OrderID = oneOrder.OrderID,
OrderDate = (oneOrder.OrderDate == null) ? "" : oneOrder.OrderDate.Value.ToString("d", ci),
RequiredDate = (oneOrder.RequiredDate == null) ? "" : oneOrder.RequiredDate.Value.ToString("d", ci),
ShippedDate = (oneOrder.ShippedDate == null) ? "" : oneOrder.ShippedDate.Value.ToString("d", ci)
});
}
return results;
}
Upvotes: 0
Views: 1082
Reputation: 9499
If you just want to test the presence of data, you have 2 options: either create a stored procedure which takes a customerid and returns a boolean, if data exists. OR, use a non-stored proc approach like this:
public bool DataExists(string customerId)
{
using (var dc = new NorthwindDataContext())
{
return dc.CustomerOrders.Any(co => co.CustomerID == customerId);
// or use the boolean stored procedure
}
}
once you have the above boolean method, you can then have your web method (operation contract in WCF) return the right JSON.
You can lookup WCF with JSON response as to how to this:
e.g.
http://www.wcftutorial.net/How_To_JSON_Using_WCF.aspx How do I return clean JSON from a WCF Service?
most of the times, you need to attribute your operation contract with:
[WebGet(ResponseFormat = WebMessageFormat.Json)]
and have a Data contract returned which maps to the JSON response you need.
Upvotes: 1