Wednesday, March 28, 2012

String was not recognized as a valid DateTime Error

I am extracting a DateTime string from a SQL table, and I want to format it. However, when I try to convert the string to a datetime, I get the error:
"String was not recognized as a valid DateTime"
Here is my code:
private string dateFormatting(String theDate)
{
DateTime strDate;
strDate = Convert.ToDateTime(theDate);
strDate = strDate.ToString("M");
return strDate;
}
theDate has the value of "5/11/2005 5:10:25 PM". If I copy-and-paste the string into the code (instead of "theDate"), it compiles properly. However, leaving it in a variable always return the error above.
What might cause this problem?

Thanks

I'm having the same trouble... Can it be a bug?
Regards,
Luis Sim?es


Your variable "strDate" is a DateTime type. You are trying to fill a DateTime variable with a string:

strDate = strDate.ToString("M");
try this:

privatestring dateFormatting(string theDate)

{

DateTime strDate;

strDate = DateTime.Parse(theDate);

string stringDate = strDate.ToString("M");

return stringDate;

}



this may sound like a dumb question, but are you absoulutly sure that the date you paste in is the same date that your code is reciveing, as in have you place a breakpoint in and checked this for sure. One possibility is that the source of your date string has a different culture to the server running the convert.

On a side note, if your function only formats a date then why not only have yout functon accept a date rather then a string? In other words what context is this function performing its role of the conversion, is it from a databind or from a database object?


I'm not sure if it's gonna change anything but.. try to convert like this..
Convert.ToShortDateTime(variable);
anyway... I guess it changes nothing... except that the date will be returned different..
I am not sure if it's gonna change anything.. but.. try to use like this:

Convert.ToShortDateTime(var_name);
I don't know if it will make any difference at all.. I think it wont accutally... I mean.. about this error...

andrew_K wrote:

this may sound like a dumb question, but are you absoulutly sure that the date you paste in is the same date that your code is reciveing, as in have you place a breakpoint in and checked this for sure. One possibility is that the source of your date string has a different culture to the server running the convert.

On a side note, if your function only formats a date then why not only have yout functon accept a date rather then a string? In other words what context is this function performing its role of the conversion, is it from a databind or from a database object?

I am positive that the date I pasted is the same as the date that my code is receiving.

I have the function accept a string because it is called from a databind. I have tried converting the date before passing it to the function, and it returns the same error. I'd post the code, but I am at home now *-)


DevSlick wrote:

Your variable "strDate" is a DateTime type. You are trying to fill a DateTime variable with a string:

strDate = strDate.ToString("M");
try this:

privatestring dateFormatting(string theDate)

{

DateTime strDate;

strDate = DateTime.Parse(theDate);

string stringDate = strDate.ToString("M");

return stringDate;

}



It still returned the same error onstrDate = DateTime.Parse(theDate);
This is very weird.
Weird, it worked for me
This is driving me crazy. Here's the entire code. Maybe the problem is somewhere else. The stored procedure returns two values, the date and an id. Any help would be greatly appreciated!

protected void Page_Load(object sender, EventArgs e)
{
SqlDataSource dsAdmin = new SqlDataSource();
dsAdmin.ConnectionString = "Data Source=(local);Initial Catalog=birdhair;Integrated Security=True";
dsAdmin.SelectCommand = "usp_Admin_ArchiveList";
dsAdmin.SelectCommandType = SqlDataSourceCommandType.StoredProcedure;
Parameter sParam1 = new Parameter();
sParam1.Name = "month";
sParam1.Type = TypeCode.Int32;
sParam1.DefaultValue = DateTime.Now.Month.ToString();
dsAdmin.SelectParameters.Add(sParam1);
Parameter sParam2 = new Parameter();
sParam2.Name = "Year";
sParam2.Type = TypeCode.Int32;
sParam2.DefaultValue = DateTime.Now.Year.ToString();
dsAdmin.SelectParameters.Add(sParam2);
DataGrid gridYear = new DataGrid();
gridYear.AutoGenerateColumns = false;
gridYear.DataSource = dsAdmin;
placeholder.Controls.Add(gridYear);
HyperLinkColumn column = new HyperLinkColumn();
column.DataNavigateUrlField = "LogID";
column.DataNavigateUrlFormatString = "edit.aspx?LogID={0}";
column.DataTextField = "LogTimeStamp";
column.DataTextFormatString = dateFormatting("{0}");
gridYear.Columns.Add(column);
gridYear.DataBind();
}
private string dateFormatting(String theDate)
{
DateTime strDate;
String stringDate;
strDate = DateTime.Parse(theDate);
stringDate = strDate.ToString("M");
return stringDate;
}

0 comments:

Post a Comment