I created a sample webservice that will ask the user for some information and return in JSON form all the information required. I have two issues here
- I don't want to make it required that the user enter bot the new dates and renewal dates, I want to make so only one of them is required while the other one is optional
- If both dates, new and renewal, are entered then the user will get it all in one JSON but I am not sure how to append both lists together.
Please let me know if you need additional information, here is my code for the webservice
namespace WebServiceTest
{
/// <summary>
/// Summary description for WebServicePrueba
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class WebServicePrueba : System.Web.Services.WebService
{
private string User { get; set; }
private string UserID { get; set; }
private DateTime NewServiceDate { get; set; }
private DateTime NewServicesTo { get; set; }
private DateTime RenewServiceDate { get; set; }
private DateTime RenewServiceTo { get; set; }
[System.Web.Services.WebMethod(Description = "Get Services Received", EnableSession = true)]
public string getSeviceInformation(string name, string userId, DateTime newServiceDate, DateTime newServicesTo, DateTime renewServiceDate, DateTime renewServiceTo)
{
User = name;
UserID = userId;
NewServiceDate = newServiceDate;
NewServicesTo = newServicesTo;
RenewServiceDate = renewServiceDate;
RenewServiceTo = renewServiceTo;
if (User == "prueba" && UserID == "prueba")
{
if (renewServiceDate == DateTime.MinValue || renewServiceTo == DateTime.MinValue)
{
ArrayList list = this.participant.getAddedTRCTest(newServiceDate, newServicesTo);
return JsonConvert.SerializeObject(list);
}
else if (newServiceDate == DateTime.MinValue || newServicesTo == DateTime.MinValue)
{
ArrayList list = this.participant.getModifiedTRCTest(renewServiceDate, renewServiceTo);
return JsonConvert.SerializeObject(list);
}
else
{
ArrayList list = this.participant.getAddedTRCTest(newServiceDate, newServicesTo);
ArrayList list2 = this.participant.getModifiedTRCTest(renewServiceDate, renewServiceTo);
return JsonConvert.SerializeObject(list);
}
}
else if (User != "prueba" && UserID == "prueba")
{
return "Please check your username";
}
else if (User == "prueba" && UserID != "prueba")
{
return "Please check your UserID";
}
else
{
return "Please check both your username and userID";
}
}
}
}
Here is the error I get when I run the webservice and don't enter one of the dates (i.e I enter newServiceDate
and newServicesTo
but not renewServiceDate
and renewServiceTo
or the other way around
System.ArgumentException: Cannot convert to System.DateTime.
Parameter name: type ---> System.FormatException: String was not recognized as a valid DateTime.
at System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles)
at System.Convert.ToDateTime(String value, IFormatProvider provider)
at System.String.System.IConvertible.ToDateTime(IFormatProvider provider)
at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)
at System.Web.Services.Protocols.ScalarFormatter.FromString(String value, Type type)
--- End of inner exception stack trace ---
at System.Web.Services.Protocols.ScalarFormatter.FromString(String value, Type type)
at System.Web.Services.Protocols.ValueCollectionParameterReader.Read(NameValueCollection collection)
at System.Web.Services.Protocols.HtmlFormParameterReader.Read(HttpRequest request)
at System.Web.Services.Protocols.HttpServerProtocol.ReadParameters()
at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()