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 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

Related Questions

Just wondering about this code below... when I turn off my internet connection and
Just wondering about some practice about this; I have made a simple visual C#
I just learnt the basics of MySQL and I'm wondering about this: I have
I'm wondering about the correct/easiest/most pythonic way of dealing with subprojects that you want
Just wondering about the performance impact of copying very large php variables. For example
I'm just wondering about large projects - say an airlines reservation system, how many
I was just wondering about something. I have a frame that loads pages and
I'm new to web development and am just wondering about best practices for java
I am using .Net3.5. Just wondering my understanding about the insert, update, delete are
Just wondering how you would go about implementing something similar to stackoverflow'd related questions.

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.