Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: SQL-server-2005 C# .NET4 , +
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 28-Nov-12 0:32am
Ank_ush2.8K
Edited 28-Nov-12 1:16am
v2
Comments
Mohd. Mukhtar at 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 at 28-Nov-12 7:16am
   
Yes, I want to generate the numbers in sequence and save it in DB.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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[^]
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

you can try this code... it may help you to get some idea...
 
 
       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);
 
       }
  Permalink  

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



Advertise | Privacy | Mobile
Web02 | 2.8.140709.1 | Last Updated 28 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid