Total number of records paged gridview

Discussion in 'ASP .Net' started by msch-prv, Aug 23, 2006.

  1. msch-prv

    msch-prv Guest

    Hi, I have a paged gridview (Apts_grd) tied to an Access DataSource
    (Apts_srcGrd). How do I get the total number of data records returned
    by the query?

    The following shows only the number of records per page:

    Apts_srcGrd.SelectCommand = "SELECT * FROM tblApts WHERE ..."
    Response.Write("Count: " + Apts_grd.Rows.Count.ToString

    TIA, Mark
    msch-prv, Aug 23, 2006
    1. Advertisements

  2. -- Call ExecuteNonQuery to send command
    int count = (int)cmd.ExecuteScalar();

    The query in the SqlCommand constructor obtains the count of all records
    from a table. This query will only return a single value. The
    ExecuteScalar method returns this value. Since the return type of
    ExecuteScalar is type object, we use a cast operator to convert the value to
    Hope that helps
    Patrick.O.Ige, Aug 23, 2006
    1. Advertisements

  3. You can do that automatically,
    you always need to count records on the DataSet itself

    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=;" & _

    '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")

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

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

    Pubs.DataSource = DS

    lblPageCount.Text = "Page " & Pubs.CurrentPageIndex + 1 & " of " &
    End Sub
    Bruno Alexandre, Aug 23, 2006
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.