Start New Discussion Reply to this Discussion How to Create Autogenerate ID in C#
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
Related Article: Autogenerate ID
is a C# discussion thread by Farhad.idrees that has 2 replies and was last updated 8 months ago.
rehanul.bappy
Newbie Poster
6 posts since Jul 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 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);
}
superjj
Light Poster
43 posts since Nov 2010
Reputation Points: 10
Solved Threads: 2
Skill Endorsements: 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.
MikeyIsMe
Master Poster
798 posts since Nov 2010
Reputation Points: 88
Solved Threads: 69
Skill Endorsements: 10
The ID need to be recovert from the database.
superjj
Light Poster
43 posts since Nov 2010
Reputation Points: 10
Solved Threads: 2
Skill Endorsements: 0
Ah fair enough then, also skimmed over the 12. id++; on my first read through. :)
MikeyIsMe
Master Poster
798 posts since Nov 2010
Reputation Points: 88
Solved Threads: 69
Skill Endorsements: 10
The Id Should be Checked with database? if Date,Month,Year is checked with Database the number will be increase by 1.
rehanul.bappy
Newbie Poster
6 posts since Jul 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0
I believe the intention is the last ID would be retrieved from the database and id set to that.
MikeyIsMe
Master Poster
798 posts since Nov 2010
Reputation Points: 88
Solved Threads: 69
Skill Endorsements: 10
But How to do that in code!! will u Reply a code for that!! thnkx
rehanul.bappy
Newbie Poster
6 posts since Jul 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0
MikeyIsMe
Master Poster
798 posts since Nov 2010
Reputation Points: 88
Solved Threads: 69
Skill Endorsements: 10
I m using Microsoft SQL Server 2005,and the table name is Testing!! and the column name is Loading Record. thanks
rehanul.bappy
Newbie Poster
6 posts since Jul 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 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
M.Waqas Aslam
Master Poster
748 posts since Aug 2011
Reputation Points: 50
Solved Threads: 121
Skill Endorsements: 2
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
rehanul.bappy
Newbie Poster
6 posts since Jul 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 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
M.Waqas Aslam
Master Poster
748 posts since Aug 2011
Reputation Points: 50
Solved Threads: 121
Skill Endorsements: 2
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
rehanul.bappy
Newbie Poster
6 posts since Jul 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 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.
ljbbq
Newbie Poster
1 post since Feb 2013
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0
© 2013 DaniWeb® LLC
Page rendered in 0.1000 seconds
using 2.67MB