title: Figure out Last_Insert_Id() Using c# and MySql stored Procedure (in Data Access Layer MVC )
body: <p>I've got an  error when i'm using MySql Stored Procedure and<br>
  Data Access Layer (MVC)have a method used to read data from database using <code>MySqlDataAdapter</code> and <code>MySqlParameter</code> and <code>DataTable</code>  to read data called <code>storedata</code>i was tested  this method before in a login controller and it's work as well : the main error here when i need to store last accountId using MySql <code>last_insert_id()</code> function but it always returns null value i tried to Convert it to int then add 1 in every time form opening , but that is useless because it's returns an error </p>
  
  <blockquote>
    <p>System.ArgumentException: 'Parameter 'accountId' not found in the collection.'</p>
  </blockquote>
  
  <p>here is the method in Data Access Layer that i use it to store data from database </p>
  
  <pre><code> public DataTable storeData(string trChannel,MySqlParameter[] list_OF)
      {
              InitializeDb();// Database configration method 
              MySqlCommand Transmeter = new MySqlCommand
              {
                  CommandType = CommandType.StoredProcedure,
                  CommandText = trChannel,
                  Connection = dbcon  // dbcon its Connection string comming from DAL
              };
  
  
              if (list_OF != null)
              {
                  Transmeter.Parameters.AddRange(list_OF);
              }
  
  
              MySqlDataAdapter massenger = new MySqlDataAdapter(Transmeter);
  
              DataTable _mainContainer = new DataTable();
              massenger.Fill(_mainContainer);
              disConnect();
              return _mainContainer;
  
          }
  </code></pre>
  
  <p>The stored procedure that i used it like this after i tried to use last_insert_id() and i failed so i change the sp to this and its work and give me result  when i call it inside MySQL Server </p>
  
  <pre><code>CREATE DEFINER=`root`@`localhost` PROCEDURE `getAccountId`(out accountId int(5))BEGIN select Max(acotId)from accounts;set @accountId=last_insert_id(acotId); END
  </code></pre>
  
  <p>In this case  the server going throw an error </p>
  
  <blockquote>
    <p>Unknown "acotId" in fields list</p>
  </blockquote>
  
  <p>but i don't need to fix it because it's give me a result , so when i called this procedure inside application using a method that return a <code>DataTable</code> value give the first error that i wrote it before <code>System.ArgumentException: 'Parameter 'accountId' not found in the collection.'</code> for the record i got more than five methods contains the same error most of them with input or output parameter and most of them without ..</p>
  
  <pre><code>  public DataTable getAcotId()
      {
          DataTable pdbContainer = new DataTable();
          pdbContainer = _socket.storeData("getAccountId",null);//_socket it's a link to data access layer
          return pdbContainer;
      }
  </code></pre>
  
  <p>I tried also method like this with a parameter and it's also didn't works </p>
  
  <pre><code>    public DataTable getCusttId()
      {
          MySqlParameter[] parCut = new MySqlParameter[1];
          parCut[0] = new MySqlParameter("?custId", MySqlDbType.Int16, 5) ;
          parCut[0].Direction = ParameterDirection.Output;
          DataTable pdbContainer = new DataTable();
          pdbContainer = _socket.storeData("getCustomerId", parCut);
          return pdbContainer;
      }
  </code></pre>
  
  <p>Finally i want to convert the result from this methods to int and i think about using this code </p>
  
  <pre><code>int customerFinalId = getCustId.Rows.Fields[i].Feilds&lt;int&gt;("custId")
  </code></pre>
  
  <p>That is all guys and i'm dire to need help immediately for more necessary </p>
  
  <p>Thank you a lot for helping me ..    </p>