Auto Number RRS feed

  • Question

  •  I want to generate auto number based on region if region is A then i want auto number to start with A-YYMM-001

    (YY is year and MM is month)

    if region is B i want  to generate auto number to start with B-YYMM-001

    if again region B is entering data it should be B-YY-MM-002

    if again region A is entering data it should be A-YY-MM-002

    Please help

    Thursday, March 15, 2018 12:08 PM

All replies

  • First of all: B-1803-001 is not a number. It never was, it never will be. Precision in naming things is a must for developers.



    public class Region
    	public string AutoText { 
    		get {
    			return string.Format("{0}-{1:2:D2}{2:2:D2}-{3:3:D3}"; this.Type, DateTime.Now.Year, DateTime.Now.Month, this.regions.IndexOf(this));
    	private List<Region> regions;
    	public string Type { get; private set; }
    	public Region(string type, List<Region> regions)		
    		this.regions = regions;		
    		this.Type = type;

    • Proposed as answer by Stanly Fan Friday, March 16, 2018 5:50 AM
    Thursday, March 15, 2018 12:32 PM
  • Try this function too:

    static Dictionary<string, int> d = new Dictionary<string, int>();
    static string GetNext( string region )
       int i;
       d.TryGetValue( region, out i );
       d[region] = ++i;
       var today = DateTime.Today;
       return string.Format( "{0}-{1:00}{2:00}-{3:000}", region, today.Year - 2000, today.Month, i );

    • Proposed as answer by Stanly Fan Friday, March 16, 2018 5:50 AM
    Thursday, March 15, 2018 12:44 PM
  • In addition to Stephan, you can call it an identifier (that is not only for databases). 


    Thursday, March 15, 2018 12:57 PM
  • Yes, I did something similar with regions and using MS SQL Server database tables that were using an Identity auto incremented  primary-key. The primary-key for a table only had the Identity auto incremented  primary-key  column in it and no other columns.

    When I inserted a record into the table through code, the next sequential integer ID was retuned from the database engine for the key, and I could use integer key it in a similar manner you are talking about to formulate a name based on the auto-count being part of the name that was Tosting() as part of the name.

    Yes, the tables were called reagionname-count tables. 

    Thursday, March 15, 2018 4:00 PM