Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9133625
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:31:13+00:00 2026-06-17T08:31:13+00:00

I have this code: // this is managed elsewhere SqlConnection connection =… connection.Open(); //

  • 0

I have this code:

// this is managed elsewhere
SqlConnection connection =...
connection.Open();

// this is one block of code, separate from the above
using( var transaction = connection.BeginTransaction() ) {
   using( var command = connection.CreateCommand() ) {
     command.Transaction = transaction;
     command.CommandText = ...
     using( var reader = command.ExecuteReader() ) {
         if( reader.HasRows ) {
             if( reader.Read() ) {
                 //get data from the reader
             }
         }
     }
}

and this code runs just fine most of the time. Yet sometimes – very rarely – retrieving HasRows yields the following exception:

Invalid attempt to call HasRows when reader is closed.
System.InvalidOperationException
  at System.Data.SqlClient.SqlDataReader.get_HasRows()
  // my code calling HasRows listed here

I’m 99,5% sure the connection is open at that very moment. My code uses HasRows before reading from the reader pretty much like MSDN suggests.

What might be the reason for that exception?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T08:31:14+00:00Added an answer on June 17, 2026 at 8:31 am

    This happens to be unexpected behavior in ExecuteReader() – most likely a bug. Deep inside ExecuteReader() some random error occurs, most likely a network timeout, the connection is closed and then a closed SqlDataReader is returned as if nothing happened. No wonder subsequent call to HasRows yields an exception.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have taken this code from somewhere and adapted it. I have managed to
Well I have this code in my Managed C++/Cli in Visual Studio 2008, I
I have managed to crunch down a several line code to this For Each
So I invoke a thread from managed code this way: Action<EFScmTechnologiesContext, long, long> updateReference
I have this code I'm trying to rewrite to c# from a vbs script.
I have this java code which is a part of a managed bean which
I have this code: FileDownloadHandler handler = new FileDownloadHandler(fileName); Browser.AddDialogHandler(handler); //using (new UseDialogOnce(Browser.DialogWatcher, handler))
I have this code below that I managed to get the names & phone
I have this HTML code: <span class=price>Price: <font class=rosu_12px_bold>2'099<sup>99</sup></font> EUR</span> and i managed to
Telephone Manager give me Null phone number while i have use this code -

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.