When i debug code with f10 IT WORKS with NO ERROR !.But on runtime i got this error:
“An item with the same key has already been added”
Plz help
My Dictionary :
public static Dictionary<string, string> ImageFilePath
= new Dictionary<string, string>();
Using in same Glob.cs my function :
public static Image ShowImageOnColumn(string Value,byte ImageHeigth,byte ImageWidth)
{
.
.
.
string FilePath = "",ImgId = "";
Image img_ = new Image();
Random rnd = new Random();
ImgId = rnd.Next(100000000).ToString();
img_.ImageUrl = "ShowImageInRuntime.aspx?FileName=" + ImgId;
ImageFilePath.Add(ImgId, FilePath);
img_.Height = Unit.Pixel(ImageHeigth);
img_.Width = Unit.Pixel(ImageWidth);
return img_;
}
You are not guarding your
ImageFilePath.Addcall. If the key already exists, you will get an exception saying as much.You can do a check for a key:
Or you can set on the index, this will add if it’s missing and update if it exists:
As opposed to calling Add.
Note, however, that static members are liable to being lost when IIS recycles worker processes. Hence they tend to be avoided. There are also multi-threading issues as the static member is visible across the process.
If you need a random file name, try
DateTime.ToString("ddMMyyyyhhmmssfff")orGuid.NewGuid(), as opposed to keeping an instance ofRandomalive.Pathalso features aGetTempFileNamemethod.