How to bind DataGrid to display only 25 records of a table having more than 1000 records ..? - asp.net

I have a datagrid control which is bound to a table which containing more than 1000 records. And i want to show only 25 records once a time. I have used paging in datagrid. But each time the next page index is set, query is fired again. This takes lots of time. So what is the easiest way to bound data in this case to improve performance..??

I don't recommend using caching since the whole data will be returned to the server anyway for the first time.
You can improve performance by using custom paging queries to the database.
Assuming you're working with at least SQL Server 2005,
Here's a great article for your purpose with different benchmarking results

have you considered caching your data set? then you would only need to query the data if the cache is empty or expired.

When you handle your Page Changed event, you need to pull in the new page information. You will need to create a stored procedure that takes CurrentPageNumber and PageSize as arguments.
This is in addition to any other arguments you already supply when bringing down your data.
In the SP, you fill up a temporary table or table variable (or you can use a CTE) with the data that you are returning, but also the RowNumber.
Based upon your CurrentPageNumber argument, you are able to return all the results between CurrentPageNumber * PageSize and (CurrentPgaeNumber + 1) * PageSize - 1.
Here's a good resource:
How to return a page of results from SQL?

Related

ASP.NET Depending on data number of pages

Right now my website(ASP.NET) has a database where it pulls data from, and the more data enter the longer the page. I am wondering how I can make it so for example once it pulls ten items from database it starts a new page and puts ten more items on the next. Basically like how http://fmylife.com does it.
Thanks
You should take a look att the DataPager Control.
To create a solution that works in the long run, you need to let the paging selection end up in your sql where clause, so that you only retreive the amount of records that you have specified in the PageSize property of the DataPager Control each time you round trip to the database. Otherwise it will lose it's performace when the records in the database increases.

gridview asp.net

I have a gridview which was working fine with a small dataset in development. In production it has to bind to thousands of records, which is making it slower to load. Is there a way to improve performance, like retrieving the data during gridview pageindex changing?
Also chances are you only want to bind it once. So you should (if not already):
if(!IsPostback)
{
DatabindGridLogicHere();
}
This way your GridView will only have to hit the db the first time to get the data.
First and formost turn off ViewState.
You should tell your datasource to take less records and then enable paging in your grid and datasource.
You can enable "AllowPaging" property to true in your GridView and give a page size say 10. Then Write your data retrieval logic to return a batch of data instead of the whole set of data at once.
When you write the SQL query make sure to order it by an ID.
Thus if the page index is 1 you can take the first batch of data by passing page index of 1 and the page size of 10.
Logic will be;
SELECT [RequiredFields]
FROM [YourDataSource]
WHERE (Id>=((PageIndex-1)*pageSize) AND Id<(PageSize*pageIndex)) ORDER BY Id
In the first page it will return first set of entries of those Ids starting from 0 to 9. In the second page it returns entries of those Ids starting from 10 to 19 assuming the pageSize is 10. You can change the page size and the query logic as you wish.
But if sorting is enable then this will not produce accurate results.

ASP.NET DataGridView paging with server-sided paging

I am passing the PageNumber and PageSize to a stored procedure, which returns only that page of data. It also returns a record count what the total number of records would be if I returned all of them at once.
Generally, how do i hook this up to the DataGridView to enable paging?
It seems like the expectation is for the resultset to contain the complete dataset.
Many of the properties that I expect to be able to set, like RecordCount, appear to be read only.
Could someone give me general pointers?
I created a similar solution once, and adjusted a GridView (not sure if you mean a DataGrid or a GridView) to use server-side paging. The code is too much to post here and since there's no option for attachments here, you can download the code from http://www.raskenlund.com/downloads/GridView.zip
Check out the following article which describes the same thing:
http://www.highoncoding.com/Articles/210_GridView_Custom_Paging.aspx

Is it possible to convert this asp to asp.net?

I have been tasked with sifting through the worst classic asp spaghetti i've ever come across.
The script runs a series of recordsets in sequence, getting 1 record at a time. As the record is built it takes the id and passes it to the next loop, which gets data, and passes on the id to the next loop. It then continues in this manner and builds an unordered list, kicking out the required html as it goes.
Here are my efforts so far:
have a class delivering data via sqldatareaders and output these to nested repeaters (this failed due to not being able to loop and get the id)
Have a datatable populated with all the required data, then datatable.select to filter it out.
have 4 datareaders looping and building the ul arraylists (I
couldnt get the id's to match up)
Please can you suggest the best method
Yes of course it can be rewritten in ASP.NET - note that i said rewritten, not just refactored, there is no saving that code (which is okay for what it does, but things are different with ASP.NET).
To be honest, i didn't even check the code, it hurt my eyes. A lot. But in general you can use a nice SQLConnection object, and a SQLCommand, call a stored procedure and get a nice SqlDataReader full of data from which you can build a DataTable or an IEnumerable list of data objects. You then have a repeater type control (ListView, GridView etc) in the UI, simply by binding your datatable or list to that control will render the results.
With the ListView, you specify a template for each data item that is rendered. For a GridView you specify the columns (or column templates) and which properties on each data item the columns should bind to.
When you retrieve the data, you can leave it as a DataTable, or translate it into something else like a list (or array) of data objects. As long as your list/array implements IEnumerable you should be able to just assign the list to the ItemsSource property of the aforementioned repeater control and it will perform its magic.
You don't even have to use a SQLCommand and DataTable object - you could even use Linq to SQL and bind the results straight to your repeater control.
This is just a high level overview of how you could do it, there are a couple of different ways. Once done, your code is going to be way cleaner and more maintainable than the classic ASP code.
Edit: your main issue is how to produce an ordered list, which is what the current code is doing with its nested loops supplying IDs for the detail items.
I would suggest you take a step back, and rethink the sql. In fact, throw the current sql away, it is incredibly inefficient with the tools you have today. It takes literally tenths of a second, and just one database call to return a flat table of data. Your first instinct may be "but i don't want to return too much data!" - relax, even returning several thousand data rows can be a sub 1 second operation if done correctly. You can also restrict the returned data by passing parameters in to the stored proc, or appending them to the dynamic sql statement that you construct (although it pains me hugely to mention dynamic sql, i think it is evil, but some people still use it - i would not recommend it unless it was your only option). To sum up what i am saying, how your data is returned from the database and how it looks on screen are two different things, don't let one guide the other. You can get the data from the database, then manipulate it before rendering it to the UI.
If you still want to show the data as an ordered list, then use a ListView, define a template for each data item, then you can use LINQ to group or filter the data you like (the data item template can contain whatever HTML or ASP.NET controls you like, and that template gets rendered for each data item in the list of data). Alternatively, you could use a GridView, and then use the grouping capability of the GridView to do the work for you - just specify which column(s) you want to group on, and the GridView takes care of the rendering.

asp.net custom datapager

in all the datapager examples i've seen (using a LinqDataSource tied to a ListView) the queries to the database return the full recordset. how can i get JUST the page of rows that i want to display?
for example, if i have a table with 1million rows and i want to see the 10 results on page 5, i would be getting rows 51 to 60.
i'm sure i have to implement custom paging but i haven't found any good examples.
There are many ways of doing this, however, I personally like a SQL based solution that goes to the database and gets the result set. This 4GuysFromRolla Article does a good job of explaining it.
If you're using MSSql2005, take a look at this article.
As you can see, the trick is to use the function ROW_NUMBER(), that allow you to get the sequential number of a row in a recordset. With it you can simply enable pagination based upon the number of rows you want to get in a page.
I was under the impression (from Scott Guthie's blog post "LINQ to SQL (Part 9)") that the LinqDataSource handles the paging for you at the database level:
One of the really cool things to notice above is that paging and sorting still work with our GridView - even though we are using a custom Selecting event to retrieve the data. This paging and sorting logic happens in the database - which means we are only pulling back the 10 products from the database that we need to display for the current page index in the GridView (making it super efficient).
(original emphasis)
If you are using some custom paging, you can do something like this in LINQ to SQL:
var tagIds = (from t in Tags where tagList.Contains(t.TagText) select t.TagID).Skip(10).Take(10).ToList();
This is telling LINQ to take 10 rows (equivalent to T-SQL "TOP 10"), after skipping the first 10 rows - obviously these values can be dynamic, based on the Page Size and page number, but you get the idea.
The referenced post also talks about using a custom expression with a LinqDataSource.
Scott has more information on Skip/Take in Part 3 as well.

Resources