Total number of records paged gridview

M

msch-prv

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 ..."
Apts_grd.DataBind()
Response.Write("Count: " + Apts_grd.Rows.Count.ToString

TIA, Mark
 
P

Patrick.O.Ige

-- 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
int.
Hope that helps
Patrick
 
B

Bruno Alexandre

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=;" & _
"database=pubs;Trusted_Connection=yes;"

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

Pubs.DataSource = DS
Pubs.Databind()

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

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,817
Latest member
AdalbertoT

Latest Threads

Top