Wrox Programmer Forums
|
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 8th, 2007, 02:40 AM
Authorized User
 
Join Date: Dec 2006
Posts: 36
Thanks: 1
Thanked 0 Times in 0 Posts
Send a message via Yahoo to deb1980
Default Record count

Hi,

How do i count the recordcount returned by a SQL in aspx?
The code that i used is:

query2 = "select * from bank_master order by bank_id"
cmd2.CommandType = CommandType.Text
cmd2.Connection = MyConnection
cmd2.CommandText = query2
rs2 = cmd2.ExecuteReader
If rs2.HasRows Then
Dim count = rs2.RecordsAffected
Response.Write(count.ToString)
%>
<table>
<%
    While rs2.Read()
    %>
    <tr>
    <td>
    <%=rs2(1)%>
    </td>
    </tr>
    <%
    End While
%>
</table>
<%
Else
Response.Write("No banks yet registered in the system")
End If


-------------------------
It returns -1

Any help?



---
Best regards,

Debashis
__________________
---
Best regards,

Debashis
http://debashis80.spaces.live.com/
 
Old April 8th, 2007, 10:47 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

A datareader is a "connected" data object. This means it is constantly connected to the database as long as it's open in the code. This makes it faster, but has the drawback that it only gets the data that is required as it is requested. As a result, you can't ask it how many records there are for a given query because it doesn't know. It only knows that it has reached the end of the requested set when it asks the database for the next record (DataReader.Read()) and that method call returns false.

In order to get a complete set of data and be able to determine the number of rows, you need to work with a "disconnected" data object, such as the DataTable. A DataTable has a rows collection which has a count property.

-Peter
 
Old April 9th, 2007, 02:26 AM
Authorized User
 
Join Date: Dec 2006
Posts: 36
Thanks: 1
Thanked 0 Times in 0 Posts
Send a message via Yahoo to deb1980
Default

Hi Peter,

Thanks for the response.

Is it possible toi provide me with a snippet code, so that I can move forward with that?



---
Best regards,

Debashis
 
Old April 9th, 2007, 03:49 AM
Friend of Wrox
 
Join Date: Aug 2004
Posts: 550
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hi

A simple way of filling a dataset and determining the recordcount of default datatable is below:

SqlConnection sConn = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
            string sQuery = "spTest";

            sConn.Open();
            SqlCommand sqlCmd = new SqlCommand(sQuery,sConn);
            sqlCmd.CommandType = CommandType.StoredProcedure;
            SqlDataAdapter da = new SqlDataAdapter(sqlCmd);

            DataSet ds = new DataSet();
            da.Fill(ds);

            Response.Write(ds.Tables[0].Rows.Count);

Regards
Mike

Fortune favours the brave, so don't regret on missed oppurtunities.
 
Old April 9th, 2007, 11:31 PM
Authorized User
 
Join Date: Dec 2006
Posts: 36
Thanks: 1
Thanked 0 Times in 0 Posts
Send a message via Yahoo to deb1980
Default

Thanks Mike,

It does help ! :)

---
Best regards,

Debashis
 
Old April 10th, 2007, 08:18 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

better than that, just do a select count(*) on the numbers of rows so you don't need to populate a dataset...

HTH

Gonzalo

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from dparsons signature and he Took that from planoie's profile
================================================== =========
My programs achieved a new certification (can you say the same?):
WORKS ON MY MACHINE
http://www.codinghorror.com/blog/archives/000818.html
================================================== =========
 
Old April 10th, 2007, 12:42 PM
Authorized User
 
Join Date: Dec 2006
Posts: 36
Thanks: 1
Thanked 0 Times in 0 Posts
Send a message via Yahoo to deb1980
Default

Hi Ganzalo,

I have been using count(*) previously only..
However now I got 2 options.. :)

---
Best regards,

Debashis





Similar Threads
Thread Thread Starter Forum Replies Last Post
record count help jpaul VB Databases Basics 2 January 22nd, 2008 02:49 PM
count no of record sanjeev jha ADO.NET 1 November 5th, 2007 12:28 PM
Count Record prasanta2expert Access 3 November 28th, 2006 10:42 AM
How to count a record when it is not there Mitch Access 1 October 3rd, 2003 03:49 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.