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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:03:56+00:00 2026-05-25T18:03:56+00:00

I have SqlDataReader that was sitting in a page’s code behind (well actually a

  • 0

I have SqlDataReader that was sitting in a page’s code behind (well actually a web user control). I’d like to move this code to a separate class which the user control calls. But how do I bind the data from the class to the on page repeater?

Is this an acceptable concept?

Here’s my code moved to a class:

public class ShoppingCart
{
    public ShoppingCart(int ShoppingCartHeadID)
    {
        SqlConnection conn;
        SqlCommand comm;
        SqlDataReader reader;

        conn = new SqlConnection(iceConns.iconn);

        comm = new SqlCommand("Ordering.sl_ShoppingCartContents", conn);
        comm.CommandType = CommandType.StoredProcedure;
        comm.Parameters.Add(new SqlParameter("@shoppingCartHeadID", SqlDbType.Int));
        comm.Parameters["@shoppingCartHeadID"].Value = ShoppingCartHeadID;

        try
        {
            conn.Open();
            reader = comm.ExecuteReader();
            Cart.DataSource = reader; //This ain't gonna work!
            if (reader.HasRows)
            {
                Cart.DataBind(); //This ain't gonna work!
                varCartStatus = true;
            }
            reader.Close();
        }
        catch
        {
            //Error trapping
        }
        finally
        {
            conn.Close();
        }
    }
}

Any help would be greatly appreciated.

  • 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-25T18:03:57+00:00Added an answer on May 25, 2026 at 6:03 pm

    You should create an entity object for the returned data. Then you can bind any control to a List of the object you created.

    Create a Cart object with the properties that map to your database table.
    Then create a class like CartRepository that will have a method called GetCartById(CartId). This method will do the work above, but instead of trying to bind anything here you are simply going to fill your Cart objects. The return a list of those objects and you can bind that list to any control.

    public class Cart
    {
        //Properties from cart - Just using name as an example.
        public string name { get; set; }
    }
    
    public class CartRepository
    {
        public List<Cart> GetById(int cartId)
        {
            List<Cart> carts = new List<Cart>();
    
            // Do all sql work including setting up connection, command, ect.
    
            while(reader.read())
            {
                // Map your reader to your cart object.
                Cart c = new Cart();
                c.name = reader["name"].ToString();
                carts.add(c);
            }
    
            return carts;
        }
    }
    

    Now from your code just bind your control to the List returned from the repository object.

     myControl1.DataSource = CartRepository.GetById(cartId);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have code that looks like the following in a class that extends MembershipProvider
I have a sql query that I execute in code behind that returns data
I have apsx page that has the following simple code: <%@ Page Title= Language=C#
I have a small C# function that takes any generic SQLDataReader and converts it
I have this piece of code in c#: private static void _constructRow(SqlDataReader reader, system.IO.StreamWriter
I have a sqldatareader spitting out SQL data on a page on page load
I have the following code in my codebehind Page_Load function that sets the default
I have windows service that's select some records. And it works like this: using
I have the following JQUERY code that I'm expecting to return 3 json results
I have a base class that contains a SqlDataReader which gets the data using

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.