0

I made a small software with C#. When I add a new record auto-increment doesn't work well like records are 1, 2, 3, 4, 5, 6, then it should be 7 but it goes to 1025, 1026, 1027, then again it goes to 2035, 2036, 2037 it doesn't work well.

Insert code:

DateTime date = DateTime.Now;
mycon.Open();

SqlCommand cmd = new SqlCommand("insert into Bill (Date, Time, Cashier, Amount, Patient) values('" + DateTime.Now + "','" + date.ToString("hh:mm") + "','" + "Ahmad" + "'," + lbltotaldu.Text + ",'" + txtpatient.Text + "')", mycon);
cmd.ExecuteNonQuery();

mycon.Close();
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Farhad paikan
  • 89
  • 2
  • 10
  • Have you restarted your SQL Service or your server ? – Zein Makki Aug 06 '16 at 07:55
  • @user3185569 yes i restart my server – Farhad paikan Aug 06 '16 at 07:57
  • 1
    SQL server does not store data in the order it is inserted. SQL uses a hash. So when you query the database to get data in a particular order you need to use an Order By. – jdweng Aug 06 '16 at 07:57
  • was the database previously used ? – Tharif Aug 06 '16 at 07:58
  • looking in to the `ORDER BY` command – user1234433222 Aug 06 '16 at 07:59
  • [SQL Injection alert](http://msdn.microsoft.com/en-us/library/ms161953%28v=sql.105%29.aspx) - you should **not** concatenate together your SQL statements - use **parametrized queries** instead to avoid SQL injection – marc_s Aug 06 '16 at 09:20
  • It's not an **error** - it's just the way it works in SQL Server 2012 and newer. See this other SO question for a great explanation my Martin Smith: http://stackoverflow.com/questions/14146148/identity-increment-is-jumping-in-sql-server-database – marc_s Aug 06 '16 at 09:21

0 Answers0