Invalid attempt to call Read when reader is closed

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • samvb
    New Member
    • Oct 2006
    • 228

    #1

    Invalid attempt to call Read when reader is closed

    I am gettting the following 2 errors:

    Invalid attempt to call Read when reader is closed

    and sometimes

    Invalid attempt to call MetaData when reader is closed

    when attempting to read from a sqldatareader object. What I want is to list groups in listview. The last column is "Total Numbers".

    Code:
    while reader.read
    'fill lv here.
    'last column value is calculated from a function that does db operation
    
    lvitem.subitems.add(totalItems(reader.item("id"))
    
    end while 
    
    function totalItems(byval id as integer) as integer
    //reader operation goes here from query like select count(*) as total from tbl where field=id
    
    end function
    It works ok for the first row but after that the first error generates. How can I do this? I don't want to include the COUNT function in the main sql cos then i would be required to group the rest fields (there are 9 of them totally). I tried MARS but it don't work at all in my connection which is like:

    Code:
    public dbconnection as new sqlclient.connection
    
    dbconnection.connectionstring=strconnection
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    We need to see the code in totalItems.

    Comment

    Working...