-1

I was working on a trivia app that gets the questions from a .txt file and then stores them in a database.

I have created a table named "Itemi" to store the question, which type of question is,four possible answers and the right answer.

Everytime I open the app, I delet all the data from the table and then insert it again. I did that to make the app portable. The .txt file is located in the "Debug" folder.

The problme is, although the table is deleted everytime I open the app and isert again the same data, the ID is not changing. Here is the commnad I used to delete the data.

 cmd = new SqlCommand("DELETE FROM Itemi", sqc);
 cmd.ExecuteNonQuery();
 cmd.Dispose();

For example, this is how the id column looks like when I open the app: enter image description here As you can see, the Id is between 33-59.

If I run the app again: enter image description here The Ids changed

Is there a way I can avoid thhis ? I want the Ids to be between 1 and 30. I forgot to mention that the Id column has the primary key.

ChJ16
  • 51
  • 1
  • 9
  • 2
    It's an identity column. You need to set the next ID to be generated. Google "sql server reseed identity column". –  Mar 11 '19 at 17:24
  • 1
    Possible duplicate of https://stackoverflow.com/questions/21824478/reset-identity-seed-after-deleting-records-in-sql-server –  Mar 11 '19 at 17:24
  • 2
    Also, your title is misleading. You didn't delete the database, or even a table. You emptied the table. –  Mar 11 '19 at 17:25
  • Why you need to delete the data ? – Chetan Mar 11 '19 at 17:26
  • Thank you, for the link @Amy. And no intetion for clickbait. – ChJ16 Mar 11 '19 at 17:28

1 Answers1

2

You need to reset the Identity after deleting the data (the query you run).

From this awnser this is the command you need

DBCC CHECKIDENT (table_name [, { NORESEED | { RESEED [, new_reseed_value ]}}])
[ WITH NO_INFOMSGS ]
Omar Martinez
  • 440
  • 6
  • 16