Results 1 to 4 of 4

Thread: Row count

  1. #1

    Thread Starter
    Member Ooogaleee's Avatar
    Join Date
    Nov 2002
    Location
    Jacksonville, FL
    Posts
    52

    Row count

    I need to return the number of rows produced into the datagrid by this code. Can someone give me a push in the right direction?

    Thanks much....Ooogs


    Dim myQuery As String = "SELECT * FROM [tbl * F002 > 4 J] WHERE MUFLID2 =" & Chr(39) & ddlWho.SelectedItem.Text & Chr(39) & " ORDER BY" & sortee
    OleDbConnection1.Open()
    Dim cmd As New OleDb.OleDbCommand()
    cmd.Connection = OleDbConnection1
    cmd.CommandText = myQuery
    Dim datareader As OleDb.OleDbDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)
    dgF002.DataSource = datareader
    dgF002.DataBind()

  2. #2
    C# Aficionado Lord_Rat's Avatar
    Join Date
    Sep 2001
    Location
    Cave
    Posts
    2,497
    There is a programmatical way, but I usually just run the same select you have, but instead of
    SELECT * ...
    I use
    SELECT Count(*) ...

    Then, I read row1,col1 to get the count and then close the recordset, run the SELECT * and press on.
    Need to re-register ASP.NET?
    C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i

    (Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)

  3. #3

    Thread Starter
    Member Ooogaleee's Avatar
    Join Date
    Nov 2002
    Location
    Jacksonville, FL
    Posts
    52
    Do you mean to do something like this?

    Dim cmd As New OleDb.OleDbCommand()
    cmd.Connection = OleDbConnection1

    Dim myCount As String = "SELECT count(*) FROM [tbl * F002 > 4 J] WHERE MUFLID2 =" & Chr(39) & ddlWho.SelectedItem.Text
    OleDbConnection1.Open()
    cmd.CommandText = myCount
    Dim datareader As OleDb.OleDbDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)

    (Here is where I guess I need to reference the count(*))

    Dim myQuery As String = "SELECT * FROM [tbl * F002 > 4 J] WHERE MUFLID2 =" & Chr(39) & ddlWho.SelectedItem.Text & Chr(39) & " ORDER BY" & sortee
    OleDbConnection1.Open()
    cmd.CommandText = myQuery
    datareader = cmd.ExecuteReader(CommandBehavior.CloseConnection)
    dgF002.DataSource = datareader
    dgF002.DataBind()
    OleDbConnection1.Close()


    If so, how would I then reference the count number I obtained in the first block?

    Ooogs

  4. #4
    C# Aficionado Lord_Rat's Avatar
    Join Date
    Sep 2001
    Location
    Cave
    Posts
    2,497
    Correct.

    Something like:
    Code:
    if not objReader.Read() Then
       'Oh My. Something went wrong...
       return false
    end if
    
    myCount = objReader.Items(0)
    
    objReader.Close()
    Just FYI. This is actually better than trying to get the row count from the objReader itself for the following reason.

    If you attempted to get the reader's row count, you would have to fetch to the end of the recordset first in order to assure you that the dataset has dumped you the entire result set (in order to get an accurate count) which also means that you have to open the recordset with a backward-readable cursor, which in turn uses more environment space and is a slower cursor.
    Need to re-register ASP.NET?
    C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i

    (Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width