Please I want to generate an Identification Number for each user. An ID that cannot be repeated with a maximum of 7 numbers. I also don't want to use GUID or IDENTITY.
Please help me check if it is okay to go with this?
private string GetAutoNumber()
{
string numbers = "1234567890";
string characters = numbers;
int length = 7;
string id = string.Empty;
for (int i = 0; i < length; i++)
{
string character = string.Empty;
do
{
int index = new Random().Next(0, characters.Length);
character = characters.ToCharArray()[index].ToString();
} while (id.IndexOf(character) != -1);
id += character;
}
return id;
}
Then call The GetAutoNumber function on pageload like this
if (!IsPostBack)
{
List<string> result = new List<string>();
if (Session["Numbers"] != null)
{
result = (List<string>)Session["Numbers"];
}
string number = GetAutoNumber();
if (!result.Contains(number))
{
result.Add(number);
Session["Numbers"] = result;
Labelinvoice.Text = number;
}
}