title: Alternatives to using Max+1 to create a user friendly unique sequence numbers
body: <p>I have the repository method inside my asp.net mvc web application, to automatically generate a unique sequence number called Tag:-</p>
  
  <pre><code>public void InsertOrUpdateServer(TMSServer server, string username)
          {
  
      if (server.TMSServerID == default(int))
              {
                  // New entity
      int technologyypeID = GetTechnologyTypeID("Server");
      Technology technology = new Technology
         {
       IsDeleted = true,
       Tag = "S" + GetTagMaximumeNumber(technologyypeID).ToString(),
       StartDate = DateTime.Now,
       };
     Save();
  //code goes here
  </code></pre>
  
  <p>Where the GetTagMaximumeNumber() function is :-</p>
  
  <pre><code>public int GetTagMaximumeNumber(int typeID) {
  string s = tms.Technologies.Where(a =&gt; a.TypeID == typeID).Max(a2 =&gt; a2.Tag.Substring(1));
  
             if (s != null)
             {
               return Int32.Parse(s) + 1;
  
             }
             else {
                 return 100;//if this is the first record created
             }}
  </code></pre>
  
  <p>The above will get the maximum number of the tag and add 1 then try to save it into the database. The above is working well, but I am sure that on production server where multiple users will be adding records, same tag numbers will be generated and exception will be raised when trying to add an automatically generated tag number that already exists on the database. 
  So I have the following questions:-1</p>
  
  <ol>
  <li><p>What are the other approaches which I can follow to have a sequence number that is always unique.</p></li>
  <li><p>In my current approach the gaps in the Tag number are minimal. The gap only occurs when user delete a record and its tag number was not the maximum. So is there a way to still have this in any new approach, so gaps will be minimal?</p></li>
  </ol>
  
  <p>Thanks for the help.</p>