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

Recommended Answers

All 14 Replies

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

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.

The ID need to be recovert from the database.

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

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

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

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

  • What database are you using?

  • What's the table name?

  • What's the ID column name?

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

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

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

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

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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.