I have been making ID in my program but to have one it need to generate random numbers but if it already exist in my database then it has to perform another random numbers to prevent duplication . My problem is how do i generate another random numbers again?
here's my code:
private void button1_Click(object sender, EventArgs e)
{
random rand = new random();
aidentification.Text = rand.Next(1, 5).ToString();
string exist = string.Empty;
exist = "Select * from fruit_stock " +
"where identification=@id";
SqlConnection conn = new SqlConnection("server=WIN10;database=fruit_storage;user=fruit_admin;password=admin;");
SqlCommand cmd = new SqlCommand()
{
Connection = conn,
CommandType = CommandType.Text,
CommandText = exist
};
cmd.Parameters.AddWithValue("@id", aidentification.Text);
try
{
conn.Open();
SqlDataReader reader;
reader = cmd.ExecuteReader();
if (reader.HasRows){
// PUT THE CODE HERE TO PERFORM ANOTHER RANDOM NUMBERS
}
conn.Close();
}
catch (Exception ex) {
MessageBox.Show(ex.Message);
}
}
}
}