Is my gridview paging / sorting inefficient?

I am using a web service that returns a list of products. I created a Grid View programmatically and used a list as my data source. However, I am unable to use the paging / sorting methods as it throws errors as I am not using the ObjectSource control. I've handled paging and sorting manually, but I don't know if I can do it efficiently. When the user clicks on a new page, I handle the page index like this:

protected void gvProductList_PageIndexChanging(object sender, GridViewPageEventArgs e)
{

    userProducts = Data.GetProductList();

    this.gvProductList.PageIndex = e.NewPageIndex;
    this.gvProductList.DataSource = userProducts;
    gvProductList.DataBind();
}

      

However, the database is always called when the pages change. Is there a way to make this more efficient, or is this okay for paging? The same goes for sorting, which uses a web service to get the products and then uses a lambda expression to sort the products according to different columns. Every time this happens, the database is called (to get the list of products again). Am I doing it wrong? I know that I can use sessions and such to store a List, but there can be hundreds of custom objects in the list and tens of thousands of users at any given time.

I should note that the web service is provided by a third party (and therefore their database is called), which means I won't have access to SQL Server itself or any methods to modify the web service.

Thanks for any help.

EDIT: I should mention that the product list is the user's cart. Therefore, it is unique for each user.

General consensus: If the shopping cart does not contain many items, the current method will be fine. However, if caching is required, this can be achieved by using In Proc sessions or storing sessions on SQL Server.

+2


source to share


2 answers


No, this is not efficient as you are returning every record to the underlying database (assuming GetProductList () returns all records). Swapping a GridView simply means that it only displays the number of rows defined in PageSize for a given PageIndex, but that does not affect how many records are actually returned from the data source.



Unfortunately, since you don't have direct access to the database, and the webservice doesn't offer any other methods, your best bet is to cache the data . Fortunately, asp.net makes data caching pretty easy . The Cache object is more like a session, except that you only have one instance per application, not a user.

+2


source


It looks like you are fetching all products from a web service and then sorting and swapping on your web page. This means that you are returning more data from the web service than you need at any given time. Does the web service provide the ability to pass arguments that represent the required page, page size, and sort order? If not, you might want to discuss the possibility of adding these features with a third party providing the service.



If this is not possible, how often is the product list updated? If it is updated relatively infrequently, you can use the ASP.NET cache to store the results of the web service call locally. A cached data source can be set to expire, say, hourly. This way, users viewing the page will have a list of up-to-date trending products.

+1


source







All Articles