c# - Solved - Date Time FormatException was unhandled by user code -


this c# web service trying right.

arg1 drop down menu user can select, yearly monthly quarterly. arg2 date input. arg3 date chose plus either year, month or 3 months added date.

so far have put together:

public serviceresult callservice(string arg1, string arg2 = "", string arg3 = "") {      system.threading.thread.currentthread.currentculture = new cultureinfo("en-gb");       //write code here      if (arg1 == "1 - annually")      {           datetime thisdate1 = convert.todatetime(arg2);          thisdate1.addyears(1);          arg3 = convert.tostring(thisdate1);       }       return new echoinputhandler().echoinput(arg1, arg2, arg3); } 

i getting error:

formatexception unhandled user code exception of type 'system.formatexception' occurred in mscor.lib not handled in user code additional information: string not recognized valid datetime. 

could educate me on why happening?

update

i have solved it! code looks now:

public serviceresult callservice(string arg1, string arg2 = "", string arg3 = "")      {         if (arg1 == "1 - annually")         {             datetime arg2date = convert.todatetime(arg2);             arg2date = arg2date.addyears(1);             arg3 = arg2date.tostring("dd/mm/yyyy");         }      } 

thank advice , help

basically tells arg2 not recognized valid datetime.

a few enhancements make:

1) make sure dont call convert.todatetime on empty string (use .parse instead, convert.todatetime uses internally)

2) make sure validate arg2 - should know input format.

3) if know input format. use parseexact

4) make sure use .parse og .parseexact try/catch handling, , return useful client on catch exception


Comments

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

python - build a suggestions list using fuzzywuzzy -