title: Casting from ExecuteScalar on OleDbCommand in C#
body: <p>I am receiving the following error from my code:</p>
  
  <p>The following errors occurred: </p>
  
  <blockquote>
    <p>System.Data.OleDb.OleDbException (0x80040E07): Data type mismatch in criteria expression.
       at System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResult hr)
       at System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object&amp; executeResult)
       at System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object&amp; executeResult)
       at System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object&amp; executeResult)
       at System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method)
       at System.Data.OleDb.OleDbCommand.ExecuteScalar()
       at NJDataExtractExceltoAccess.Form1.FindAcctId(String x, OleDbConnection y) in C:\Path\Form1.cs:line 158</p>
  </blockquote>
  
  <pre><code>private int FindAcctId(string x, OleDbConnection y)
  {
      //try
      //{
          string[] ID = x.Split('-');
  
          string lookup = "Select Id from tbl_name where Col1 = '110' And Col2 = " + ID[0] + "And Col3 = " + ID[1] + "And Col4 = " + ID[2] + "And Col5 = " + ID[3] + "And Col6 = " + ID[4] + "And Col7 = " + ID[5] + "And Col7 = " + ID[6];
          OleDbCommand cmd = new OleDbCommand(lookup, y);
          Int32 result = (Int32)cmd.ExecuteScalar();  // line 158
          Return_val = (int)result;
      //}
      //catch (Exception Er)
      //{
      //    OutputBox.Text = "The following errors occurred: " + Er;
      //}
      return Return_val;
  }
  </code></pre>
  
  <p>The Microsoft page says this:</p>
  
  <blockquote>
    <p>A typical ExecuteScalar query can be formatted as in the following C# example:  </p>
  </blockquote>
  
  <pre><code>cmd.CommandText = "SELECT COUNT(*) FROM dbo.region";
  Int32 count = (Int32) cmd.ExecuteScalar();
  </code></pre>
  
  <p>I have also tried: </p>
  
  <pre><code>int result = (int)cmd.ExecuteScalar();
  </code></pre>
  
  <p>and </p>
  
  <pre><code>object result = cmd.ExecuteScalar();
  </code></pre>
  
  <p>but I receive the same datatype error at the same line.  How do I cast from <code>ExecuteScalar()</code> on an <code>OleDbCommand</code> object (the connection and insert string work)?</p>