• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 199
  • Last Modified:

Logic for generation code in C#


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

1 Solution
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();
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.
tia_kamakshiAuthor Commented:
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Anurag ThakurCommented:
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.
Well, that's simple:

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

After that... use the code above :P
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.
Forced accept.

EE Admin

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now