[Solved] How to autogenerate and increment Customer ID using C# in Asp.NET? [closed]


You need to store your counter in outside your method, like in SQL Server and then return the customer id. Below is a sample code:

private static readonly object o = new object();
public static int GenerateNextCustomerId() {
   lock (o) {
      // Write code to get the next counter from db in this method
       var counter = GetNextCounter();
       var nextCustomerId = string.Format("{0}{1}{2}",
                            DateTime.Now.Year, DateTime.Now.Month,
                            counter.ToString().PadLeft(4, '0'));

       // Write code to increment the counter and save to db in this method
       IncrementCounter();
       return int.Parse(nextCustomerId);
   }
}

2

solved How to autogenerate and increment Customer ID using C# in Asp.NET? [closed]