hi!
how to give unique id's to images in asp.net web forms.
means every image goes to images folder like. photo1,photo2,photo3.........
plz give example code to do this.
thanks.
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Rahul RayRank #4,064 · 368 reputationPosted Mar 8, 2010, 3:46 AM EST
Use
Following code to get the unique id
Guid guid = Guid.NewGuid;
string uniqueId=guid .Tostring();
string fileName="originalFileName"+"_"+uniqueId;
assign to this to ur file and during fileUpload.SaveAs();
give name to it.
Thanks
Rahul Deb Ray
Amit ChoudharyRank #145 · 15.8K reputationPosted Mar 8, 2010, 1:39 AM EST
public static string GenerateRandomString(StringType stype, int numberOfChars) { string s = ""; string alphas = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; //if you want to add other charaters add it here - zero and O are removed to avoid confusion string alphanumeric = "1234567890ABCDEFGHIJKLMNPQRSTUVWXYZ"; Random rnd = new Random(); for (int i = 0; i < numberOfChars; i++) { if (stype == StringType.Numeric) { s = String.Concat(s, rnd.Next(10).ToString()); } else if (stype == StringType.Alpha) { s = String.Concat(s, alphas.Substring(rnd.Next(24), 1)); } else { s = String.Concat(s, alphanumeric.Substring(rnd.Next(33), 1)); } } return s; }Pass no. character for random generated string and enumeration of the stringType define above.
use following function to generate the random strings and add them to your images names while uploading in your file.SaveAs() method.
Please Don't forget to mark as answer.
ajay rajuRank #4,639 · 333 reputationPosted Mar 8, 2010, 8:43 PM EST
Thanks For Helping, But Here images are not goto uniform. it goes like Photo0,Photo1,Photo3,Photo6,Photo8,Photo7....
i write
Dim uniqueID As String = GenerateRandomString(StringType.Numeric, 1).ToString()
Dim filename As String = "photo" & uniqueID & ".Jpg"
How to solve This.