title: Is there a way to check if random Generated Number existed in database? C#
body: <p>I have this automatic LRN generate Numbers consist of 12 numbers only first i set "number" = 100000000000 + random numbers so example of output 100569815234 then that number will check if it is already existing in my database if not that number will use to register a student. but if it already existing in database I have this another generating random number same as the first random numbers but how can I check again if it already exist in database? </p>
  
  <p>this is my code</p>
  
  <p>inside form2_load*</p>
  
  <pre><code>        Random rnd = new Random(); //
          long firstLRN = rnd.Next(1000000000);
          long addLRN = 100000000000 + firstLRN;
          string FinalLRN = Convert.ToString(addLRN);
  
          try
          {
              connection.Open();
              OleDbCommand command = new OleDbCommand();
              command.Connection = connection;
              command.CommandText = "select LRN FROM Student_LRN where LRN = '" + FinalLRN + "'";
  
              OleDbDataReader reader = command.ExecuteReader();
  
              int countLRN = 0;
              while (reader.Read())
              {
                  countLRN++;
  
              }
              if (countLRN == 0)
              {
                  label_LRN.Text = FinalLRN;
              }
              else if (countLRN == 1)
              {
                  long randomNewLRN = rnd.Next(1000000000);
                  long newLRN = 100000000000 + randomNewLRN;
                  string newFinalLRN = Convert.ToString(newLRN);
                  label_LRN.Text = newFinalLRN;
                  /*
                   * what if this another random generated number already existing again? what can i do with this?
                   */
              }
  
          }
          catch (Exception ex)
          {
              MessageBox.Show("Error" + ex);
          }
          finally
          {
              connection.Close();
          }
  </code></pre>