Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I am working on a windows application with SQL 2005 as backend.
I want to auto generate numbers and show it in textbox
I want to generate the numbers in sequence and save it in DB.

Thanks in Advance !!!
Posted
Updated 28-Nov-12 1:16am
v2
Comments
Mohd. Mukhtar 28-Nov-12 6:57am    
What kind of number you want to generate?

Is it random number or
You want to generate number for primary key column to store in DB.
Ank_ush 28-Nov-12 7:16am    
Yes, I want to generate the numbers in sequence and save it in DB.

If you want to save the value into primary column then you just need to do the setting into SQL server.

1. Right click on table name.
2. Select the column name
3. Go into property window
4. Double click on identity specification.
5. Save the table
Now when you run any insert query you dont need to provide this column value it will automatically incremented.

If you want custom sequence number from C# application then you need to execute select query to fetch the last inserted record id with scope_Identity() and need to increment the value.

refer the below link.

http://msdn.microsoft.com/en-us/library/ms190315.aspx[^]
 
Share this answer
 
you can try this code... it may help you to get some idea...

C#
private static Random rnd;
static void random()
{
    rnd = new Random();
}

private string GenerateId()
{

    string allowedChars = "ABCDEFGHJKLM0123456789NOPQRSTUVWXYZ";

    char[] chars = new char[6];

    for (int i = 0; i < 6; i++)
    {
        chars[i] = allowedChars[rnd.Next(0, allowedChars.Length)];
    }

    return new string(chars);

}
 
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