title: ADO.Net retrieving autoincrement values for MS Access database in strongly typed DataSets
body: <p>I write a database client application in C# using MS Access as a DB server.</p>
  
  <p>Users insert new rows using a <code>DataGridView</code>. I need to retrieve autoincrement values of the inserted rows on </p>
  
  <pre><code>this.MyTableTableAdapter.Update(MyDataSet.MyTable)
  </code></pre>
  
  <p>operation.</p>
  
  <p>I can't use </p>
  
  <pre><code>this.MyTableTableAdapter.Fill(MyDataSet.MyTable); 
  </code></pre>
  
  <p>after update operation to refresh the entire table, because the position of required inserted record is lost.</p>
  
  <p>So I read <code>docs.microsoft.com</code>, section "Retrieving Identity or Autonumber Values":</p>
  
  <p><a href="https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/retrieving-identity-or-autonumber-values" rel="nofollow noreferrer">https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/retrieving-identity-or-autonumber-values</a></p>
  
  <p>to understand how to do it.</p>
  
  <p>They write a vague description about it:</p>
  
  <blockquote>
    <p>Some database engines, such as the Microsoft Access Jet database engine, do not support output parameters and cannot process multiple statements in a single batch. When working with the Jet database engine, you can retrieve the new AutoNumber value generated for an inserted row by executing a separate SELECT command in an event handler for the RowUpdated event of the DataAdapter.</p>
  </blockquote>
  
  <p>I also found a code that must be performed in the event</p>
  
  <p><a href="https://www.safaribooksonline.com/library/view/adonet-cookbook/0596004397/ch04s04.html" rel="nofollow noreferrer">https://www.safaribooksonline.com/library/view/adonet-cookbook/0596004397/ch04s04.html</a></p>
  
  <pre><code>private void OnRowUpdated(object Sender, OleDbRowUpdatedEventArgs args)
  {
      // Retrieve autonumber value for inserts only.
      if(args.StatementType == StatementType.Insert)
      {
          // SQL command to retrieve the identity value created
          OleDbCommand cmd = new OleDbCommand("SELECT @@IDENTITY", da.SelectCommand.Connection);
  
          // Store the new identity value to the CategoryID in the table.
          args.Row[CATEGORYID_FIELD] = (int)cmd.ExecuteScalar( );
      }
  }
  </code></pre>
  
  <p>The problem is that VS IDE designer creates strongly typed DataSet without external visibility of DataAdapter object.</p>
  
  <p>It creates TableAdapter that is inherited from Component but not from DataAdapter.</p>
  
  <p>And although it creates a real DataAdapter inside the myTableTableAdapter class, it have protected level.</p>
  
  <pre><code>protected internal global::System.Data.OleDb.OleDbDataAdapter Adapter
  </code></pre>
  
  <p>So I can't add any event to the Adapter outside the class myTableTableAdapter.</p>
  
  <p>I assume that the code should be written inside the myTableTableAdapter class 
  but the code of this class is autogenerated, and file have next comment</p>
  
  <pre><code>//------------------------------------------------------------------------------
  // &lt;auto-generated&gt;
  //     This code was generated by a tool.
  //     Runtime Version:4.0.30319.42000
  //
  //     Changes to this file may cause incorrect behavior and will be lost if
  //     the code is regenerated.
  // &lt;/auto-generated&gt;
  //------------------------------------------------------------------------------
  </code></pre>
  
  <p>So my custom code could be lost if I add any changes.</p>
  
  <p>So my question is - How to add RowUpdated event for the DataAdapter in strongly typed DataSets?</p>