Search code examples
c#sqliteauto-increment

c# - Auto increment in sqlite stops at number 10


i have a Table in sqlite database that contains the column "id" i use this code to auto increment this column when i press a button

con.Open();
        SQLiteCommand cmd = new SQLiteCommand("Select MAX(id)+1 FROM Data", con);

        object obj = cmd.ExecuteScalar();
        int Amount = (obj != null && obj != DBNull.Value) ? Convert.ToInt32(obj) : 1;

        con.Close();

        tId.Text = Convert.ToString(Amount);

everything is fine until there is 10 records in the tables , then it refuses to retrieve the id number "11"


Solution

  • You can try this updated code of your one :

    con.Open();
    SQLiteCommand cmd = new SQLiteCommand("Select MAX(cast(id as int))+1 FROM Data", con);
    
    object obj = cmd.ExecuteScalar();
    int Amount = (obj != null && obj != DBNull.Value) ? Convert.ToInt32(obj) : 1;
    
    con.Close();
    
    tId.Text = Convert.ToString(Amount);