Auto number generation using C# and SQL

Auto number generation using C# and SQL
------------------------------------------------------

Req:  Create a Autonumber for Particular Application form(Eg:Order or Quote)

Approach:
-------------
1.Create a Separate autonumber configuration table which looks like below

AutoNumConfig table
--------------------------
 
AutoNumberId  AutoNumber   FormNameorFormId  
-------------------------------------------
   1             ORD-0002     Order or 1066
   2             QUO-0002     Quote or 1067


2.Create Transaction table for Application  Form(Eg:Order/Quote) which looks like below

OrderTable
--------------

OrderId   OrderName  OrderAutoNum
-------------------------------------------
  1       Spartan      ORD-0001
  2       Xamarin      ORD-0002


QuoteTable
--------------

QuoteId   QuoteName  QuoteAutoNum
-------------------------------------------
  1       Spartan      QUO-0001
  2       Xamarin      QUO-0002


3.When User X logged in and Create a New Order/Ouote for the First time
 
4.Check the table Contains order/quote has value,if not it should be first Value which means ORD-00001

5.If User X logged in second time and create a new Order.

6.Reterive the newly generated autonumber from autonumconfig table and increment the value by 1 and update the AutoNumConfig table and lock the table.
  reason behind to lock the table is, another user to prevent to read the previous generated autonumber.If another user logged in and try to create order,in this case the user gets error as it locks.

7.Update the newly generated autonumber in the transation table.(Eg:ORD-00002)

8.After Successful inserted record in Order table release the lock of autonumconfig table.

9.U can use DB lock or C# lock.My preference is best to go C# lock



Advantage of use above Design
---------------------------------------

1.Extensiblity,Suppose if req change like,want to generate autonumber based on Organization
like below

  Eg:  ORD-ORG1-00001
         ORD-ORG2-00001
         ORD-ORG1-00002

the above design will be suitable and best way to go.

2.You can Generate new auto number for any new Form as autonumber configuartion table is separate from transacation table which is loosly coupling.

3.Also there is concept called Autonumber generate based on primary key in SQL,this will generate automatically without any hard stuff,but the thing is Point no:1 requriment will not satisfy if it change based on Organization.


                                          Happy Coding!!!!

 

Comments

Popular posts from this blog

Asp.net- Encrypt and Decrypt connection strings in web.config file

CRM Automate build Solution using Powershell commands

Access the fields on Quick View Form – Dynamics CRM 2015