C# .NET - increment a number - Asked By kiran Kumar on 12-Nov-11 08:27 AM


  in my windows forms application project, i have placed one Label control, and one textbox control  and a button.

  For increment the number which was in Label control. I have set the Label Properties as follows:

  Label1--> Properties--->  text --   1
                                        Enabled---> false    so that when form load, first time label will show id as 1 , from then it will increment each time by 1.

  If i click on button[Next] then Id(1) will be saved in database table and Label will display id as 2 , if i click Button[Next] again then id will saved and show id as 3  , like that it will increments each time by 1. For that i wrote the code as follows:

  SqlConnection con = new SqlConnection("user id=sa;password=123;database=empdetails");
            SqlCommand cmd = new SqlCommand("select max(id) as id from employee", con);
            cmd.CommandType = CommandType.Text;
            SqlDataReader dr = cmd.ExecuteReader();
            if (dr["id"].ToString() != "")
                temp = int.Parse(dr["id"].ToString()) + 1;
                temp = 1;
            label24.Text = temp.ToString();


this is working but id is incrementing upto 10 only , after that it is not incrementing to 11,12,13...

Please solve this , i want the id upto 1000 employees
I have created the table with values as id (char)   , table name as employee in empdetails database, i wrote the code for saving the id in the table. 

But id is not incrementing after 10 , please give the solution for incrementing after 10 

dipa ahuja replied to kiran Kumar on 12-Nov-11 09:00 AM
the problem is may be occurring because of the char dataType just change the dataType of ID column either nvarchar or int

Kirtan Patel replied to kiran Kumar on 13-Nov-11 05:22 AM
you dont have to do all that manually to increment id automatically you can do that in database itself instead of calculating manually .

set DataType of table's column to Integer and in Property of Column set Identity = True