locked
"Invalid attempt to call IsDBNull when reader is closed" exception while iterating over EF collection RRS feed

  • Question

  • I written some sample code that demonstrates how concurrent transactions works with SQL Server Express 2008.

    I retrieve entire table "Proxy" from the database without setting Shared Locks so I use System.Transactions.IsolationLevel.ReadUncommitted:

                using (var scope = new TransactionScope(TransactionScopeOption.Required,
                    new TransactionOptions() { Timeout = new TimeSpan(0, 30, 0), IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted }))
                {
                    using (MyContainer db = new MyContainer())
                    {
                        var temp_query = from p in db.Proxies select p;
    
                        foreach (Proxy proxy in temp_query)
                        {
                            checkPool.Queue.Enqueue(new CheckJob(proxy.IP, proxy.Port));
                        }
                    }
                }

    Some other "checker" threads reads and modifies Proxy records (added to checkPool.Queue) one by one concurrently with System.Transactions.IsolationLevel.ReadCommitted. The count of checker threads could be 20 or 50 for example and multiple instances of the application could be started.

    "foreach" sometimes throws InvalidOperationException with message "Invalid attempt to call IsDBNull when reader is closed.". The inner exception is null.

    Any ideas? Why does it happen?


    • Edited by Dmitriano1 Tuesday, May 28, 2013 7:43 AM
    Tuesday, May 28, 2013 7:33 AM

Answers

  • It looks like a .NET question. You will get better help in .NET forum.

    http://social.msdn.microsoft.com/Forums/en-US/category/netdevelopment

    However, with very limited knowledge, It looks like some cases, your proc/query return NO data resulting no table available to the Reader which throws the error. You can try create a dataTable object and assign the value from the reader and then return the datatable object.

    The above is just a wild guess as you have not provided the complete info of your function.


    Please use Marked as Answer if my post solved your problem and use Vote As Helpful if a post was useful.

    Tuesday, May 28, 2013 8:28 AM
    Answerer

All replies

  • It looks like a .NET question. You will get better help in .NET forum.

    http://social.msdn.microsoft.com/Forums/en-US/category/netdevelopment

    However, with very limited knowledge, It looks like some cases, your proc/query return NO data resulting no table available to the Reader which throws the error. You can try create a dataTable object and assign the value from the reader and then return the datatable object.

    The above is just a wild guess as you have not provided the complete info of your function.


    Please use Marked as Answer if my post solved your problem and use Vote As Helpful if a post was useful.

    Tuesday, May 28, 2013 8:28 AM
    Answerer
  • Yes, obviously the query returns some closed reader, but table "Proxy" exists in the database and has about 2000 records.
    Tuesday, May 28, 2013 9:30 AM
  • I think its not the question of existence of Proxy table. The result it returns. Can you check what does it return incase there is no data for a search criteria.


    Please use Marked as Answer if my post solved your problem and use Vote As Helpful if a post was useful.

    Tuesday, May 28, 2013 9:37 AM
    Answerer
  • I think its not the question of existence of Proxy table. The result it returns. Can you check what does it return incase there is no data for a search criteria.


    Please use Marked as Answer if my post solved your problem and use Vote As Helpful if a post was useful.

    Latheesh, thank you for all your contributions in this forum! Can you email me? It's edprice at Microsoft.

    Thanks again!


    Ed Price (a.k.a User Ed), SQL Server Customer Program Manager (Blog, Small Basic, Wiki Ninjas, Wiki)

    Answer an interesting question? Create a wiki article about it!

    Thursday, June 6, 2013 4:22 PM