Resources » Code Snippets » ADO.NET

ID Auto Generation

Updated: Category: ADO.NET
Author: Member Level: GoldPoints: 10

We need to auto generate ID's say Employee ID, Student ID etc , in order to avoid the duplication.

The below piece of code will give you a idea about ID Auto generation.
Here have to select the max value of the id and have to increment it accordingly.

int iJobid = 0;
SqlConnection sqlCon = new SqlConnection(""Database=Master;Server=in123;User ID=sa;Password=sa123;");
SqlCommand sqlCmd = new SqlCommand();
sqlCmd.Connection = sqlCon;
sqlCmd.CommandText = "Select Max(EmpID) from EmployeeDetails";
iJobid = (int) sqlCmd.ExecuteScalar();
if (iJobid > 0)
iJobid = iJobid + 1;
iJobid = 1;
txtEmpID.Text = iJobid.ToString();

Did you like this resource? Share it with your friends and show your love!

Responses to "ID Auto Generation "
Author: Atul Vishwakarma  19 May 2009   Member Level: Bronze   Points : 1
This code is to simple...

Can you give the code increment like



Guest Author: deepa   07 Jun 2012
Code for autogeneration

Guest Author: Anil   20 Dec 2012
using char variable replacin int for ABC ,,,,,;)


Post Comment:

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)

    Type the numbers and letters shown on the left.

    Submit Article     Return to Article Index
    Subscribe to Subscribers
    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Talk to Webmaster Tony John

    Online Members

    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India