I am using this code to generate unique code
public static string CreateRandomPassword()
{
string _allowedChars = "1234567899999";
Random randNum = new Random((int)DateTime.Now.Ticks);
char[] chars = new char[5];
for (int i = 0; i < 5; i++)
{
chars[i] = _allowedChars[randNum.Next(_allowedChars.Length)];
//No need to over complicate this, passing an integer value to Random.Next will "Return a nonnegative random number less than the specified maximum."
}
return new string(chars);
And this to use to send entry to sql table . ids is the text box in which i am entering how many unique code is to be generate
protected void Button1_Click(object sender, EventArgs e)
{
var sand = CreateRandomPassword();
int num;
if ( int.TryParse(ids.Text,out num))
{
for (int i = 1; i <= num; i++)
{
string strcon = ConfigurationManager.ConnectionStrings["slxserv"].ToString();
using (SqlConnection con = new SqlConnection(strcon))
using (SqlCommand cmd = new SqlCommand("INSERT INTO passwords (password) VALUES (@password) "))
{
cmd.Connection = con;
cmd.Parameters.AddWithValue("@password",sand);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
Please help me when i am trying to generate more than one unique code it send the data to sql with same code multiple times . i am doing anything wrong please let me know . or it is possible to do with sql procedure also please let me know