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 the following code in my codebehind Page_Load function that sets the default
I have a function that is normally called while looping inside a SqlDataReader, that
Is the SqlClient.SqlDataReader a .NET managed object or not? Why do we have to
Have you guys had any experiences (positive or negative) by placing your source code/solution
Have a n-tire web application and search often times out after 30 secs. How
I have a SQL query that takes less than a second to execute when
I have a search engine that is supposed to search through descriptions of products
I have a jquery example that works when i enter letters but does not
I have a server process that has to execute a lot of database queries,
I have a static DataAccess class with static methods: MyExecuteReader(returns sqldatareader) and MyExecuteXmlReader (returns

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.