Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Count of Rows Returned

Count of Rows Returned

Scheduled Pinned Locked Moved Database
question
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kenexcelon
    wrote on last edited by
    #1

    I have this: With Me.daNumCourses.SelectCommand .CommandText = "SELECT Section.Section_ID, Section.InstructorID, Instructor.ID FROM Section INNER JOIN Instructor ON Section.InstructorID = Instructor.ID AND Section.InstructorID = '" & Me.instr.ID & "'" intCount = .ExecuteNonQuery() End With Me.dsNumCourses.Clear() Me.daNumCourses.Fill(Me.dsNumCourses) Me.txtNumberCourse.Text = intCount I keep getting -1 so what am I doing wrong? Thanks

    C 1 Reply Last reply
    0
    • K kenexcelon

      I have this: With Me.daNumCourses.SelectCommand .CommandText = "SELECT Section.Section_ID, Section.InstructorID, Instructor.ID FROM Section INNER JOIN Instructor ON Section.InstructorID = Instructor.ID AND Section.InstructorID = '" & Me.instr.ID & "'" intCount = .ExecuteNonQuery() End With Me.dsNumCourses.Clear() Me.daNumCourses.Fill(Me.dsNumCourses) Me.txtNumberCourse.Text = intCount I keep getting -1 so what am I doing wrong? Thanks

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      Why are you doing this? Why not just fill the data set and get a count of the rows in the DataTable? e.g. dsNumCourses.Tables[0].Rows.Count


      My: Blog | Photos "Man who stand on hill with mouth open will wait long time for roast duck to drop in." -- Confucious

      K 1 Reply Last reply
      0
      • C Colin Angus Mackay

        Why are you doing this? Why not just fill the data set and get a count of the rows in the DataTable? e.g. dsNumCourses.Tables[0].Rows.Count


        My: Blog | Photos "Man who stand on hill with mouth open will wait long time for roast duck to drop in." -- Confucious

        K Offline
        K Offline
        kenexcelon
        wrote on last edited by
        #3

        I just want to get the count of the classes that a certain instructor teaches. I tried the .count property and that counts all of the rows in a particular table. Thanks for your reply. If you have a suggestion on how to do that please post back.

        C 1 Reply Last reply
        0
        • K kenexcelon

          I just want to get the count of the classes that a certain instructor teaches. I tried the .count property and that counts all of the rows in a particular table. Thanks for your reply. If you have a suggestion on how to do that please post back.

          C Offline
          C Offline
          Colin Angus Mackay
          wrote on last edited by
          #4

          Why don't you have your SQL as:

          SELECT COUNT(*) FROM ....

          Also, the way you build your SQL is suscepatable to a SQL Injection Attack and you should try and use parameterised queries where ever possible. You can read more on how to improve the security of your application here: SQL Injection attacks and tips on how to prevent them[^]


          My: Blog | Photos "Man who stand on hill with mouth open will wait long time for roast duck to drop in." -- Confucious

          K 1 Reply Last reply
          0
          • C Colin Angus Mackay

            Why don't you have your SQL as:

            SELECT COUNT(*) FROM ....

            Also, the way you build your SQL is suscepatable to a SQL Injection Attack and you should try and use parameterised queries where ever possible. You can read more on how to improve the security of your application here: SQL Injection attacks and tips on how to prevent them[^]


            My: Blog | Photos "Man who stand on hill with mouth open will wait long time for roast duck to drop in." -- Confucious

            K Offline
            K Offline
            kenexcelon
            wrote on last edited by
            #5

            How do you return the number of rows from the query? In other words if I have a variable intCount how do I set intCount to the "count" of the query? Thanks for your reply. We've noticed a few security holes in our application in a few places and we are fixing those right now.

            C 1 Reply Last reply
            0
            • K kenexcelon

              How do you return the number of rows from the query? In other words if I have a variable intCount how do I set intCount to the "count" of the query? Thanks for your reply. We've noticed a few security holes in our application in a few places and we are fixing those right now.

              C Offline
              C Offline
              Colin Angus Mackay
              wrote on last edited by
              #6

              Dim cmd As SqlCommand
              cmd = SqlCommand.New()
              cmd.CommandText = "SELECT COUNT(*) FROM MyTable WHERE someColumn=@someValue"
              cmd.Connection = myConnection
              Dim numRows As Int
              numRows = cmd.ExecuteScalar()

              numRows will contain the number of rows that match the given filter (i.e. what is filtered in the WHERE clause). COUNT() is an aggregate function that returns a count, when COUNT(*) is used it returns the number of rows. If you use COUNT(columnName) then it will count the number of rows where the column value is not null. Does this help?


              My: Blog | Photos "Man who stand on hill with mouth open will wait long time for roast duck to drop in." -- Confucious

              A 1 Reply Last reply
              0
              • C Colin Angus Mackay

                Dim cmd As SqlCommand
                cmd = SqlCommand.New()
                cmd.CommandText = "SELECT COUNT(*) FROM MyTable WHERE someColumn=@someValue"
                cmd.Connection = myConnection
                Dim numRows As Int
                numRows = cmd.ExecuteScalar()

                numRows will contain the number of rows that match the given filter (i.e. what is filtered in the WHERE clause). COUNT() is an aggregate function that returns a count, when COUNT(*) is used it returns the number of rows. If you use COUNT(columnName) then it will count the number of rows where the column value is not null. Does this help?


                My: Blog | Photos "Man who stand on hill with mouth open will wait long time for roast duck to drop in." -- Confucious

                A Offline
                A Offline
                anandss
                wrote on last edited by
                #7

                This is the correct way. ExecuteNonQuery should be used for Insert/Update/Delete statements. If the CommandText is "SELECT" statment, then it will always return -1. When it is DML statement, then it will return the number of rows affected by that DML statement.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups