Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
I have been going through the motions and still cannot seem to get this right. I have text boxes in a grid view that are populated from an access database. They are all date time values. In the back-end code, I am trying to loop through all those values and then apply conditional formatting. For some unknown reason I am unable to get the value from those text boxes in the grid view and when I do, they are seen by the application as string as opposed to date time. Converting is futile as the same error, "String was not recognized as a valid DateTime." keeps popping up.
 
Any ideas on how to get values from a grid view text box, and then convert them from a string to a date time format?
 
Here is the code thus far.
var myLabel2 = (TextBox)GridView1.Rows[p].Cells[0].FindControl("Label2");
            var myLabel4 = (TextBox)GridView1.Rows[p].Cells[0].FindControl("Label4");
 
            var myLabel7 = (Label)GridView1.Rows[p].Cells[0].FindControl("Label7");
            var myLabel9 = (Label)GridView1.Rows[p].Cells[0].FindControl("Label9");
 
            string dateString = myLabel2.Text;
 
            CultureInfo provider = CultureInfo.InvariantCulture;
            string format = "dd/MMM/yyyy h:mm tt zzz";
            DateTime result = DateTime.ParseExact(dateString, format, provider);
 
            DateTime start = Convert.ToDateTime(myLabel2.Text).Date;
            DateTime now = DateTime.Now.Date;
            DateTime end = Convert.ToDateTime(myLabel4.Text).Date;
 
            if (now >= start && now <= end)
            {
                myLabel2.BackColor = Color.Chartreuse;
                myLabel4.BackColor = Color.Chartreuse;
                myLabel7.BackColor = Color.Chartreuse;
                myLabel9.BackColor = Color.Chartreuse;
            }
            else
            {
                myLabel2.BackColor = Color.White;
                myLabel4.BackColor = Color.White;
                myLabel7.BackColor = Color.White;
                myLabel9.BackColor = Color.White;
            }
Thanks in advance
Posted 8-Apr-12 4:07am
Edited 8-Apr-12 6:40am
v3
Comments
Reza Ahmadi at 8-Apr-12 11:04am
   
I could not understand yet, could you get the value of the Text property in your code behind? and if so, have you debugged to realize what is exactly in your labels' Text property?
Mark Nischalke at 8-Apr-12 11:14am
   
Have you debugged? Are you sure of the value in your textboxes? The error message is very clear.
VirgilPlatoKeats at 8-Apr-12 12:31pm
   
The value of the textboxes are there, but the format which it perceives is string. Is there a simple method to get a value from a gridview textbox and then convert it from string to datetime?
Reza Ahmadi at 8-Apr-12 12:33pm
   
DateTime.Parse can be used to convert a date string to date.
VirgilPlatoKeats at 8-Apr-12 12:39pm
   
I have tried various forms of Parse to convert, yet the same error keeps popping up...
Wes Aday at 8-Apr-12 12:44pm
   
Well what is the string you are trying to convert?
VirgilPlatoKeats at 8-Apr-12 13:05pm
   
I have populated a series of gridview label with dates, ie. "2012/04/03 12:00:00 AM" from an access database. Could it be a problem with the actual access field?
Wes Aday at 8-Apr-12 14:11pm
   
Try
 
DateTime result = new DateTime(myLabel2.Text);
 
and see what happens
VirgilPlatoKeats at 8-Apr-12 15:54pm
   
The best overloaded method match for 'System.DateTime.DateTime(long)' has some invalid arguments
 
Different error, in this case which arguments would you need to validate this method?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Check your language settings. The standard English settings are mm/dd/yyyy and you trying to convert a date in the format yyyy/mm/dd.
  Permalink  
v2
Comments
VirgilPlatoKeats at 9-Apr-12 11:59am
   
DateTime date = DateTime.ParseExact(myLabel2.Text, "mm/dd/yyyy", null);
 
Would this work to convert it? It still gives the same error? Is there an alternative method to Parse?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Use Convert.ToDateTime("SomeDateString")
 
string dateString = "2012/04/03 12:00:00 AM";
DateTime _dt = Convert.ToDateTime(dateString);
  Permalink  
Comments
VirgilPlatoKeats at 8-Apr-12 13:54pm
   
Still no solution, "String was not recognized as a valid DateTime."... The problem is getting the value from the label that is loaded into the gridview... I have tried the following...
 
string dateString = myLabel2.Text;
DateTime _dt = Convert.ToDateTime(dateString);
Ashish Bhujbal at 9-Apr-12 8:13am
   
1> Before using your above code, make sure that myLabel2.Text in not a empty string.
2> if both label giving you proper values then please revert back with your date format, which is at your labels.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

I'm not sure what your problem is. Try this, it works:
 
        DateTime t1 = DateTime.Now.AddDays(1).Date;
            DateTime t2 = DateTime.Now.Date;
            DateTime t3 = DateTime.Now.AddDays(-1).Date;
            if (t1 > t2 && t2 > t3)
            {
                Console.WriteLine("It worked");
            }
 
I would think you did not check the dates to see if they were in the format you expected. Could be you were assuming some other format.
  Permalink  
Comments
VirgilPlatoKeats at 8-Apr-12 15:59pm
   
This situation is fine if you are not working with values from labels within a gridview. This case, it works, but when you combine conditional formatting with a conversion of data from a string to datetime, certain errors arise.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 363
1 OriginalGriff 354
2 George Jonsson 248
3 Shemeemsha RA 148
4 Animesh Datta 130
0 OriginalGriff 6,179
1 Sergey Alexandrovich Kryukov 5,616
2 CPallini 4,770
3 George Jonsson 3,400
4 Gihan Liyanage 2,522


Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 8 Apr 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100