Generating Auto Number

This code snippet will generate a Auto Number with your format, I tried generalize as much as possible, this function allows to decide how many digits you want have and you have a Pre Fix to your Auto Number.

public string GenerateAutoNumber(string preFix, int numberOfDigits, string currentNumber)

{

//preFix : it can be any string which you want to have starting of your number

//numberOfDigits: this specifies how many digits you have in your AutoNumber

//currentNumber: this is current Number if pass current Number it will generate next number for example

// if your number is SAT00001 then function will return SAT00002

if (currentNumber == "")

{

currentNumber = preFix + currentNumber.PadLeft(numberOfDigits, ’0′);

}

double autoNumber = Convert.ToDouble("2" + currentNumber.Substring(preFix.Length, currentNumber.Length – (preFix.Length)));

string strNumber = (autoNumber + 1).ToString();

return preFix + strNumber.Substring(1, strNumber.Length – 1);

}

 

Testing Above Function

//Initially Generating Number

String currentNumber = GenerateAutoNumber("SAT", 5 , "");

Response.Write(currentNumber); // Ouput will be SAT00001

//Generating auto number based on currentNumber

Response.Write(GenerateAutoNumber("SAT", 5, currentNumber)); // Ouput will be SAT00002

 

Satish Kumar MVP (ASP.NET)

Leave a Reply

Powered by WP Hashcash