(SOLVED) Error: The string was not recognized as a valid DateTime. There is an unknown word starting at index 0.
-
Greetings again dear experts, Back for more your great assistance.
// check to be sure the date selected is not ouside acceptable boudaries
DateTime runDate = DateTime.Parse(rsRunDate.Text);When I run my app, I get the following: The string was not recognized as a valid DateTime. There is an unknown word starting at index 0. This error points to the line code I posted. I tried changing the code to the following:
DateTime runDate = DateTime.ParseExact(srRunDate.Text,"dd/MM/yyyy", null);
However, I still get same error. I tried the following:
DateTime _runDate;
string runDate = "";_runDate = DateTime.Parse(srRunDate.Text);
runDate = _runDate.ToString("dd/MMM/yyyy");But then, the following produces an error:
if (runDate.Date > DateTime.Now.Date)...
Now, I get the following error:
The name 'runDate' does not exist in the current context
I ran the code in debug mode, it shows runDate with the following value: {1/1/0001 12:00:00 AM} System.DateTime I am out of ideas. Any ideas how to fix this?
-
Greetings again dear experts, Back for more your great assistance.
// check to be sure the date selected is not ouside acceptable boudaries
DateTime runDate = DateTime.Parse(rsRunDate.Text);When I run my app, I get the following: The string was not recognized as a valid DateTime. There is an unknown word starting at index 0. This error points to the line code I posted. I tried changing the code to the following:
DateTime runDate = DateTime.ParseExact(srRunDate.Text,"dd/MM/yyyy", null);
However, I still get same error. I tried the following:
DateTime _runDate;
string runDate = "";_runDate = DateTime.Parse(srRunDate.Text);
runDate = _runDate.ToString("dd/MMM/yyyy");But then, the following produces an error:
if (runDate.Date > DateTime.Now.Date)...
Now, I get the following error:
The name 'runDate' does not exist in the current context
I ran the code in debug mode, it shows runDate with the following value: {1/1/0001 12:00:00 AM} System.DateTime I am out of ideas. Any ideas how to fix this?
You shouldn't be looking at the variable that hold the parsed value. The text hasn't been parsed, so what's the point? You should be looking at the content of
srRunDate.Text
. For example, trying to use Parse on an empty string will throw an exception. You should change your code to use TryParse instead. At least then, you'll get a return value of true or false denoting if the parse operation worked or not.Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
Dave Kreskowiak -
You shouldn't be looking at the variable that hold the parsed value. The text hasn't been parsed, so what's the point? You should be looking at the content of
srRunDate.Text
. For example, trying to use Parse on an empty string will throw an exception. You should change your code to use TryParse instead. At least then, you'll get a return value of true or false denoting if the parse operation worked or not.Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
Dave Kreskowiak -
Greetings again dear experts, Back for more your great assistance.
// check to be sure the date selected is not ouside acceptable boudaries
DateTime runDate = DateTime.Parse(rsRunDate.Text);When I run my app, I get the following: The string was not recognized as a valid DateTime. There is an unknown word starting at index 0. This error points to the line code I posted. I tried changing the code to the following:
DateTime runDate = DateTime.ParseExact(srRunDate.Text,"dd/MM/yyyy", null);
However, I still get same error. I tried the following:
DateTime _runDate;
string runDate = "";_runDate = DateTime.Parse(srRunDate.Text);
runDate = _runDate.ToString("dd/MMM/yyyy");But then, the following produces an error:
if (runDate.Date > DateTime.Now.Date)...
Now, I get the following error:
The name 'runDate' does not exist in the current context
I ran the code in debug mode, it shows runDate with the following value: {1/1/0001 12:00:00 AM} System.DateTime I am out of ideas. Any ideas how to fix this?