Watching loval variables in Debug mode causes The query results cannot be enumerated more than once RRS feed

  • Question

  • Hello all,

    I am facing another interesting linq to sql in combination with stored procedure phenomenon. I wrote a stored procedure and drag and droped it into the sql designer which generated the wrapper code.

    I can run the example without any error. But when i debug the example and expand the local result variable:

    -        result    {System.Data.Linq.SqlClient.SqlProvider.ExecuteResult}    System.Data.Linq.IExecuteResult {System.Data.Linq.SqlClient.SqlProvider.ExecuteResult}

    +        Results View    Expanding the Results View will enumerate the IEnumerable   

    I get a System.InvalidOperationException {"The query results cannot be enumerated more than once."} when binding the result to a gridview.

    This is super strange because i don't change my code at all. My only clue is that there is probably something like a time constraint, and i just need too long watching the variables.

    Thanks for any help for further diagnostics!
    Monday, August 4, 2008 12:47 PM


  • When you try to watch the results, you are causing the iterator to fetch the results from the underlying data reader. Since the datareader is a forward only cursor, you can't reset the cursor and iterate over it a second time. If you want to use the results of the stored proc in a watch window and also in your code, you need to cast the results to a list or array. At that point, your results will be stored in memory and you can move around them at will.


    Jim Wooley

    Monday, August 4, 2008 3:40 PM