Click here to Skip to main content
15,890,609 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How to create auto generated Product Id in textbox and store in database ??
Posted

why you need a auto Generated id?. I think you work done sql Automatically,
SQL
CREATE TABLE [dbo].[Table_Name](
    [id] [int] NOT NULL, ///this id will automatically increment.
    [role] [varchar](20) NULL,
    [password] [varchar](20) NULL,
 CONSTRAINT [PK_jogi2] PRIMARY KEY CLUSTERED
(


For any query hit reply option.
 
Share this answer
 
Unless your auto generated id must follow a given pattern, it is a good idea (as already suggested) to make the database itself generate it for you.

If you really need to do that in code then you have to save the last generated id (or the whole set of generated ones, if you aren't generating a sequence) to prevent creating duplicates.
 
Share this answer
 
Did you mean coding for these answer?
This is my advice for auto generate in C#

con.Open();
string str = @"select count(*) from product";

SqlCommand cmd = new SqlCommand(str, con);
int i = (int)cmd.ExecuteScalar();
i++;
txtNo.Text = i.ToString();

con.Close();
 
Share this answer
 
do you want random product id or auto increment product id....
auto increment product id:
1.your table have column name id datatype maybe int
2.select max(id) table_name
3.display in textbox+1
 
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