title: Is mixing ADO.NET and LINQ-TO-SQL bad? My data layer isn't working
body: <p><strong>UPDATE</strong></p>
  
  <p>As Mathias notes below, this exact problem has been reported and resolved here:
  <a href="https://stackoverflow.com/questions/318716/asp-net-mvc-iis6-error-on-high-traffic-specified-cast-is-not-valid">ASP.NET-MVC (IIS6) Error on high traffic: Specified cast is not valid</a></p>
  
  <p><strong>ORIGINAL POST</strong></p>
  
  <p>This may be too specific a debugging issue to be posted here, but I'm posting it anyway in the hopes that it produces a solution that others find useful.</p>
  
  <p>I have a web application that operates under moderate load -- maybe 5 requests per second. It has some older code talking to Sql via ADO.NET + DataReaders and has been using this same technique for at least five years without problem. It also has some newer code using LINQ-to-SQL. Both techniques use the same connection string to maximize connection pool reuse.</p>
  
  <p>Recently I'm experiencing a very weird behavior described by these symptoms:</p>
  
  <ol>
  <li><p>Everything will work perfectly for about a day, then suddenly every call (or nearly every call) to the data layer (both ADO.NET and LINQ) returns data that cannot be parsed by my code -- I'll get exceptions like "Unable to cast object of type 'System.Int32' to type 'System.String'." or "Sequence contains no elements" or "IndexOutOfRangeException" or "Invalid attempt to call Read when reader is closed".</p></li>
  <li><p>Interestingly, I never get exceptions from SqlCommand.ExecuteReader() or DataReader.Read() -- the exceptions only occur when I try to parse the IDataRecord that is returned.</p></li>
  <li><p>I am able to fix the problem temporarily by restarting either Sql or IIS. After a few hours it comes back again.</p></li>
  <li><p>I've tried monitoring the number of connections in the connection pool and it never goes above 3 or so. Certainly never above 100.</p></li>
  <li><p>I'm not getting anything in the event log that indicates any problem with Sql or IIS.</p></li>
  <li><p>The drive has 9 GB empty space.</p></li>
  <li><p>I suspected bad RAM, but the server is using registered ECC DIMMs.</p></li>
  <li><p>I have other applications using ADO.NET that work fine and never exhibit the problem.</p></li>
  <li><p>When the problem is occurring I can call the exact same stored procedures via Management Studio and they return the correct, expected results.</p></li>
  </ol>
  
  <p>Here is my pattern for ADO.NET access:</p>
  
  <pre><code>using (var dbConn = Database.Connection) // gets already-open connection
  {
    var cmd = new SqlCommand("GetData", dbConn);
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.AddWithValue("@id", id);
  
    SomeDataObject dataObject = null;
  
    var dr = cmd.ExecuteReader(CommandBehavior.CloseConnection | CommandBehavior.SingleRow);
  
    if (dr.Read())
      dataObject = new SomeDataObject(dr);
  
    dr.Close();
  
    return dataObject;
  }
  </code></pre>
  
  <p>Theory: Is it possible that the combination of ADO.NET in one part of the code and LINQ in another part of the code, both using the same connections from the connection pool, is having some weird side-effect?</p>
  
  <p>Question: Are there any debugging steps I should be trying? Any events logs or performance metrics that might help?</p>