Click here to Skip to main content
15,887,776 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
stuID lastName firstName sex DOB Phone
st001
st002


i need to Generate ID number Increment Auto , but how ? i problem with String "st" ..
Posted
Updated 21-Feb-13 2:59am
v6
Comments
Shubh Agrahari 21-Feb-13 8:53am    
are you using database....?
phanithly 21-Feb-13 8:54am    
yes
CHill60 21-Feb-13 8:59am    
then use an ID field on your database table defined as (e.g.) [ID] [int] IDENTITY(1,1). For display purposes you can format the number as stu000 or whatever
phanithly 21-Feb-13 9:06am    
this format it can be but how to Increase st000 to st001 Automatic ?
BC @ CV 21-Feb-13 9:15am    
It is best to let the DBMS increment the ID with IDENTITY(1,1). If you then need to return the identity of the newly inserted record you can use @@IDENTITY in your stored proc. Read more here.

This is no Java question - and for real not even one for C#.

You should set up an extra key, the stuID is a value, not a counting key. You would end up with a mess if someone deletes the dataset "st001".
 
Share this answer
 
Comments
phanithly 21-Feb-13 9:05am    
but i need to save st001--- but how and i want to Increment this st001 to st002 Auto after Insert Data into Database
TorstenH. 21-Feb-13 9:08am    
check the comment answer of CHill60 & phil.o and combine that with my answer. Anything else will mess up.
phanithly 21-Feb-13 9:12am    
yes , Thank i will Try more.. Thank you ^^
It's better to leave 'st' letters for your Student ID ; there is no point to using a string key rather than an integer auto-incremented one.

So you just have to make your stuID column an integer one, with the IDENTITY property. This way your database system will take care of the creation and uniqueness of your primary keys.
 
Share this answer
 
Comments
phanithly 21-Feb-13 9:10am    
yes .., but My Friend Request me to do Like this
phil.o 21-Feb-13 9:24am    
Did you (or your friend) wonder if this choice will introduce some performance issues ? Maintaining an integer-based index is much more efficient, from a DBMS point of view, than maintaining a string-based one.
The way you want to create this primary key seems to be a design issue to me.
just have some counter and increment with any loop and concatenate it with st, below is the sample in which I am displaying the string into one label.

C#
private void button1_Click(object sender, EventArgs e)
        {
            string Row = "";
            for (int i = 1; i <= 5; i++)
            {
                Row += "st" + i.ToString("000") + System.Environment.NewLine;
            }
            label1.Text = Row;
        }


so result would look like as below and you can using this code to generate ids and use as you like

VB
st001
st002
st003
st004
st005
 
Share this answer
 
v2
Comments
phanithly 21-Feb-13 9:11am    
Thank You i Will try
CHill60 21-Feb-13 10:18am    
I would advise you to use the DBMS method suggested in the other solutions and comments rather than this method
phanithly 21-Feb-13 10:41am    
What that method ?
CHill60 21-Feb-13 12:01pm    
i.e. the database method - an Identity column on the database - see my comments and phil.o and TorstenH's stuff above. (DBMS stands for DataBase Management System)

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