Logic for generation code in C#

Posted on 2007-10-18
Last Modified: 2010-04-15

I am working on C#, ASP.Net 1.2, and MSSQL 2000

In my database I have the code like C02, C03...C10, S01, S02, S05... etc. where first letter stands for the supplier name

Now, When I create the new code for supplier it should take the first letter from suppler name and the next 2 availabe digits

means if the max code availabe for supplier Cxyz is C20 then the next code generated for the supplier should be C21.

How to find the next available code and set the code to the supplier.

I require the logic for this

Question by:tia_kamakshi
    LVL 7

    Expert Comment

    string customerCode = "C123";
    string newCode = customerCode.subString(0, 1);

    int currentNumber = int.Parse(customerCode.subString(1, customerCode.length - 1));
    newNumber = currentNumber + 1;

    newCode += newNumber.toString();
    LVL 7

    Expert Comment

    Oops.. sorry, didn't read your question carefully.

    In your case I think you have to
    1. Query your database to get the max number (for that supplier)
    2. Do increment
    3. Add supplier's first letter to get new code.

    Author Comment

    But how do I get the max number, If I can get the max number then it was easy. Since I ave the code like C01, C02... these are not int

    LVL 26

    Expert Comment

    by:Anurag Thakur
    you can do the following logic
    you can improve the logic
    the logic i am giving is for SQL Server 2000

    Declare @SuppID Varchar(20)
    Select @SuppID = Max(Supplier_ID) From Suppliers

    Declare @IntSupplierID Int
    Declare @temp Varchar(10)
    Select @Temp = Substring(@SuppID, 2, len(@SuppID) - 1)

    Select @IntSupplierID = Convert(Int, @temp)

    Select @IntSupplierID = @IntSupplierID + 1
    Print @IntSupplierID

    If you want to make the integer of a particular length i.e. append zeros in fron of the number then you will have to use the string concatenation to make the string of a fixed size.
    LVL 7

    Expert Comment

    Well, that's simple:

    Select Max(supplierID)
    From Supplier
    Where SupplierID like 'C%';

    After that... use the code above :P
    LVL 7

    Accepted Solution

    So .. code in C# would be like this:

    // Done on one of my tables (my ID starts with REF, REG, PTC etc. followed by numbers)
    private string maxcurrentCode (string firstLetters)
      string query = "Select max(sampleID) from Sample where sampleID like '" + firstLetters + "%' ";

      // This is a method that I have in my application
      // getTable( tableName, query );
      DataTable table = getTable("Sample", query);

      return table.Rows[0][0].toString();

    private string getNewCode (string currentCode)
     string newCode = currentCode.subString(0, 1);
     int currentNumber = int.Parse(currentCode.subString(1, currentCode.length - 1));
     newNumber = currentNumber + 1;

     // Maybe you should append '0' like ragi0017 suggested
     newCode += newNumber.toString();
     return newCode;

    Hope this helps.
    LVL 1

    Expert Comment

    Forced accept.

    EE Admin

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
    Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now