c# - Make input to Web Service field optional not required -
i created sample webservice ask user information , return in json form information required. have 2 issues here
- i don't want make required user enter bot new dates , renewal dates, want make 1 of them required while other 1 optional
- if both dates, new , renewal, entered user in 1 json not sure how append both lists together.
please let me know if need additional information, here code webservice
namespace webservicetest { /// <summary> /// summary description webserviceprueba /// </summary> [webservice(namespace = "http://tempuri.org/")] [webservicebinding(conformsto = wsiprofiles.basicprofile1_1)] [system.componentmodel.toolboxitem(false)] // allow web service called script, using asp.net ajax, uncomment 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 username"; } else if (user == "prueba" && userid != "prueba") { return "please check userid"; } else { return "please check both username , userid"; } } } }
here error when run webservice , don't enter 1 of dates (i.e enter newservicedate
, newservicesto
not renewservicedate
, renewserviceto
or other way around
system.argumentexception: cannot convert system.datetime. parameter name: type ---> system.formatexception: string not recognized valid datetime. @ system.datetimeparse.parse(string s, datetimeformatinfo dtfi, datetimestyles styles) @ system.convert.todatetime(string value, iformatprovider provider) @ system.string.system.iconvertible.todatetime(iformatprovider provider) @ system.convert.changetype(object value, type conversiontype, iformatprovider provider) @ system.web.services.protocols.scalarformatter.fromstring(string value, type type) --- end of inner exception stack trace --- @ system.web.services.protocols.scalarformatter.fromstring(string value, type type) @ system.web.services.protocols.valuecollectionparameterreader.read(namevaluecollection collection) @ system.web.services.protocols.htmlformparameterreader.read(httprequest request) @ system.web.services.protocols.httpserverprotocol.readparameters() @ system.web.services.protocols.webservicehandler.coreprocessrequest()
-----------------------------update------------------------
@jianping liu when declare nullable parameters in function no longer able test webservice, `test
the test form available methods primitive types parameters.` here how declaring it
[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)
you can use nullable variable 4 datetime
private datetime? newservicedate { get; set; } private datetime? newservicesto { get; set; } private datetime? renewservicedate { get; set; } private datetime? renewserviceto { get; set; }
and use newservicedate.hasvalue check if contains value.
Comments
Post a Comment