Click here to Skip to main content
15,917,702 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
hi!

i have a from in which i have two text boxes
texboxid
texboxname
i want to get auto number in textboxid whenever i want to enter new entry
using sql server 2008
how it will be done
Posted

Don't have an ID entered in the form. Generate it automatically in the table by having the field for ID set up as integer column and as Identity with increment 1 in Column properties in sql2008
 
Share this answer
 
v3
you try this code which retrieve the last row form your table
private int NewId(){
SqlConnection con = new SqlConnection(//your ConnectionStrings);
        SqlDataAdapter adapter = new SqlDataAdapter("Select * from yourtablename", con);
        SqlCommandBuilder cmd = new SqlCommandBuilder(adapter);
        DataSet ds = new DataSet();
        adapter.Fill(ds);
        int ID = 0;
        if (ds.Tables[0].Rows.Count > 0)
        {
            int len = ds.Tables[0].Rows.Count - 1;
            ID = int.Parse(ds.Tables["Arcticle"].Rows[len]["arcId"].ToString()) + 1;
        }
        else
            ID = 1;

return ID;
}


you call above function where you need to new id.
 
Share this answer
 
v2
hi,
pick the last entry from the table and incriment the the count by on eand set it as id.auto incrimen tthe id field
regards,
shefeek
 
Share this answer
 
v2
Comments
Philippe Mori 21-Jul-11 7:23am    
You never knows if someone else add a row in the mean time (particulary on the web). For a new record, you should never display an ID but let SQL pick one when the row is inserted. For existing records, it is generally not that much useful for the end user to see it.
shefeekcm 21-Jul-11 7:30am    
thnks for your information.i know that its only to give an answer to the questioner.
hey! you can use NEWID function. with assigning it will enable you to generate the default value of NEWID(), each new and existing row has a unique value for the associate Column.
Take a look fore more details
NEWID (Transact-SQL)[^]
 
Share this answer
 
Putting the ID into a textbox is ludicrous. Just configure the table's ID column to be auto-incrementing, and the database will fill it in for you when you add a new row of data.
 
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