ASP.NET - How to increase ID by +1 - Asked By Ajay Paritala on 10-Feb-12 11:50 AM

How to increment table id based on last high value if we don't have identity for primary key
kalpana aparnathi replied to Ajay Paritala on 10-Feb-12 12:06 PM
hi,


private void getid()

    {
     tot = ds.Tables[tableindex].Rows.Count - 1;
      i = tot;
      int id = Convert.ToInt32(ds.Tables["tablename"].Rows[i][0].ToString());
      txt1.Text = (id + 1).ToString();
    }
Suchit shah replied to Ajay Paritala on 10-Feb-12 12:12 PM
In this case you can do it like below

1) get the highest inserted ID

select max(id) from table

2) increment that id by 1

say you take value in count variable then do count = count +1

3) now when inserting the row at that assign new counter value so you insert new value

Counter = (select max(ix) from table)
Counter = Counter + 1
insert into (ID,Name) value (counter,"test")

In  this way you can do it
dipa ahuja replied to Ajay Paritala on 10-Feb-12 01:08 PM
Way 1 : set the primary key field as identity column this way:

Way 2: Get the id everytime and increase it:

SqlConnection conn = new SqlConnection("Connstring");
SqlCommand comm = new SqlCommand("Select MAX(id) from table1", conn);
conn.Open();
 
int id = int.Parse(comm.ExecuteScalar().ToString());
    
conn.Close();
//increament id
id = id + 1;

Ajay Paritala replied to Suchit shah on 10-Feb-12 03:46 PM
Thanks....
Suchit shah replied to Ajay Paritala on 10-Feb-12 11:41 PM
YOU ARE WELCOME
Web Star replied to Ajay Paritala on 11-Feb-12 01:10 AM
If your column are not atuo increase than than you need to get max id and increse it by 1 but make sure that column should be int datatype only

Select @nextId = Max(id) + 1 From tblname