protected void Button4_Click(object sender, EventArgs e) { try { if ((Path.GetExtension(FileUpload2.PostedFile.FileName).Substring(1) == "jpg") || (Path.GetExtension(FileUpload2.PostedFile.FileName).Substring(1) == "JPG")) { abc A = new abc(); int t = A.chkcoverstat(Session["uname"].ToString()); if (t == 0) { fi = new FileInfo(Server.MapPath("temp") + "\\q.jpg"); fi.CopyTo(Server.MapPath("Profileimg" + "\\" + Session["uname"].ToString()) + "\\q" + i + ".jpg"); } else { fi = new FileInfo(Server.MapPath("Account" + "\\" + Session["uname"].ToString()) + "\\q.jpg"); string cnn = Server.MapPath("Profileimg" + "\\" + Session["uname"].ToString()); DirectoryInfo d = new DirectoryInfo(cnn); FileInfo[] f = d.GetFiles(); j = f.Count(); k = i + j; fi.CopyTo(Server.MapPath("Profileimg" + "\\" + Session["uname"].ToString()) + "\\q" + k + ".jpg"); } FileUpload2.SaveAs(Server.MapPath("Account" + "\\" + Session["uname"].ToString() + "\\q.jpg")); SqlConnection cn = new SqlConnection(); cn.ConnectionString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["cn"].ToString(); cn.Open(); string str = "update reg set CoverStatus=1 where Email='" + Session["uname"].ToString() + "'"; SqlCommand cmd = new SqlCommand(str, cn); cmd.ExecuteNonQuery(); cn.Close(); } else { alert1.Visible = true; Label3.Text = "OOPs! Only jpg Format images can be uploaded"; } } catch (Exception ex) { alert1.Visible = true; Label3.Text = "OOPs! Choose the image you want to upload"; } }