Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all

I want to generate auto number corresponding to primary keys

like

if pk is 1 the no will be 000001

if pk is 10 :000010

if 100: 000100

the total digits in a number should be same..how to code for it in c#?


Thanks in advance
Posted

All you need to do is add formatting information to your ToString call:
C#
int i = 100;
Console.WriteLine(i.ToString("D6"));

Alternatively, you could do it with String.Format:
C#
int i = 100;
Console.WriteLine(string.Format("{0:D6}", i));
 
Share this answer
 
Which database are you using? You might want to check if you can do something similar in the database directly.
 
Share this answer
 
Some rdbms engines like MS SQL use IDENTITY[^]

While others use sequences[^]

It's usually a good idea to let the rdbms generate the key for you to avoid concurrency issues because many users access and change data in a relational database, the database manager must be able both to allow users to make these changes and to ensure that data integrity is preserved.

Concurrency refers to the sharing of resources by multiple interactive users or application programs at the same time.

Regards
Espen Harlinn
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900