0

i am using linq for my connection to the database and this is my first time to display a large amount of data from the database so i don't how to handle it..can you give me a tip? thanks!

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!Page.IsPostBack)

        rebind();
    }

    private void rebind()
    {
        using ( var db = new linqDataContext())
        {
            GridView1.DataSource = db.Orders.Select(p => new { p.OrderID, p.CustomerID, p.ShipName, p.ShipCity }).ToList();
            GridView1.DataBind();
        }
    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        rebind();
    }
}
Bernard
  • 7,908
  • 2
  • 36
  • 33
handsomeboy
  • 35
  • 1
  • 9
  • 1
    Can't really help you without some example code. How many records are we talking about here? What does your LINQ query look like? – dtown123 Apr 14 '12 at 00:37
  • around 20-50,000 of datas.. actually i didn't started the code yet.. can you give me an example what should it be like ? – handsomeboy Apr 14 '12 at 00:43

4 Answers4

5

Tip: Don't display a lot of data at once in your web application. Use a paging control to retrieve and display only a few rows from the database at a time.

Bernard
  • 7,908
  • 2
  • 36
  • 33
  • when i load the page..the page load slowly..how can i make it faster? – handsomeboy Apr 14 '12 at 01:37
  • @handsomeboy: Don't load a lot of data and other content when the page initially loads. Load only a few rows of data and allow the user to request more through a paging control. – Bernard Apr 14 '12 at 01:40
  • @Bernard that's what i needed! can you give me an example code for that ?or maybe a link!thanks man! – handsomeboy Apr 14 '12 at 01:43
  • @handsomeboy: See [this](http://stackoverflow.com/questions/652594/asp-net-paging-control) question. – Bernard Apr 14 '12 at 01:45
2

Have you thought about paging? You should be able to bind the LINQ results to the Gridview datasource. I believe all you need to do is set the pagesize on the gridview and it will work.

I've done it before but i actually keep track of the page (and pagesize) myself in code behind and had LINQ something like gv.DataSource = (From s In Results Select s).Skip(pageSize * (currPage-1)).Take(pageSize)

TheDPQ
  • 476
  • 1
  • 6
  • 13
  • i already included my code in paging..question is what if the records that i will retrieve will be millions of datas?surely the webpage will be crash..how can i get rid of that ?thanks! i set the allowpaging to true.. – handsomeboy Apr 14 '12 at 01:04
  • You either return a lot of data and have the Gridview handle the paging (looks likes that what you did) or you implement paging yourself and only retrieve the data page you need. The example I posted would only review a single page of information. It skips the # of records times the number of pages you are on, and then only takes the number of records for a single page. You'll hit the SQL far more often but only return the data that you are displaying. – TheDPQ Apr 16 '12 at 15:20
2

For paging through large amount of data, you need to make use of custom paging.

Custom paging ensures that only the precise set of records needed for a particular page of data is retrieved from the database at a time.

The following link explains in detail how to perform custom paging using the ObjectDataSource control: http://msdn.microsoft.com/en-us/library/bb445504.aspx

Anil Mathew
  • 2,590
  • 1
  • 15
  • 15
1

Paging. Look at two amazing JQuery based tables (that handle a whole lot of other stuff, for you, like ordering and filtering):

Joseph Victor Zammit
  • 14,760
  • 10
  • 76
  • 102