I am trying to ensure the interaction of my DataGrid with SQLite database with standard, such as mechanics:

private void mainTabControl_Loaded(object sender, RoutedEventArgs e) { MainAdapter = new SQLiteDataAdapter("SELECT * FROM Main", _connection); MainCommandBuilder = new SQLiteCommandBuilder(MainAdapter); MainAdapter.Fill(MainTable); mainGrid.DataContext = MainTable.DefaultView; FillMainDeptComboBox(); MainTable.TableNewRow += (o, args) => { MainAdapter.Update(MainTable); }; MainTable.RowChanged += (o, args) => { MainAdapter.Update(MainTable); }; } 

Edited lines are regularly translated into DB. Problems arise when a new row is created and added to the DataGrid, and then when I try to edit it, I get: System.Data.DBConcurrencyException. Obviously, this is due to the presence in the table of the bd column "id" with auto-increment. I have a rough idea of ​​how to manually drop this value into the DataTable, but first I would like to know if there are any prepared tools for this?

  • Doesn't it seem to you that if you load the database into memory in the designer of the control, then your application will brakly shamelessly? (And then they complain that modern programs are slower than old ones.) - VladD
  • one
    @VladD, I agree with you, it is. It’s just that during the debriefing I find it much more convenient to select only the target section of the code that is guaranteed to cause a problem, regardless of the presence of Task'ov or other means of eliminating sticking. - Pavel
  • Understood thanks! This is the first in my memory a good and constructive response to such a remark. - VladD
  • @Pavel after calling MainAdapter.Fill(MainTable); what value returns MainTable.Columns["id"].AutoIncrement ? - Stack

1 answer 1

The question is quite interesting. First of all, in sqlite autoincrement is disabled by default. Do you have it turned on? )

To use auto-increment id, you usually do not need to pass anything to the Id field when inserting records. You may have a different value from the “Default ID”

From the SQLite documentation:

I / O AUTOINCREMENT keyword imposes extra CPU, memory card, disk space, and disk space. It is usually not needed.

In SQLite, it’s an alias for the ROWID (except in WITHOUT ROWID tables) which is a 64-bit signed integer.

If it’s a rule, it’s not given that it’s a rule, if it’s a rule, it’s not so much a rule. This is true regardless of whether or not the AUTOINCREMENT keyword is used.

If the AUTOINCREMENT keyword appears to be allowed, then it’s possible. In other words, it is not possible to prevent rows from ROWIDs from previously deleted rows.