The string was not recognized as a valid DateTime. There is an unknown word starting at index 0.

Last Reply 10 days ago By dharmendr

Posted 11 days ago

The string was not recognized as a valid DateTime. There is an unknown word starting at index 0.

Getting error below line.

cmd.Parameters.AddWithValue("@D_Date", Convert.ToDateTime(date));

 

            string date;
            foreach (GridViewRow row in GridView1.Rows)
            {
                if (!string.IsNullOrEmpty((row.FindControl("TIME_IN") as TextBox).Text.Trim()) && !string.IsNullOrEmpty((row.FindControl("TIME_Out") as TextBox).Text.Trim()))
                {


                    TextBox inttime = (row.FindControl("TIME_IN") as TextBox);
                    TextBox outtime = (row.FindControl("TIME_Out") as TextBox);
                     date = Convert.ToString(row.FindControl("Date") as Label);

            


                    if (!string.IsNullOrEmpty(inttime.Text) && !string.IsNullOrEmpty(outtime.Text))

                        Insert((Convert.ToDateTime(inttime.Text)), (Convert.ToDateTime(outtime.Text)),date);

                }

            }

        }
private void Insert(DateTime inttime,DateTime outtime,string  date)
{
    using (SqlCommand cmd = new SqlCommand("Sp_Get_Absent", con))
    {
        cmd.Parameters.AddWithValue("@Action", "Update");
        cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@EmpID", lbid.Text);
                cmd.Parameters.AddWithValue("@INTIMe", inttime);
                cmd.Parameters.AddWithValue("@OutTime", outtime);
                cmd.Parameters.AddWithValue("@D_Date", Convert.ToDateTime(date));
                con.Open();
        cmd.ExecuteNonQuery();
        ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", " alert('Time  saved sucessfully');", true);
        con.Close();
    }
}

 

 

You are viewing reply posted by: dharmendr 10 days ago.
Posted 10 days ago

Hi akhter,

Use TryParseExact.

DateTime dateOut;
string date = "2022-06-12";
DateTime.TryParseExact(date, "yyyy-MM-dd", new CultureInfo("en-US"), DateTimeStyles.None, out dateOut);