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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T10:53:59+00:00 2026-06-02T10:53:59+00:00

I have the following class: public class Data { static public SqlDataReader ExecutSql(string sql)

  • 0

I have the following class:

public class Data
{
    static public SqlDataReader ExecutSql(string sql)
    {
        var cmd = new SqlCommand(sql, SqlCon.Conn);
        var data = cmd.ExecuteReader();
        return data;
    }
}

It’s called from an asp.net webpage and I’m getting the error: “There is already an open DataReader associated with this command which must be closed first.”

Clearly I’m instantiating a new SqlCommand the line right before I execute the data reader. I am new to web development (my background is WinForms) but even so I can’t comprehend how I can already have an open DataReader associated with a Command that was literally just created?? I could possibly understand if it was a multi-threading sort of issue, but I’m stepping throught the code in the debugger and getting this error.

Someone want to tell me what I’m missing here?

  • 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-02T10:54:01+00:00Added an answer on June 2, 2026 at 10:54 am

    Did you already called this method before ? Your method is returning an SqlDataReader and i am not sure whether it is being closed properly.As per msdn,

    You must explicitly call the Close method when you are through using
    the SqlDataReader to use the associated SqlConnection for any other
    purpose.

    I would suggest you to read the data from your reader and close the reader and return the new type( a DataTable / DataSet or your custom class filled with properties)

    using(SqlCommand command =new SqlCommand(sql, SqlCon.Conn))
    {
       SqlDataReader reader = command.ExecuteReader();
       while (reader.Read())
       {
         //Fill your object and then use that to return
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following Data class: class CustomerProducts { public string Id { get;
I have the following class: public abstract class AbstractParent { static String method() {
I have the following code: public static class ScraperMasterUriDetails { public static Dictionary<Guid, string>
I have the following code: Imports MySql.Data.MySqlClient Imports MySql.Data.Types Public Class FormMain Private Sub
As a simplified example, I have the following data classes: public class Employee {
I have the following class public class UIControl { public string FName{ get; set;
I have the following class public class CountrySpecificPIIEntity { public string Country { get;
I have a class like following: public class Trainee { private static int numberOfTrainee
I have the following DB helper class: public int studentExists(String studid) { Cursor dataCount
I have following Objectify entity to store data in Google DataStore. public class Record

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.