title: IndexOutOfRangeException (C#) on OleDBDataReader with SQL Count Function
body: <p>Good afternoon,</p>
  
  <p>I'm having a problem with a section of code I'm working on that checks to see if a dataitem of the same name already exists in a database.</p>
  
  <p>The following code throws up an IndexOutOfRangeException</p>
  
  <pre><code>static int checkExists(String checkIf)
      {
          String connectionString = "provider=Microsoft.Jet.OLEDB.4.0;data source=" + @"ffmpegDB.mdb";
          OleDbConnection connection = new OleDbConnection(connectionString);
          connection.Open();
          OleDbCommand command = connection.CreateCommand();           
  
          try
          {
              command.CommandText = checkIf;
  
              OleDbDataReader reader = command.ExecuteReader();
  
              reader.Read();
              try
              {
                  result = (int)reader["Result"];
              }
              catch (InvalidOperationException ioe) { }
  
              reader.Close();
              connection.Close();
          }
          catch (FormatException Fe)
          {}
  
          return result;            
      }
  </code></pre>
  
  <p>..on this line </p>
  
  <pre><code>result = (int)reader["Result"];
  </code></pre>
  
  <p>The function is executed as follows:</p>
  
  <pre><code>if (checkExists("SELECT COUNT(*) AS 'Result' FROM Files WHERE Filename ='"+fileNames[i]+"' AND File_Directory ='"+directories[i]+"'")==0)
  </code></pre>
  
  <p>{</p>
  
  <p>}</p>
  
  <p>etc..</p>
  
  <p>Could this be a problem with OleDB and how it handles SQL statements?
  Perhaps the reader is looking for the column 'Result' literally? </p>
  
  <p>I hope I've provided enough information.</p>
  
  <p>Regards</p>