i got the solution, i just have to check the image path with the folder images name before perform update. because if i update first then my image path is updated with new one and i am not able to check it. SO first check and if found delete it and then update to database.
protected void btn_upload1_Click(object sender, EventArgs e)
{
try
{
if (FileUpload1.HasFile)
{
SqlCommand cmd2 = new SqlCommand("sps_addetails", con);
cmd2.CommandType = CommandType.StoredProcedure;
cmd2.Parameters.AddWithValue("@ad_id", ad_id);
cmd2.Parameters.AddWithValue("@useremail", ses);
con.Open();
SqlDataReader dr = cmd2.ExecuteReader();
while (dr.Read())
{
if (!string.IsNullOrEmpty(Convert.ToString(dr["imagepath1"])))
{
string imgpath = Convert.ToString(dr["imagepath1"]);
string abc = Server.MapPath("~/Image/" + imgpath);
File.Delete(abc);
}
}
con.Close();
fileName1 = FileUpload1.FileName;
filePath1 = Server.MapPath("Image/" + System.Guid.NewGuid() + fileName1);
FileUpload1.SaveAs(filePath1);
int getPos = filePath1.LastIndexOf("\\");
int len = filePath1.Length;
getPath1 = filePath1.Substring(getPos, len - getPos);
pathToStore1 = getPath1.Remove(0, 1);
con.Open();
SqlCommand cmd = new SqlCommand("sps_uploadphoto", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@flag", 1);
cmd.Parameters.AddWithValue("@ad_id", ad_id);
cmd.Parameters.AddWithValue("@useremail", ses);
cmd.Parameters.AddWithValue("@imagedata", pathToStore1);
cmd.Parameters.AddWithValue("@updatetime", DateTime.Now);
cmd.ExecuteNonQuery();
con.Close();
}
else
{
ScriptManager.RegisterStartupScript(this, typeof(string), "Error", "alert('Please browse file.');", true);
}
}
catch (Exception)
{
throw;
}
}