Click here to Skip to main content
15,890,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to generate EmpID automatically. EmpID must be generated like EMP0001, EMP0002,EMP0003... How to increment ID which contains variable?
Posted

Use following code snippet. create one function as below :

C#
public string NewEmpId()
{
        string strTempId, strMaxNum;
        strMaxNum = "2";    //Get this number from your database.using max(right(EmpId,4))
        if (strMaxNum == null || strMaxNum == "")
        {
            strMaxNum = "0";
        }
        strTempId = Convert.ToString(Convert.ToInt32(strMaxNum) + 1);
        if (strTempId.Length == 1)
        {
            strTempId = "000" + strTempId;
        }
        else if (strTempId.Length == 2)
        {
            strTempId = "00" + strTempId;
        }
        else if (strTempId.Length == 3)
        {
            strTempId = "0" + strTempId;
        }
        string strQuoteId = "EMP" + strTempId;
        return strQuoteId;
}



than call this function as below :

C#
string strNewEmpId = NewEmpId();
 
Share this answer
 
v2
Comments
NehaShetty 13-Oct-12 6:24am    
Thanks Bhushan Shah1988 for reply, it worked fine.
SQL
create table randomid(CandID as 'EMP' + RIGHT('000' + CONVERT(varchar, CourseID),5),
CourseID int IDENTITY(1,1),
ReferrerName varchar(10)
)
insert into randomid values('sonu')
select * from randomid
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900