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 7173641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:52:01+00:00 2026-05-28T15:52:01+00:00

I got a problem with an error that say that my datareader is already

  • 0

I got a problem with an error that say that my datareader is already open.

My code looks like this

public static Users GetByID(int ID, SqlConnection connection)
    {
        SqlCommand command = new SqlCommand("Select Name, Email, LastLogin, FK_Role_ID from Users where ID=@id");
        command.Connection = connection;

        command.Parameters.Add(new SqlParameter("id", ID));

        SqlDataReader reader = command.ExecuteReader();
        if (reader.Read())
        {
            Users user = new Users();
            user.ID = ID;
            user.Name = reader.GetString(0);
            user.Email = reader.GetString(1);
            user.LastLogin = reader.GetString(2);
            user.role = Role.GetRoleByID(reader.GetInt32(3), connection);
            reader.Close();
            return user;
        }
        else
        {
            reader.Close();
            return null;
        }
    }

The error occours in the Role.GetRoleByID saying that the datareader command is alreader open. Which is true, but how do I call Role.GetRoleByID with the information from my reader.

I code in c# and ASP.NET

  • 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-05-28T15:52:02+00:00Added an answer on May 28, 2026 at 3:52 pm

    It looks like your Role.GetRoleByID will try to reuse the connection.

    Options:

    • Fetch the data you need from the SqlDataReader within GetByID, close that reader, and then call Role.GetRoleByID (so you’ve only got one active reader at a time)
    • Enable Multiple Active ResultSets (MARS) – I can’t say I have any experience of that
    • Make each method use a separate connection to reduce the dependencies between the methods. Note that the connection pool will make the opening/closing reasonably cheap.

    I’d go with the first option if I were you – or possibly the last. I’d also use a using statement to close the reader automatically:

    private const string GetUserByIdSql =
        "Select Name, Email, LastLogin, FK_Role_ID from Users where ID=@id";
    
    public static Users GetByID(int ID, SqlConnection connection)
    {
        var sql = ;
        Users user;
        int roleId;
        using (var command = new SqlCommand(GetUserByIdSql, connection))
        {
            command.Parameters.Add(new SqlParameter("id", ID));
            using (var reader = command.ExecuteReader())
            {
                if (!reader.Read())
                {
                    return null;
                }
                user = new Users
                {
                    Name = reader.GetString(0),
                    Email = reader.GetString(1),
                    LastLogin = reader.GetString(2),
                };
                // Remember this so we can call GetRoleByID after closing the reader
                roleID = reader.GetInt32(3);
            }
        }
        user.Role = Role.GetRoleByID(roleID, connection);
        return user;
    }
    

    As a fourth option – why not just perform the join required by GetRoleByID in your existing query? That would mean you’d only need one trip to the database.

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

Sidebar

Related Questions

I've got another problem in the same code... I'm getting this error: initialization method
I got a problem like this (this is html/css menu): Eshop | Another eshop
I have a class that looks like this: class SearchService include Mongoid::Document key :name,
I'm playing around with error handling and got a little problem. I connect with
i got problem with my code and hopefully someone able to figure it out.
I got this an asp.net 3.5 page that and I have few tabs (telerik
I got this problem with TFS, I connected to the wrong network and tried
Ok, so I've got some legacy code from another company that I have to
I'm using Python 2.7.2 on Ubuntu 11.10. I got this error when importing the
Ive got a problem that if I have a template class, which in turn

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.