Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

Rated
Read 120,953 times

Contents

Related Categories

Custom ASP.NET Datagrid Paging With Exact Count - Accessing our Data

DMarko1

Accessing our Data

The first step is of course to query our database, and send our data into our datagrid. The first thing we should concern ourselves with in any .Net page is that we import the necessary namespaces for our app. In this case, as with most data access apps, I'm importing System.Data and System.Data.SqlClient for SQL Server. If you use MS Access or another database then System.Data.OleDb namespace and associated classes will work just fine, providing you modify the connection variables and data adapters.

This importing of namespaces is all done before our script tags like so:

<%@ Import Namespace = "System.Data" %>
<%@ Import Namespace = "System.Data.SqlClient" %>

Now, within our server-side script tags we include our object-oriented stuff – our Subroutine to access our database and bind our result set into our ASP.NET Datagrid. This subroutine, BindSQL() needs to first create all of our variables that we'll use:

Sub BindSQL()
    Dim MyConnection As SqlConnection
    Dim DS as DataSet
    Dim MyCommand As SqlDataAdapter
    Dim RcdCount As Integer

    'Our SQL string
    Dim sqlStr As String = "SELECT titles.title, authors.au_lname, " & _
        "authors.au_fname, titles.price " & _
        "FROM authors INNER JOIN titleauthor ON " & _
        "authors.au_id = titleauthor.au_id " & _
        "INNER JOIN titles ON " & _
        "titleauthor.title_id = titles.title_id"

    'The connection to our database
    Dim strConn As String = "server=(local);uid=sa;pwd=;" & _
        "database=pubs;Trusted_Connection=yes;"

Next we need to instantiate our connection and command object, and the fill our DataSet object with the results of the SQL query:

    ...

    'Open up our connection with our connection object
    MyConnection = New SQLConnection(strConn)

    'To execute our Sql Statement and provide our active connection
    MyCommand = NewSqlDataAdapter(sqlStr, MyConnection)

    'Create instance of DataSet object and fill our predetermined
    'datagrid with it and we name it
    DS = new DataSet()
    MyCommand.Fill(DS, "pubs")

    ...

Now comes the one part that we'll used for our custom paging – the record count, and you'll see it quite different than our classic ASP way.

RcdCount = DS.Tables("pubs").Rows.Count.ToString()

Now that we have this total count of the records in the DataSet, we'll save it to a global variable, since we'll want to access it from other subroutines. The variable ResultCount should be defined in global-scope, as an Integer. (See the complete source later on in this article to note how to create global-scoped variables)

ResultCount = RcdCount

Next, we display the number of records found in a label control:

RecordCount.Text = "<b><font color=red>" & RcdCount & "</font> records found"

Finally, at this point, we can bind our DataSet to the DataGrid and display a label illustrating what page of results we're currently viewing, which will display :

Pubs.DataSource = DS
Pubs.Databind()

lblPageCount.Text = "Page " & Pubs.CurrentPageIndex + 1 & " of " & Pubs.PageCount

At this point, we need to determine if we need to show the Next/Prev links, as well as the First Page/Last Page links:

'Do we want to show the prev/First Page buttons?
If Pubs.CurrentPageIndex <> 0 Then
    Call Prev_Buttons()
    Firstbutton.Visible = true
    Prevbutton.Visible = true
Else
    Firstbutton.Visible = false
    Prevbutton.Visible = false
End If

'Do we want to show the Next/Last Page buttons?
If Pubs.CurrentPageIndex <> (Pubs.PageCount-1) then
    Call Next_Buttons()
    NextButton.Visible = true
    Lastbutton.Visible = true
Else
    NextButton.Visible = false
    Lastbutton.Visible = False
End If

End Sub

That concludes our BindSQL() subroutine, which is a bit lengthy. Don't worry, that's, by far, the most complex piece of our ASP.NET Web page! We still have three more short server-side subroutines to examine, but let's first look at the HTML portion of our ASP.NET Web page, which we'll do on the next page.

Dimitrios, or Jimmy as his friends call him, is a .NET developer/architect who specializes in Microsoft Technologies for creating high-performance and scalable data-driven enterprise Web and desktop applications. Till now Jimmy has authored nearly two dozen .NET articles, published on Dot Net Junkies, 4 Guys From Rolla, Sitepoint, MSDN Academic Alliance, Developers.NET, The Official Microsoft ASP.NET Site, and here on Developer Fusion, covering various unique and advanced techniques on .NET.

Comments

  • Posted by DMarko1 on 08 Jul 2005

    Hi, thanks, I appreciate your compliments.

    I have written another older article that I think should help. It's entitled- [url="http://www.sitepoint.com/article/datagrid-searching-asp-net"]Drilldown...

  • About Custom Paging

    Posted by jmawebco on 08 Jul 2005

    I loved your article on custom paging but would very much like to know how to apply it when you filter your initial data set to retreive specific records. I have a grid with 813 records sorted by user...

  • Paging with exact count and large datasets.

    Posted by mcslayton on 26 Oct 2003

    [url="http://www.seasidecon.com/arsnet/"]Check this site...[/url]

    I too was on the same quest and I've created a best practices reference site that covers many enterprise topics necessary to creat...

  • Posted by Grumly on 17 Oct 2003

    Hello,

    Tankete, I just have try what you are asking for a couple of days ago :

    The principe is simple : returning only a needed page from 50th to 60th records consists in querying this way :

    ...

  • What about large datasets?

    Posted by tankete on 16 Oct 2003

    I was hoping that you would address how to do paging with exact counts while NOT returning the entire dataset from the database, just the current page worth of data. Can you provide any information on...