Resources » .NET programming » ASP.NET/Web Applications

How to create Auto Generate Id in C#


Last Updated:   Category: ASP.NET/Web Applications    
Author: Member Level: Gold    Points: 10


Here create Auto generate Id in .Net application.The AutoGenerate Id used in more application..For Example Employee Information System, Employee Id is Company name with serial no.Example: XYZ001. Using Padleft function,For example:PadLeft(4, '0') and substring function used.



How to create AutoGenerate Id



The AutoGenerate Id used in more application..For Example Employee Information System, Employee Id is Company name with serial no.Example: XYZ001

Here I explaind about AutoGenerate Id and how to create.


Just assign int NewId and Padleft is after "XYZ" four charecter assign 0 value ,ie XYZ0001

Here this method invoke DB.newid()


//DataSet Invoke from DBConnection class
  

public class DBconnection
{
string str;
SqlConnection sqlconn;
SqlCommand cmd;
SqlDataAdapter sqlada;
DataSet ds;

public string newid()
{
string AtCnt;
SqlCommand DTcmd = new SqlCommand();
SqlConnection Dtcon = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
Dtcon.Open();
DTcmd.CommandType = CommandType.Text;
DTcmd.CommandText = "select max(substring(EmpId,4,5)) from Emp_Table (nolock)";
DTcmd.Connection = Dtcon;
AtCnt = Convert.ToString(DTcmd.ExecuteScalar());
Dtcon.Close();
return AtCnt;
}
}


C# coding:




protected void btn_submit_Click(object sender, EventArgs e)
{
lblmessage.Text = "";
int NewId = 1;
string strnewid = " ";
object obj = DB.newid(); //DataSet Invoke from DBConnection class
if (obj.ToString() != "")
{
NewId = Convert.ToInt32(obj.ToString()) + 1;
strnewid = NewId.ToString();
strnewid = strnewid.PadLeft(4, '0');
lbl_id.Text = "XYZ" + strnewid;
}
else
{
lbl_id.Text = "XYZ0001";
}

}



Output:
XYZ0001


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

Responses to "How to create Auto Generate Id in C#"
Author: ghjjfgnj    20 May 2011Member Level: Silver   Points : 1
in sql server

create database Hospital
create table registration (PatientID numeric(20) IDENTITY(1,1) primary key , PatientName varchar(30),DOB varchar(20) , Patient_Address varchar(100), MobileNo numeric(15), age int, sex varchar(10), MaritalStatus varchar(20), Problem varchar(100),TotalRegFee numeric(20), FeeGiven numeric(20), BalanceFee numeric(20))
insert into registration values('aman','12-Jan-1998','c 85 sec 49, Noida' ,9090909090,29,'Female', 'Married',' stomach pain',2000,1500,500)
insert into registration values('shikha','13-Mar-1998','c 82 sec 49, Noida' ,909033390,29,'Female', 'Married',' kideny pain',2000,1500,500)

select * from registration

in c# class


namespace ClassEntity
{[Table (Name="registration")]
public class ClassEC
{
private long _PatientID;
private string _PatientName;
private string _PatientDOB;
private string _Address;
private long _MobileNo;
private long _Age;
private string _Sex;
private string _MaritalStatus;
private string _Problem;
private long _TotalRegFee;
private long _FeeGiven;
private long _BalanceFee;

[Column(DbType = "numeric(20)", IsPrimaryKey = true, IsDbGenerated=true)]
public long PatientID
{
get
{
return _PatientID;
}
set

{
_PatientID = value;

}
}



Feedbacks      

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
    Awards & Gifts
    Talk to Webmaster Tony John

    Online Members

    Jeevan Joshi
    More...
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India