title: Get the id of inserted row using C#
body: <p>I have a query to insert a row into a table, which has a field called ID, which is populated using an AUTO_INCREMENT on the column. I need to get this value for the next bit of functionality, but when I run the following, it always returns 0 even though the actual value is not 0:</p>
  
  <pre><code>MySqlCommand comm = connect.CreateCommand();
  comm.CommandText = insertInvoice;
  comm.CommandText += "\'" + invoiceDate.ToString("yyyy:MM:dd hh:mm:ss") + "\', " + bookFee + ", " + adminFee + ", " + totalFee + ", " + customerID +  ")";
  int id = Convert.ToInt32(comm.ExecuteScalar());
  </code></pre>
  
  <p>According to my understanding, this should return the ID column, but it just returns 0 every time. Any ideas?</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>When I run:</p>
  
  <pre><code>"INSERT INTO INVOICE (INVOICE_DATE, BOOK_FEE, ADMIN_FEE, TOTAL_FEE, CUSTOMER_ID) VALUES ('2009:01:01 10:21:12', 50, 7, 57, 2134);last_insert_id();"
  </code></pre>
  
  <p>I get:</p>
  
  <pre><code>{"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'last_insert_id()' at line 1"}
  </code></pre>