Click here to Skip to main content
15,881,898 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i want to compare grid view column date with system date then enable disable link button? how its possible i m trying some butt error occured.


error is ({"The string was not recognized as a valid DateTime. There is an unknown word starting at index 0."})

What I have tried:

C#
LinkButton lnk2 = (LinkButton)e.Row.FindControl("lnkSelect");
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                string dtimeString = Convert.ToDateTime(e.Row.Cells[2].Text).ToString("yy/MM/dd");

                DateTime dtime = Convert.ToDateTime(dtimeString);

                DateTime nowtime = Convert.ToDateTime(DateTime.Now.ToString("yy/MM/dd"));
                if (dtime == nowtime)
                {
                    lnk2.Enabled = true;
                    //change the column backcolor 
                  //  e.Row.Cells[0].BackColor = System.Drawing.Color.Yellow;

                    //change the row backcolor
                    //  e.Row.BackColor = System.Drawing.Color.Yellow;
                }
                if (dtime > nowtime)
                {
                    lnk2.Enabled = false;
                    e.Row.Cells[0].BackColor = System.Drawing.Color.Red;

                    // e.Row.BackColor = System.Drawing.Color.Red;
                }
            }
Posted
Updated 10-May-17 16:35pm
v2
Comments
[no name] 9-May-17 16:02pm    
The error message tells you what the problem is.
shreyal acharya 10-May-17 1:52am    
also you compare your data in query.
Richard MacCutchan 10-May-17 4:16am    
What is the point of these lines:

1. string dtimeString = Convert.ToDateTime(e.Row.Cells[2].Text).ToString("yy/MM/dd");

2. DateTime dtime = Convert.ToDateTime(dtimeString);

3. DateTime nowtime = Convert.ToDateTime(DateTime.Now.ToString("yy/MM/dd"));

1. You take a string (presumably a date), convert it to a DateTime value, and then convert that back to a string.

2. You take the final string from 1 and convert it back to a DateTime.

3. You the get the current time (which is a DateTime), convert it to a string, and then immediately convert it back to a DateTime.

Do not use strings when comparing, or otherwise manipulating, DateTime values. Use the actual numeric values.
Suvendu Shekhar Giri 10-May-17 22:43pm    
my virtual 5!

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900