Getting the record count

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lotus18
    Contributor
    • Nov 2007
    • 865

    Getting the record count

    Hello World

    I know how to display or manipulate (add,delete,upd ate,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.recor dcount. I've just tried using DataTable and OleDbDataAdapte r class but I can't make it. Please guide me. BTW, I'm using vb2005.

    Rey Sean
  • jeffstl
    Recognized Expert Contributor
    • Feb 2008
    • 432

    #2
    A quick answer would be to modify your query slightly to contain a count of the records.

    Code:
    sql = "SELECT COUNT(column) as MyCount, * FROM MyTable"
    
    DBReader.Read()
    
    NumberOfReceords = DBReader("MyCount")
    
    DBReader.Close
    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.

    Comment

    • lotus18
      Contributor
      • Nov 2007
      • 865

      #3
      Originally posted by jeffstl
      A quick answer would be to modify your query slightly to contain a count of the records.

      Code:
      sql = "SELECT COUNT(column) as MyCount, * FROM MyTable"
      
      DBReader.Read()
      
      NumberOfReceords = DBReader("MyCount")
      
      DBReader.Close
      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 .

      Comment

      • jeffstl
        Recognized Expert Contributor
        • Feb 2008
        • 432

        #4
        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.....i t was just an example.

        Comment

        • lotus18
          Contributor
          • Nov 2007
          • 865

          #5
          [CODE=vb .net]
          Private adDataReader As OleDbDataReader
          Private adCommand As New OleDbCommand

          Friend Function GetTotalItem() As Integer
          Call Main.SetConnect ion()

          adCommand = New OleDbCommand("S elect Count(ScheduleI D) As myCount, * From Schedules", dbConnection)
          adCommand.Comma ndType = CommandType.Tex t
          adDataReader = adCommand.Execu teReader
          adDataReader.Re ad()


          GetTotalItem = adDataReader("m yCount")

          adDataReader.Cl ose()
          Main.CloseConne ction()
          End Function[/CODE]

          Rey Sean
          Last edited by lotus18; Jun 14 '08, 03:05 AM. Reason: GetTotalItmem has been changed to GetTotalItem

          Comment

          • jeffstl
            Recognized Expert Contributor
            • Feb 2008
            • 432

            #6
            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

            Code:
            Private adDataReader As OleDbDataReader
            Private adCommand As New OleDbCommand
             
            Friend Function GetTotalItem() As Integer
                    Call Main.SetConnection()
             
                    adCommand = New OleDbCommand("Select Count(ScheduleID) As myCount, * From Schedules", dbConnection)
                    adCommand.CommandType = CommandType.Text
                    adDataReader = adCommand.ExecuteReader
                    
            
            	if adDataReader.Read() then
             		GetTotalItem = adDataReader("myCount")
             	else
            		GetTotalItem = 0
            	end if
                    adDataReader.Close()
                    Main.CloseConnection()
            End Function

            Comment

            • lotus18
              Contributor
              • Nov 2007
              • 865

              #7
              Sorry for the late reply. Even with or without if-else statement, it always returns 0.

              Rey Sean

              Comment

              • QVeen72
                Recognized Expert Top Contributor
                • Oct 2006
                • 1445

                #8
                Hi Ray,

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

                [code=vb]
                Dim OConn As New OleDbConnection ("Provider=Micr osoft.Jet.OLEDB .4.0; Data Source=C:\MyDb. mdb;")
                OConn.Open()
                Dim sSQL As String = "Select * From MyTable"
                Dim ODa As New OleDbDataAdapte r(sSQL, OConn)
                Dim DS As New DataSet
                Oda.Fill(DS)
                Dim DT As DataTable = DS.Tables(0)
                Dim RecCnt As Integer = DT.Rows.Count
                OConn.Close
                [/code]

                To jeffstl,
                Originally posted by jeffstl
                adCommand = New OleDbCommand("S elect Count(ScheduleI D) As myCount, * From Schedules", dbConnection)
                Problem with the SQL Statement, using aggregrate function Count without Grouping Results in an Error..

                Regards
                Veena

                Comment

                • lotus18
                  Contributor
                  • Nov 2007
                  • 865

                  #9
                  Thanks veena.. I'll try this later : )

                  Rey Sean

                  Comment

                  Working...