String was not recognized as a valid DateTime ,when pass value from gridview to database

Analyst_SQL 3,576 Reputation points
2022-11-17T14:19:57.537+00:00

When i am inserting Time values from gridview to database ,then it is giving me error below

String was not recognized as a valid DateTime.

on this line > foreach (GridViewRow row in this.GVTime.Rows)

 <asp:TemplateField HeaderText="INTime">  
                <ItemTemplate>  
                    <asp:TextBox ID="txtINTime"  runat="server" Text='<%# Bind("TimeIN") %>'   TextMode="Time"> </asp:TextBox></ItemTemplate>  
            </asp:TemplateField>  

   protected void update_Click(object sender, EventArgs e)  
        {  
  
            foreach (GridViewRow row in this.GVTime.Rows)  
            {  
  
                var AttIDS = row.FindControl("R_ID") as Label;  
  
                var date2 = (row.FindControl("txtINTime") as TextBox).Text;  
                var date3 = (row.FindControl("txtOuttime") as TextBox).Text;  
  
  
                DateTime INTime;  
  
                if (!string.IsNullOrEmpty(date2))  
                {  
                  //  INTime = DateTime.ParseExact(date2, "HH:mm:ss", CultureInfo.InvariantCulture);  
                    INTime = DateTime.ParseExact(date2, "hh:mm:ss tt", CultureInfo.InvariantCulture);  
  
                }  
                else  
                {  
                    INTime = System.DateTime.Now;  
  
  
                }  
  
                DateTime Outtime;  
                if (!string.IsNullOrEmpty(date3))  
                {  
                 //   Outtime = DateTime.ParseExact(date3, "HH:mm:ss", CultureInfo.InvariantCulture);  
                    Outtime = DateTime.ParseExact(date2, "hh:mm:ss tt", CultureInfo.InvariantCulture);  
                }  
                else  
                {  
                    Outtime = System.DateTime.Today;  
  
                }  
  
  

below is gridview image,i have null value also in Time

261495-time.jpg

In Database format

INtime OutTime
08:05:00 17:09:00
08:01:00 16:59:00
08:13:00 17:15:00

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,532 questions
ASP.NET API
ASP.NET API
ASP.NET: A set of technologies in the .NET Framework for building web applications and XML web services.API: A software intermediary that allows two applications to interact with each other.
347 questions
0 comments No comments
{count} votes

Accepted answer
  1. Lan Huang-MSFT 29,911 Reputation points Microsoft Vendor
    2022-11-18T05:40:24.617+00:00

    Hi @Analyst_SQL ,
    DateTime.ParseExact Method Converts the specified string representation of a date and time to its DateTime equivalent.
    But you only want the time, you need to use DateTime.ToShortTimeString Method:Converts the value of the current DateTime object to its equivalent short time string representation.
    https://learn.microsoft.com/en-us/dotnet/api/system.datetime.toshorttimestring?view=net-6.0

    DateTime.ParseExact(date2, "HH:mm:ss", CultureInfo.InvariantCulture).ToShortTimeString();  
    

    According to your code, I guess you want to replace the empty value with the current time. I slightly modified your code:

     protected void Button1_Click(object sender, EventArgs e)  
            {  
                foreach (GridViewRow row in this.GridView1.Rows)  
                {  
                    var date2 = (row.FindControl("txtINTime") as TextBox).Text;  
                    var date3 = (row.FindControl("txtOuttime") as TextBox).Text;  
                    for (int i = 0; i < GridView1.Rows.Count; i++)  
                    {  
                         
                        if (!string.IsNullOrEmpty(date2))  
                        {  
                            //Cells[4] is modified according to its own column. Start from 0.  
                            row.Cells[4].Text = DateTime.ParseExact(date2, "HH:mm:ss", CultureInfo.InvariantCulture).ToShortTimeString();  
                        }  
                        else  
                        {  
      
                            row.Cells[4].Text = DateTime.Now.ToShortTimeString();  
                        }  
                        if (!string.IsNullOrEmpty(date3))  
                        {                         
                            row.Cells[5].Text = DateTime.ParseExact(date3, "HH:mm:ss", CultureInfo.InvariantCulture).ToShortTimeString();  
                        }  
                        else  
                        {  
                            row.Cells[5].Text = DateTime.Now.ToShortTimeString();  
                        }   
                    }  
                }             
            }  
    

    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.