.NETGURU
records
Messages   Related Types
This message was discovered on ASPFriends.com 'aspngdata' list.


eoghan
-- Moved from [aspngfreeforall] to [aspngdata] by Tim Musschoot <Click here to reveal e-mail address> --

hi
how do i get the number of records returned by
a query in asp and ouput the result: eg.
query returned 50 records ?
thanks - eoghan

Reply to this message...
 
    
Brian W. Spolarich
The problem is that SQLDataReader only provides forward-only access, so there's not an easy way to count the result set before using it.

I found the following article that provides a good solution to this.

<http://www.bipinjoshi.com/displayarticle.aspx?id=105> http://www.bipinjoshi.com/displayarticle.aspx?id=105

Introduction

DataReader allows forwardonly and readonly data access. Many times we need to know the number of records returned after firing a SELECT query. DataReader do not offer this functionality. We can, however, use some trick to quickly and efficiently get this data. This How To is intended to show you just that.

Executing Batch Queries

The Command object can be used to fire batch of queries instead of single query. In this case we can execute action as well as select queries together. We can then call ExecuteReader method of the Command object. There can be multiple result sets obtained and those can be navigated using NextResult method of DataReader. Following code snippet shows how it works:
Dim cnn As New SqlConnection(Global.GetConnectionString)

Dim cmd As New SqlCommand()

Dim dr As SqlDataReader

cnn.Open()

cmd.CommandText = "select count(*) from employees;

select * from employees"

cmd.Connection = cnn

dr = cmd.ExecuteReader

dr.Read()

Label1.Text = dr.GetValue(0)

dr.NextResult()

DataGrid1.DataSource = dr

DataGrid1.DataBind()
Here, we have first declared objects of connection, command and DataReader. Note how the CommandText property of the command object is set to two SELECT queries separated by a ";". Next, the Command is executed and the output is assigned to DataReader. The DataReader contains two result sets matching our queries. First query gives us the number of records. The NextResult method shifts to the next result set and we can bind the actual data with the DataGrid.

-bws

| -----Original Message-----
| From: eoghan [ mailto:Click here to reveal e-mail address]
| Sent: Thursday, July 11, 2002 7:42 AM
| To: aspngdata
| Subject: [aspngdata] records
|
|
| -- Moved from [aspngfreeforall] to [aspngdata] by Tim
| Musschoot <Click here to reveal e-mail address> --
|
| hi
| how do i get the number of records returned by
| a query in asp and ouput the result: eg.
| query returned 50 records ?
| thanks - eoghan
| | [aspngdata] member Click here to reveal e-mail address = YOUR ID
| | http://www.asplists.com/asplists/aspngdata.asp = JOIN/QUIT
| | http://www.asplists.com/search = SEARCH Archives
|
|
Reply to this message...
 
    
Collins, Mike
Well, I don't know about regular asp, but if you really meant asp.net, this
is how I did it.

First I put the returned recordset in to a dataset:
Dim myDataset As New DataSet
Dim recordCount AS Integer

objDataAdapter.Fill(myDataset, "tableName")
recordCount = myDataset.Tables("currDot Net Guruers").Rows.Count.ToString()

tableName above is just a name you give the dataset table. Not a table name
from the database.

Then just assign the record count to a control:

lblRowCount.text = recordCount

-----Original Message-----
From: eoghan [mailto:Click here to reveal e-mail address]
Sent: Thursday, July 11, 2002 7:42 AM
To: aspngdata
Subject: [aspngdata] records

-- Moved from [aspngfreeforall] to [aspngdata] by Tim Musschoot
<Click here to reveal e-mail address> --

hi
how do i get the number of records returned by
a query in asp and ouput the result: eg.
query returned 50 records ?
thanks - eoghan
| [aspngdata] member Click here to reveal e-mail address = YOUR ID
| http://www.asplists.com/asplists/aspngdata.asp = JOIN/QUIT
| http://www.asplists.com/search = SEARCH Archives

***********************************************************************
NOTICE: This e-mail message and all attachments transmitted with it may contain legally privileged and confidential information intended solely for the use of the addressee. If the reader of this message is not the intended recipient, you are hereby notified that any reading, dissemination, distribution, copying, or other use of this message or its attachments is strictly prohibited. If you have received this message in error, please notify the sender immediately by telephone (404-881-7000) or by electronic mail (Click here to reveal e-mail address), and delete this message and all copies and backups thereof. Thank you.
=======================================================

Reply to this message...
 
 
System.Data.DataSet
System.Data.SqlClient.SqlCommand
System.Data.SqlClient.SqlConnection
System.Data.SqlClient.SqlDataReader
System.Web.UI.WebControls.DataGrid
System.Windows.Forms.DataGrid




ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
Ad


Need Dot Net Interview Questions?
Ask ExamGuru, Inc. for advice and help on Passing .Net Interviews
.Net Projects
Best-of-breed application framework for .NET projects, developed by ExamGuru, Inc. and ExamGuru IT
Free .net Help
Commission ExamGuru, Inc. and his team for your next bespoke software project
FogBUGZ
The only bug tracking system carefully crafted with one goal in mind: helping teams create great software.
Awesome Tools
If you don't know about these, you're missing out... IT Certification Questions
IT Interview Questions
Free Oracle 10g Training
MCSE Boortcamp
Cisco Study Guides
Cheap Study Guides
Exact Questions
Dot Net Interview Questions
Oracle OCP
Cheap Travel
Designer Perfumes - Wholesale Prices
Free Programming Tutorials
 
ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
 Copyright © ExamGuru, Inc. 2001-2006
Contact Us - Terms of Use - Privacy Policy - www.dot-net-guru.com - www.examguru.net - www.oraclesource.net - www.itinterviews.net - www.examguru.net/ITCertification