How to create a unique reference number

0 comments

create a unique reference number, then check in the database to make sure it's not already there


		/// <summary>
		/// create a reference number of the form 6 chars, dash, 3 chars. check it is unique across all records in database
		/// </summary>
		/// <returns></returns>
		public static string GenerateRef()
		{
		  string result = "";
		  for (int sc = 0; ; sc++)
		  {
			if (sc > 20) throw new Exception("ref gen failed");
			//result = RandomPassword.Generate(6, 6, "Q", "QWERTYPASDFGHJKLZXBNM", "23456", "2") + "-" + RandomPassword.Generate(3, 3, "W", "WXYZ", "23456", "2");
			result = "IY" + Crypto.RandomDigit() + Crypto.RandomDigit() + Crypto.RandomDigit() + Crypto.RandomDigit() + Crypto.RandomDigit();
			if (QuoteList.LoadByRef(result).RecordCount == 0)
			{
			  break;
			}
		  }
		  return result;
		}

Comments


Leave a Comment