I have a database where I am uploading the photo path name of the folder and photo name, and I am able to pull photos in my GridView.
My problem is when I try to update the photo from my GridView, what it does is if the photo name is same then it replaces the previous photo which is of same name in the path folder, thus deleting that photo from the path folder.
What I want is how to generate unique photo name if photo name is all ready existing?
This is my code:
protected void GridView5_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string Id = GridView5.DataKeys[e.RowIndex].Value.ToString();
FileUpload FileUpload1 = (FileUpload)GridView5.Rows[e.RowIndex].FindControl("FileUpload1");
con = new SqlConnection(connStr);
string path = "~/PPhoto/";
if (FileUpload1.HasFile)
{
for (int i = 0; i < Request.Files.Count; i++)
{
path += FileUpload1.FileName;
// save image in folder
FileUpload1.SaveAs(MapPath(path));
SqlCommand cmd = new SqlCommand("update tblLogin set Pic = @Pic, ImageName = @ImageName where LoginId=" + Id + "", con);
cmd.Parameters.AddWithValue("@Pic", path);
cmd.Parameters.AddWithValue("@ImageName", FileUpload1.FileName);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}