Click here to Skip to main content
15,908,675 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
C#
SqlCommand cmd1 = new SqlCommand("select max(id) from login1",conn);
                      int id1 = Convert.ToInt32(cmd1.ExecuteScalar());
               id1 =id1 + 1;
//Here  id is primary key.but it will not increment after digit 10,it remain as it is
Posted
Updated 31-Mar-11 0:31am
v3
Comments
Mahendra.p25 31-Mar-11 6:29am    
Clear your Question what is the error?

1)id1 is int, which can't limit your count to 10.
2)id column in login1 could possibly be tinyint, which also allows 0 to 255, so no problem.

It makes me think to check if you have any silly constraint on login1.
 
Share this answer
 
v2
The way you're doing this is wrong and is a habit you should get out of right now

It's the job of the database to allocate primary key identifiers, not your code. Trying to get a MAX of a field, then increment by 1 is not the way you should be doing this.

Instead, your field id in table login1 should be set as an Identity (SQL Server)

This means when you add a record to the table, the database engine will automatically assign an ID to the new record, which you can then retrieve and return to your application code.
 
Share this answer
 
hi,

always try to give id column as identity(1,1) it will automatically insert int value 1 to your primay id column and when you insert other record it will increment it by 1 you can also changed that as required
as it will reduce your chances of error.

SQL
create table test(id int primary key identity(1,1),Name varchar(200))
insert into test values ('Pratik')
 
Share this answer
 
Comments
Ankur\m/ 31-Mar-11 7:44am    
Did you mention anything different from the above responder?

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