1,105,221 Community Members

How to Create Autogenerate ID in C#

Member Avatar
rehanul.bappy
Newbie Poster
6 posts since Jul 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

How to generate auto id like PA-01-07-2012-0001. Where 01 is the Date of a month,07 is the Month, 2012 is the year, and 0001 is auto generate id and it will be increment. after that it will check The Generated id in database.And create a new id

Member Avatar
superjj
Light Poster
42 posts since Nov 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
0
 
int id;
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {


        string ID = "PA-"+DateTime.Now.Day+"-"+DateTime.Now.Month+"-"+DateTime.Now.Year+"-"+id;
        id++;
        MessageBox.Show(ID);
    }
Member Avatar
Mike Askew
Posting Shark
925 posts since Nov 2010
Reputation Points: 132 [?]
Q&As Helped to Solve: 85 [?]
Skill Endorsements: 25 [?]
Featured
 
0
 

@superjj

So where does id value come from? Looks like you add a blank value there.

Declared at the top and then used while blank if we assume no code isnt written there.

Member Avatar
superjj
Light Poster
42 posts since Nov 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
0
 

The ID need to be recovert from the database.

Member Avatar
Mike Askew
Posting Shark
925 posts since Nov 2010
Reputation Points: 132 [?]
Q&As Helped to Solve: 85 [?]
Skill Endorsements: 25 [?]
Featured
 
0
 

Ah fair enough then, also skimmed over the 12. id++; on my first read through. :)

Member Avatar
rehanul.bappy
Newbie Poster
6 posts since Jul 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

The Id Should be Checked with database? if Date,Month,Year is checked with Database the number will be increase by 1.

Member Avatar
Mike Askew
Posting Shark
925 posts since Nov 2010
Reputation Points: 132 [?]
Q&As Helped to Solve: 85 [?]
Skill Endorsements: 25 [?]
Featured
 
0
 

I believe the intention is the last ID would be retrieved from the database and id set to that.

Member Avatar
rehanul.bappy
Newbie Poster
6 posts since Jul 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

But How to do that in code!! will u Reply a code for that!! thnkx

Member Avatar
Mike Askew
Posting Shark
925 posts since Nov 2010
Reputation Points: 132 [?]
Q&As Helped to Solve: 85 [?]
Skill Endorsements: 25 [?]
Featured
 
0
 
  • What database are you using?

  • What's the table name?

  • What's the ID column name?

Member Avatar
rehanul.bappy
Newbie Poster
6 posts since Jul 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I m using Microsoft SQL Server 2005,and the table name is Testing!! and the column name is Loading Record. thanks

Member Avatar
M.Waqas Aslam
Practically a Posting Shark
837 posts since Aug 2011
Reputation Points: 67 [?]
Q&As Helped to Solve: 137 [?]
Skill Endorsements: 4 [?]
Featured
 
0
 

try this

int id ;
sqlconnection con = new sqlconnection("your connection string");
sqlcommand cmd = new sqlcommand();
con.Open();
cmd.Connection = con;
cmd.CommandText = "select  isnull(max(loading_record)) + 1  from testing ";
sqldatareader dr = cmd.executereader();

while(dr.read())
{
id = convert.toInt16( dr["loading_record"].toString());
}
string ID = "PA-"+DateTime.Now.Day+"-"+DateTime.Now.Month+"-"+DateTime.Now.Year+"-"+id;
dr.close()
con.close

i typed this code here so there are some typing errors , but hope this will solve your prob.

Regards

Member Avatar
rehanul.bappy
Newbie Poster
6 posts since Jul 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

yes thanks M.Waqas. But the think is when the day is change from 25 to 26 the number will be 0001. also same for Month and year. i cant do that.can u help me. thanks

Member Avatar
M.Waqas Aslam
Practically a Posting Shark
837 posts since Aug 2011
Reputation Points: 67 [?]
Q&As Helped to Solve: 137 [?]
Skill Endorsements: 4 [?]
Featured
 
0
 

ok , please explain little bit more , if i am worng please correct me ,
you want that your id will start from 1 with each month and same with year ?

But the think is when the day is change from 25 to 26 the number will be 0001. also same for Month and year.

Please explain it .

Regards

Member Avatar
rehanul.bappy
Newbie Poster
6 posts since Jul 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Ok.
My C# Code is for To Autogenerate the Id Number.
Suppose There is a Id Like PA-12-08-0001.Where 12 is The Year,08 is the Month,and 0001 is the Id.If The Month will change, the Id number should be start from 0001 and increment it, Like PA-12-09-0001,PA-12-09-0002. also when year will change the month should be change and the Id will start from 0001 Like PA-13-01-0001,PA-13-01-0002. if the month and year is not changing the ID will be Increment like PA-12-08-0002,PA-12-08-0003 and so on.
I cant do This. Can You Help me in code

Member Avatar
ljbbq
Newbie Poster
1 post since Feb 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 
int id;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string ID = "PA-"+DateTime.Now.Day+"-"+DateTime.Now.Month+"-"+DateTime.Now.Year+"-"+id;
id++;
MessageBox.Show(ID);
}

this is good, but how will i code it, like if you click button1 the d++ will appear in the database is like this PA-12-02-2013-01..as always it will start in 0.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article