1

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();                  
        }
    }
}
5377037
  • 9,493
  • 12
  • 43
  • 73
Turing
  • 132
  • 11
  • before updating the image, you can check same record is available or not in the database using `select` query. if count of existing records are more than zero then update file name with some random new name and update the record. – Prasad Telkikar Mar 09 '18 at 07:46
  • 1
    **WHY** aren't you using a **parameter** for the `LoginId` ?!?!?! YOu're using parameters for the other values - why not for this one, too?? (as you **should!**) – marc_s Mar 09 '18 at 07:53
  • You should check out [Can we stop using AddWithValue() already?](http://blogs.msmvps.com/jcoehoorn/blog/2014/05/12/can-we-stop-using-addwithvalue-already/) and stop using `.AddWithValue()` - it can lead to unexpected and surprising results... – marc_s Mar 09 '18 at 07:54
  • @Prasadtelkikar allright I'll find if duplicate name existing, but can you show me how to generate random name? – Turing Mar 09 '18 at 07:59
  • This might be helpful: https://stackoverflow.com/questions/1344221/how-can-i-generate-random-alphanumeric-strings-in-c – Prasad Telkikar Mar 09 '18 at 08:03
  • System.Guid for random string. – Chetan Ranpariya Mar 09 '18 at 08:07
  • why no stored procs? – Michael Wayne Mar 09 '18 at 11:21

1 Answers1

1

You have to generate a random number using below method:

private int GenerateRandomCode()
{
    Random rnd = new Random();
    int randomNumber = rnd.Next(1, 999); // creates a number between 1 and 999
    return randomNumber;
}

And then concatenate that generated number with file name (without extension using Path.GetFileNameWithoutExtension Method (String)) as like this:

string namewithoutextension = Path.GetFileNameWithoutExtension(path);// pass full path here
string filename = namewithoutextension + GenerateRandomCode(); // result: e.g. ImageName565

BTW: you should also use AddWithValue Method for LoginId:

cmd.Parameters.AddWithValue("@LoginId", Id);
5377037
  • 9,493
  • 12
  • 43
  • 73
  • This works, but wondering can the concatenate happens before the extension of the image? For example now it is happening this way "photo.jpg234" but just wondering can it happen this way "photo234.jpg"? – Turing Mar 09 '18 at 08:51
  • nope, it still generates the number after extension: "photo.jpg324" what I am looking for is "photo324.jpg". I guess when it is taking the path it takes with the extension too and then concatenate. So maybe concatenate just before "." extension, I don't know if this make sense. – Turing Mar 09 '18 at 09:17
  • your path should be complete with file name and then use `Path.GetFileNameWithoutExtension(path)` – 5377037 Mar 09 '18 at 09:21
  • Could not achieve that, but any ways it solved my problem by not letting duplicate photo name. Thanks!!! – Turing Mar 09 '18 at 11:13