I am trying to inserting time (HH:mm:ss) to database from GridView, it is giving error
String was not recognized as a valid DateTime.
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, " h:mm:ss tt", CultureInfo.InvariantCulture);
}
else
{
INTime = System.DateTime.Now;
}
DateTime Outtime;
if (!string.IsNullOrEmpty(date3))
{
Outtime = DateTime.ParseExact(date3, "h:mm:ss tt", CultureInfo.InvariantCulture);
}
else
{
Outtime = System.DateTime.Today;
}
SqlCommand cmd = new SqlCommand("SP_Insert_Emp_Time", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@R_ID", AttIDS.Text);
cmd.Parameters.AddWithValue("@TimeIN", date2);
cmd.Parameters.AddWithValue("@TimeOut", date3);
if ((date2 == ""))
{
cmd.Parameters["@TimeIN"].Value = DBNull.Value;
}
else
{
cmd.Parameters["@TimeIN"].Value = DateTime.Parse(date2);
}
if ((date3 == ""))
{
cmd.Parameters["@TimeOut"].Value = DBNull.Value;
}
else
{
cmd.Parameters["@TimeOut"].Value = DateTime.Parse(date3);
}
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
error on below line
Line 60: foreach (GridViewRow row in this.GVTime.Rows)