Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a gridview with containing to date columns from database. I bind them successfully with database and i obtain the dates in dd/MM/yyyy format but when i clicked on auto generated Edit Button then i obtain the dates in dd/MM/yyyy format but when i clicked on update button then it gives the error that
String was not recognized as a valid DateTime.

In gridview row i have two different date such as follows

Form Date - 11/09/2014
To Date - 15/11/2014

I got the above mentioned error for to date conversion while updating date

Here is my code for GridView

XML
<asp:TemplateField HeaderText="From Date">
        <EditItemTemplate>
            <asp:TextBox ID="from_date" runat="server" Text='<%# Bind("from_date","{0:dd/MM/yyyy}") %>'></asp:TextBox>
        </EditItemTemplate>
        <ItemTemplate>
            <asp:Label ID="Label2" runat="server" Text='<%# Bind("from_date","{0:dd/MM/yyyy}") %>'></asp:Label>
        </ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="To Date">
        <EditItemTemplate>
            <asp:TextBox ID="to_date" runat="server" Text='<%# Bind("to_date","{0:dd/MM/yyyy}") %>'></asp:TextBox>
        </EditItemTemplate>
        <ItemTemplate>
            <asp:Label ID="Label3" runat="server" Text='<%# Bind("to_date","{0:dd/MM/yyyy}") %>'></asp:Label>
        </ItemTemplate>
</asp:TemplateField>

on update click i have a code like this
C#
protected void leave_gridview_RowUpdating(object sender, GridViewUpdateEventArgs e)
   {
       GridViewRow row = (GridViewRow)leave_gridview.Rows[e.RowIndex];
       Label leave_id = (Label)row.FindControl("leave_id");
       string num = leave_id.Text;
       TextBox from_date = (TextBox)row.FindControl("from_date");
       DateTime from_dt = Convert.ToDateTime(from_date.Text);
       TextBox to_date = (TextBox)row.FindControl("to_date");
       DateTime to_dt = Convert.ToDateTime(to_date.Text);
       TextBox sick_leaves = (TextBox)row.FindControl("sick_leaves_allowed");
       int new_sick_leaves = Convert.ToInt32(sick_leaves.Text);
       TextBox casual_leaves = (TextBox)row.FindControl("casual_leaves_allowed");
       int new_casual_leaves = Convert.ToInt32(casual_leaves.Text);
   }



Please help me to solve my this error
Thanks to you all for always supporting me
Posted
Updated 10-Sep-14 23:11pm
v4

1 solution

You need to use formatting when converting the string.
Some options. Pick a winner.

If your date format looks like this "dd/MM/yyyy" then use this code
C#
DateTime from_dt = DateTime.ParseExact(from_date.Text, "dd/MM/yyyy", null);

If your date format looks like this "MM/dd/yyyy" then use this code
C#
DateTime from_dt = DateTime.ParseExact(from_date.Text, "MM/dd/yyyy", null);

If your date format looks like this "yyyy-MM-dd" then use this code
C#
DateTime from_dt = DateTime.ParseExact(from_date.Text, "yyyy-MM-dd", null);



Refer to these links for more info
Standard Date and Time Format Strings[^]

Custom Date and Time Format Strings[^]
Refer to these links for more info
Standard Date and Time Format Strings[^]

Custom Date and Time Format Strings[^]
 
Share this answer
 
v2
Comments
Omkar Hendre 11-Sep-14 5:15am    
Sorry but its not working correctly while debugging i got the from date as 09/11/2014 which is wrong as per my requirement because i want this date in 11/09/2014 format and in my database it is stored as 2014-09-11
George Jonsson 11-Sep-14 5:21am    
The code I provided was an example. If you have another date format you need to change the format string.
What is the exact contents of 'from_date.Text'?
Also don't worry about the display format. That can easily be fixed to what ever you desire in the GUI.
Omkar Hendre 11-Sep-14 6:59am    
actually i stored the from date as 2014-09-11 i.e. (2014 sept 11th) ok and while reteriving the date i got 11/09/2014 i.e. (11 sept 2014) in item template of gridview but while updating it gives the error that String was not recognized as a valid DateTime. i think i may be possible that my month becomes and day and day becomes month i don't what exactly happened but i want to solve this error
George Jonsson 11-Sep-14 7:08am    
The default date in the grid view depends on the locale settings on your computer.
If you need a specific date format, but run on computers with different locales, then you need to check what the the current setting is.
Omkar Hendre 11-Sep-14 7:23am    
Great sir it works for me but now i got the from date as 11-sep-2014 12:00:00 AM in from_dt and
15-NOV-2014 12:00:00 AM in to_dt is it ok to update contents of table with this format or i need to changed it again and thanks for your valuable help

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