String was not recognized as a valid DateTime.

Mar 9 2017 12:05 AM
protected void btnrenew_Click(object sender, EventArgs e)
{
int P_Id = Convert.ToInt32(((Button)sender).CommandArgument.ToString());
SqlCommand cmd = new SqlCommand("Select Renew, Term From Plans where P_Id= '" + P_Id + "'", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
int RenewDate = Convert.ToDateTime(dt.Rows[0]["Renew"].ToString()).Day;
int RenewMonth = Convert.ToDateTime(dt.Rows[0]["Renew"].ToString()).Month;
int RenewYear = Convert.ToDateTime(dt.Rows[0]["Renew"].ToString()).Year;
int Term = Convert.ToInt32(dt.Rows[0]["Term"].ToString());
int TotalMonth = RenewMonth + Term;
if (TotalMonth <= 12)
{
string NextRenewDate = Convert.ToString(RenewDate.ToString("00") + "-" + TotalMonth.ToString("00") + "-" + RenewYear.ToString("00"));
SqlCommand cmd1 = new SqlCommand("Update Plans Set Renew=@Renew Where P_Id= '" + P_Id + "' ", con);
cmd1.Parameters.AddWithValue("@Renew", Convert.ToDateTime(NextRenewDate).ToString("yyyy/dd/MM"));
con.Open();
cmd1.ExecuteNonQuery();
con.Close();
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Your Policy RenewDate is Updated Successfully')", true);
displaydetail();
}
else if (TotalMonth > 24)
{
int NextRenewYear = RenewYear + 2;
int TotalMonth1 = (RenewMonth + Term) % 12;
string NextRenewDate1 = Convert.ToString(RenewDate.ToString("00") + "-" + TotalMonth1.ToString("00") + "-" + NextRenewYear.ToString("00"));
SqlCommand cmd1 = new SqlCommand("Update Plans Set Renew=@Renew Where P_Id = '" + P_Id + "' ", con);
cmd1.Parameters.AddWithValue("@Renew", Convert.ToDateTime(NextRenewDate1).ToString("yyyy/dd/MM"));
con.Open();
cmd1.ExecuteNonQuery();
con.Close();
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Your Policy RenewDate is Updated Successfully')", true);
displaydetail();
}
else if (TotalMonth > 12)
{
int NextRenewYear = RenewYear + 1;
int TotalMonth1 = (RenewMonth + Term) % 12;
string NextRenewDate1 = Convert.ToString(RenewDate.ToString("00") + "-" + TotalMonth1.ToString("00") + "-" + NextRenewYear.ToString("00"));
SqlCommand cmd1 = new SqlCommand("Update Plans Set Renew=@Renew Where P_Id = '" + P_Id + "' ", con);
cmd1.Parameters.AddWithValue("@Renew", Convert.ToDateTime(NextRenewDate1).Date);
con.Open();
cmd1.ExecuteNonQuery();
con.Close();
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Your Policy RenewDate is Updated Successfully')", true);
displaydetail();
}
else
{
}
}

Answers (3)