Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old April 8th, 2007, 02:40 AM
Authorized User
 
Join Date: Dec 2006
Location: Kolkata, India
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/
Reply With Quote
  #2 (permalink)  
Old April 8th, 2007, 10:47 PM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
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
Reply With Quote
  #3 (permalink)  
Old April 9th, 2007, 02:26 AM
Authorized User
 
Join Date: Dec 2006
Location: Kolkata, India
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
Reply With Quote
  #4 (permalink)  
Old April 9th, 2007, 03:49 AM
Friend of Wrox
Points: 1,935, Level: 17
Points: 1,935, Level: 17 Points: 1,935, Level: 17 Points: 1,935, Level: 17
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2004
Location: United Kingdom
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.
Reply With Quote
  #5 (permalink)  
Old April 9th, 2007, 11:31 PM
Authorized User
 
Join Date: Dec 2006
Location: Kolkata, India
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
Reply With Quote
  #6 (permalink)  
Old April 10th, 2007, 08:18 AM
Friend of Wrox
Points: 7,395, Level: 36
Points: 7,395, Level: 36 Points: 7,395, Level: 36 Points: 7,395, Level: 36
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Capital Federal, , Argentina.
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
================================================== =========
Reply With Quote
  #7 (permalink)  
Old April 10th, 2007, 12:42 PM
Authorized User
 
Join Date: Dec 2006
Location: Kolkata, India
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
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 12:13 AM.


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