Saturday, May 19, 2012

String was not recognized as a valid DateTime C#


Im stuck here why i run my project in local properly but when i upload to server got this error.
i passing like this.

///////////////////in js///////////////////
'&dFrom='+Ext.getCmp('txtDateFrom').getValue().dateFormat('m/d/Y')
'dTo=' + Ext.getCmp('txtDateTo').getValue().dateFormat('m/d/Y')

///////////////////in c/////////////////////
 DateTime dFrom;
 DateTime dTo;
 dFrom = Convert.ToDateTime(Request.Params["dFrom"]);
 dTo = Convert.ToDateTime(Request.Params["dTo"]);


Solution:
1. Make date string format as looks like "dd/MMM/yyyy"


this.Text="22/may/2012";
 DateTime dFrom;
 dFrom = Convert.ToDateTime(this.Text);

2.May be  a culture problem
try to use ParseExact
DateTime.ParseExact(Request.Params["dFrom"], "MM/dd/yyyy", CultureInfo.InvariantCulture)
3.Try other sample too
  Thread.CurrentThread.CurrentCulture = new CultureInfo("en-GB"); //dd/MM/yyyy

    this.Text="22/11/2009";

    DateTime date = DateTime.Parse(this.Text);
For more details go here:

No comments: