473,395 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Getting the record count

lotus18
866 512MB
Hello World

I know how to display or manipulate (add,delete,update,search) records using the OleDbDataReader and OleDbCommand class. But I'm clueless in getting the total record on a query.

In vb6, I know how to do it by using recordset.recordcount. I've just tried using DataTable and OleDbDataAdapter class but I can't make it. Please guide me. BTW, I'm using vb2005.

Rey Sean
Jun 13 '08 #1
8 7279
jeffstl
432 Expert 256MB
A quick answer would be to modify your query slightly to contain a count of the records.

Expand|Select|Wrap|Line Numbers
  1.  
  2. sql = "SELECT COUNT(column) as MyCount, * FROM MyTable"
  3.  
  4. DBReader.Read()
  5.  
  6. NumberOfReceords = DBReader("MyCount")
  7.  
  8. DBReader.Close
  9.  
I am probably missing a better way, but I too am accustomed to using ADODB recordsets in classic asp.

That said though, this will work.
Jun 13 '08 #2
lotus18
866 512MB
A quick answer would be to modify your query slightly to contain a count of the records.

Expand|Select|Wrap|Line Numbers
  1.  
  2. sql = "SELECT COUNT(column) as MyCount, * FROM MyTable"
  3.  
  4. DBReader.Read()
  5.  
  6. NumberOfReceords = DBReader("MyCount")
  7.  
  8. DBReader.Close
  9.  
I am probably missing a better way, but I too am accustomed to using ADODB recordsets in classic asp.

That said though, this will work.
It gives me an error result .
Jun 13 '08 #3
jeffstl
432 Expert 256MB
OK.

Can I see what your code looks like ?

You wouldnt be able to copy and paste mine directly and expect it to work obviously.....it was just an example.
Jun 13 '08 #4
lotus18
866 512MB
Expand|Select|Wrap|Line Numbers
  1.  Private adDataReader As OleDbDataReader
  2. Private adCommand As New OleDbCommand
  3.  
  4. Friend Function GetTotalItem() As Integer
  5.         Call Main.SetConnection()
  6.  
  7.         adCommand = New OleDbCommand("Select Count(ScheduleID) As myCount, * From Schedules", dbConnection)
  8.         adCommand.CommandType = CommandType.Text
  9.         adDataReader = adCommand.ExecuteReader
  10.         adDataReader.Read()
  11.  
  12.  
  13.         GetTotalItem = adDataReader("myCount")
  14.  
  15.         adDataReader.Close()
  16.         Main.CloseConnection()
  17.     End Function
Rey Sean
Jun 14 '08 #5
jeffstl
432 Expert 256MB
Ok. It would also help to know what the actual error is.

Off hand you could try this in case your query is not pulling anything

Expand|Select|Wrap|Line Numbers
  1.  
  2. Private adDataReader As OleDbDataReader
  3. Private adCommand As New OleDbCommand
  4.  
  5. Friend Function GetTotalItem() As Integer
  6.         Call Main.SetConnection()
  7.  
  8.         adCommand = New OleDbCommand("Select Count(ScheduleID) As myCount, * From Schedules", dbConnection)
  9.         adCommand.CommandType = CommandType.Text
  10.         adDataReader = adCommand.ExecuteReader
  11.  
  12.  
  13.     if adDataReader.Read() then
  14.          GetTotalItem = adDataReader("myCount")
  15.      else
  16.         GetTotalItem = 0
  17.     end if
  18.         adDataReader.Close()
  19.         Main.CloseConnection()
  20. End Function
  21.  
  22.  
  23.  
Jun 14 '08 #6
lotus18
866 512MB
Sorry for the late reply. Even with or without if-else statement, it always returns 0.

Rey Sean
Jun 16 '08 #7
QVeen72
1,445 Expert 1GB
Hi Ray,

Instead of DataReader , Use a Combination of DataAdapter,DataSet,DataTable..
Check this code:

Expand|Select|Wrap|Line Numbers
  1. Dim OConn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\MyDb.mdb;")
  2. OConn.Open()
  3. Dim sSQL As String = "Select * From MyTable"
  4. Dim ODa As New OleDbDataAdapter(sSQL, OConn)
  5. Dim DS As New DataSet
  6. Oda.Fill(DS)
  7. Dim DT As DataTable = DS.Tables(0)
  8. Dim RecCnt As Integer = DT.Rows.Count
  9. OConn.Close
  10.  
To jeffstl,
adCommand = New OleDbCommand("Select Count(ScheduleID) As myCount, * From Schedules", dbConnection)
Problem with the SQL Statement, using aggregrate function Count without Grouping Results in an Error..

Regards
Veena
Jun 16 '08 #8
lotus18
866 512MB
Thanks veena.. I'll try this later : )

Rey Sean
Jun 16 '08 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Rick | last post by:
I want to get a resultset of every table in the database, with the current record count of each. What is the easiest way to do this? I can get the list of tables with: Select s.name from...
6
by: 6thirty | last post by:
Hi, I've created a stocktaking database using Access XP. This is indexed by two fields - part number and shelf location. I am currently inputting all the data via a form. When I have entered a...
1
by: Greg Smith | last post by:
I am trying to write a SQL query that will return a record count to my C# application. I wrote the following query: ALTER PROCEDURE up_justification_duplicate AS SELECT COUNT(*) FROM...
1
by: darrel | last post by:
I'm trying to whip up a fancy repeater control that will put records into a two-column table for me. This is how I envision it working: itemtemplate if record count = odd then write out the...
4
by: darrel | last post by:
I have a repeater that I'd like to apply some logic to, namly highlighting the top few items in the repeater. To do this, I need to determine the record count. I've tried this: <% dim...
4
by: Peter W Johnson | last post by:
Hi guys, I have a problem with a datagrid record count. Here is the code:- <snip> Public Class frmMerchantDeposit Inherits System.Windows.Forms.Form Dim myconnection As New...
5
by: noLoveLusT | last post by:
hi everyone i am very very new to the sql server (2 days actually and ) so far i learned creating SPs etc but couldnt workout how to get return value from my prodecure my sp as follows...
0
by: Pramod Kadur | last post by:
Using borland c++, how can we fetch the record count in the following case: Suppose in XY table in Foxpro, First column A B C D E F
4
by: bushi | last post by:
hi ! i have following code to display some text on a web form,after getting it from database. <asp:DataList ID="DataList1" runat="server" DataSourceID="SqlDataSource1"> <ItemTemplate>...
1
by: sparks | last post by:
I have always just got a count on records and put up a box at the top of the entry forms saying something like 1 of 500 and updating it as you got to 500 of 500. I have never thought of this as...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.