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

  • Home
  • SEARCH
  • 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 526713
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:48:22+00:00 2026-05-13T08:48:22+00:00

I was just wondering about the correct way to return a reader from a

  • 0

I was just wondering about the correct way to return a reader from a class?

My code below works, but I’m unsure if this is correct.

Also. I can’t close the the connection in my class method and still access it from my ascx page, is

that OK?

// In my class I have the following method to return the record/reader — it’s a single record in this case.

public SqlDataReader GetPost()
    {
        SqlConnection conn = new SqlConnection(connectionString);
        SqlCommand cmd = new SqlCommand("con_spPost", conn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@blogid", blogid);
        try
        {
            conn.Open();
            return cmd.ExecuteReader();
        }
        finally
        {
          //  conn.Close();
        }
    }

//I then call the GetPost method in my ascx page like so:

protected void Page_Load(object sender, EventArgs e)
{

    //instantiate our class
    MyClass DB = new MyClass();

    //pass in the id of the post we want to view
    DB.PostID = Int32.Parse(Request.QueryString["p"]);

    ///call our GetPost method
    SqlDataReader reader = DB.GetPost();

   //output the result
    reader.Read();
    this.viewpost.InnerHtml = "<span id='post1_CreatedDate'>" + reader["CreatedDate"].ToString() + "</span><br>";
    this.viewpost.InnerHtml += "<span class='blogheads'>" + reader["BlogTitle"].ToString() + "</span><p><p>";
    this.viewpost.InnerHtml += reader["BlogText"].ToString();
    reader.Close();
}

I’d appreciate any comments on my code or tips, thanks.

Melt

  • 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-13T08:48:22+00:00Added an answer on May 13, 2026 at 8:48 am

    Generally speaking it’s fine to return a reader from a method, but the reader’s consumer needs to take control of all the disposable objects that will be used during the reader’s lifetime.

    To do this, you’d pass an IDbConnection into the GetPost method, then make sure your caller disposes both the connection and reader. The using keyword is the most convenient way to do this:

    protected void Page_Load(object sender, EventArgs e) {
    
        // Create the DB, get the id, etc.    
    
        using (IDbConnection connection = new SqlConnection(connectionString))
        using (IDataReader reader = DB.GetPost(connection)) {
            reader.Read();
            this.viewpost.InnerHtml = reader["BlogText"].ToString();
            // finishing doing stuff with the reader  
        }
    }
    

    As others have pointed out, this starts to clutter your application’s presentation layer with way too much data-access infrastructure – so it isn’t appropriate here. Until you find yourself with a performance problem or need to display an unreasonable amount of data, you shouldn’t be handling data readers in your presentation layer. Just make DB.GetPost return a string, and encapsulate all the data-access code in there.

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

Sidebar

Ask A Question

Stats

  • Questions 325k
  • Answers 325k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If you're on GNU ls, you can pass --time-style=long-iso. More… May 14, 2026 at 1:27 am
  • Editorial Team
    Editorial Team added an answer You have to set inside as precompiled code the parameters… May 14, 2026 at 1:27 am
  • Editorial Team
    Editorial Team added an answer The JDK is tested , at least now, using jtreg… May 14, 2026 at 1:27 am

Related Questions

Was looking to get peoples thoughts on keeping a Lucene index up to date
I was thinking about this the other day and wanted to see what the
I have written a script that goes through a bunch of files and snips
I was just fooling around with some Domain Specific Language designs for a new
Ok, I'm programming in objective-C and using Xcode. I have read through the documentation

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.