Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > .NET > .NET 1.0 and Visual Studio.NET > VS.NET 2002/2003
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
VS.NET 2002/2003 Discussions about the Visual Studio.NET programming environment, the 2002 (1.0) and 2003 (1.1). ** Please don't post code questions here ** For issues specific to a particular language in .NET, please see the other forum categories.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VS.NET 2002/2003 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old April 27th, 2004, 07:13 PM
Registered User
 
Join Date: Apr 2004
Location: , , .
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default Invalid attempt to Read when reader is closed.

'This code is not complete but I am getting the error "Invalid attempt to Read when reader is closed." at the marked error line. I dont understand what's wrong here. please help.



 For Each row In dset1.Tables("LOCATION").Rows
                rnew = dset1.Tables("BHLOCBDSBD").NewRow

                pid = row("PointID")
                cnt = cnt + 1
                str2 = "SELECT * FROM DATATYPE WHERE DataTypeID=1 AND PointID=" & pid
                Dim getrid As New OleDbCommand(str2, myconnection)
                ''myconnection.Close()
                ''myconnection.Open()
                myreader1 = getrid.ExecuteReader()
                While (myreader1.Read())
                    rid = myreader1(2)
                End While
                myreader1.Close()
                ''myconnection.Close()
                'myconnection.Open()

                query1 = "SELECT * FROM BOREHOLEDATASOURCE WHERE RecordID=" & rid
                query2 = "SELECT * FROM BOREHOLEDATA WHERE RecordID=" & rid

                If cnt = 1 Then
                    com1.CommandText = query1
                    adapter1.Fill(dset1, "BHDATASOURCE")
                    com1.CommandText = query2
                    adapter1.Fill(dset1, "BHDATA")
                End If

                rownew = dset1.Tables("BHDATASOURCE").NewRow

                Dim get1 As New OleDbCommand(query1, myconnection)
                myreader1 = get1.ExecuteReader()
      'Error Line
             While (myreader1.Read())

                    rownew("RecordID") = myreader1(0)
                    rownew("FundAgencyID") = myreader1(1)
                    rownew("EngineeringID") = myreader1(2)
                    rownew("GeotechCoID") = myreader1(3)
                    rownew("DrillingCoID") = myreader1(4)
                    rownew("ProjectNameID") = myreader1(5)
                    rownew("HoleID") = myreader1(6)
                    rownew("ReportNo") = myreader1(7)
                    rownew("ContractNo") = myreader1(8)
                    rownew("GeofileNo") = myreader1(9)
                    rownew("DrillFileNo") = myreader1(10)
                    rownew("BridgeNo") = myreader1(11)

                    rnew("RecordID") = myreader1(0)
                    myreader1.Close()
                    strfa = "Select o.Name from ORGANISATIONNAME o, BOREHOLEDATASOURCE bds, FUNDAGENCY f where f.FundAgencyID = bds.FundAgencyID and o.OrgCatID=2 and bds.FundAgencyID=" & rownew("FundAgencyID")
                    strec = "Select o.Name from ORGANISATIONNAME o, BOREHOLEDATASOURCE bds, ENGINEERINGCOMPANY e where e.EngineeringID = bds.EngineeringID and o.OrgCatID=3 and bds.EngineeringID=" & rownew("EngineeringID")
                    strgc = "Select o.Name from ORGANISATIONNAME o, BOREHOLEDATASOURCE bds, GEOTECHCOMPANY g where g.GeotechCoID = bds.GeotechCoID and o.OrgCatID=4 and bds.GeotechCoID=" & rownew("GeotechCoID")
                    strdc = "Select o.Name from ORGANISATIONNAME o, BOREHOLEDATASOURCE bds, DRILLINGCOMPANY d where d.DrillingCoID = bds.DrillingCoID and o.OrgCatID=3 and bds.DrillingCoID=" & rownew("DrillingCoID")
                    strpn = "Select pu.ProjectName from PROJECTNAMELOOKUP pu, BOREHOLEDATASOURCE bds where pu.ProjectNameID = bds.ProjectNameID and bds.ProjectNameID=" & rownew("ProjectNameID")


                    Dim comfa As New OleDbCommand(strfa, myconnection)
                    'Dim readerfa As OleDbDataReader
                    'myconnection.Close()
                    'myconnection.Open()
                    myreader1 = comfa.ExecuteReader()
                    While (myreader1.Read())
                        rnew("FundAgc") = myreader1(0) & ""
                    End While
                    myreader1.Close()
                End While

                myreader1 = get1.ExecuteReader()
                While (myreader1.Read())

                    Dim comec As New OleDbCommand(strec, myconnection)
                    ' Dim readerec As OleDbDataReader
                    'myconnection.Close()
                    'myconnection.Open()
                    myreader1 = comec.ExecuteReader()
                    While (myreader1.Read())
                        'Response.Write("string is " & readerec(0))
                        'Response.End()
                        rnew("EngComp") = myreader1(0) & ""
                    End While
                    myreader1.Close()
                End While

                myreader1 = get1.ExecuteReader()
                While (myreader1.Read())

                    Dim comgc As New OleDbCommand(strgc, myconnection)
                    'Dim readergc As OleDbDataReader
                    'myconnection.Close()
                    'myconnection.Open()
                    myreader1 = comgc.ExecuteReader()
                    While (myreader1.Read())
                        'Response.Write("string is " & readerec(0))
                        'Response.End()
                        rnew("GeoComp") = myreader1(0) & ""
                    End While
                    myreader1.Close()
                End While

                myreader1 = get1.ExecuteReader()
                While (myreader1.Read())

                    Dim comdc As New OleDbCommand(strdc, myconnection)
                    'Dim readerdc As OleDbDataReader
                    'myconnection.Close()
                    'myconnection.Open()
                    myreader1 = comdc.ExecuteReader()
                    While (myreader1.Read())
                        'Response.Write("string is " & readerec(0))
                        'Response.End()
                        rnew("DrilComp") = myreader1(0) & ""
                    End While
                    myreader1.Close()
                End While

                myreader1 = get1.ExecuteReader()
                While (myreader1.Read())

                    Dim compn As New OleDbCommand(strpn, myconnection)
                    'Dim readerpn As OleDbDataReader
                    'myconnection.Close()
                    'myconnection.Open()
                    myreader1 = compn.ExecuteReader()
                    While (myreader1.Read())
                        'Response.Write("string is " & readerec(0))
                        'Response.End()
                        rnew("ProjName") = myreader1(0) & ""
                    End While
                    myreader1.Close()
                End While

                myreader1 = get1.ExecuteReader()
                While (myreader1.Read())
                    rnew("HoleID") = myreader1(6)
                    rnew("RepNo") = myreader1(7)
                    rnew("ContractNo") = myreader1(8)
                    rnew("GeofileNo") = myreader1(9)
                    rnew("DrilfileNo") = myreader1(10)
                    rnew("BridgeNo") = myreader1(11)

                    dset1.Tables("BHDATASOURCE").Rows.Add(rownew)

                End While

                myreader1.Close()
Reply With Quote
  #2 (permalink)  
Old April 28th, 2004, 02:19 AM
Registered User
 
Join Date: Apr 2004
Location: Hyderabad, AP, India.
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to nagendrakumar_b
Default

Hi,

you are reading the datareader using while loop, thats fine. but u r closing the datareader in the same loop,( I observed this for the first while loop) that causing the error, as while loop tries to check whether the boolean propery 'read' of the datareader is returning true or false, but u had already closed the datareader. one more issure is u rrecreating the reader within the while loop. Avoid to do that. Firstly u rearrange ur code. use the while loop, read the data and close the datareader after 'End while' not inside the while loop.REMEMBER, DON'T CHANGE THE QUERY OF THE DATAREADER INSIDE A WHILE LOOP. CREATE SEPARATE WHILE LOOP FOR EACH QUERY ASSINED TO UR DATAREADER.
CHEERS


Cause for the error(Higlighted with *****) checkout:


 While (myreader1.Read())

                    rownew("RecordID") = myreader1(0)
                    rownew("FundAgencyID") = myreader1(1)
                    rownew("EngineeringID") = myreader1(2)
                    rownew("GeotechCoID") = myreader1(3)
                    rownew("DrillingCoID") = myreader1(4)
                    rownew("ProjectNameID") = myreader1(5)
                    rownew("HoleID") = myreader1(6)
                    rownew("ReportNo") = myreader1(7)
                    rownew("ContractNo") = myreader1(8)
                    rownew("GeofileNo") = myreader1(9)
                    rownew("DrillFileNo") = myreader1(10)
                    rownew("BridgeNo") = myreader1(11)

                    rnew("RecordID") = myreader1(0)
                    myreader1.Close()
                    strfa = "Select o.Name from ORGANISATIONNAME o, BOREHOLEDATASOURCE bds, FUNDAGENCY f where f.FundAgencyID = bds.FundAgencyID and o.OrgCatID=2 and bds.FundAgencyID=" & rownew("FundAgencyID")
                    strec = "Select o.Name from ORGANISATIONNAME o, BOREHOLEDATASOURCE bds, ENGINEERINGCOMPANY e where e.EngineeringID = bds.EngineeringID and o.OrgCatID=3 and bds.EngineeringID=" & rownew("EngineeringID")
                    strgc = "Select o.Name from ORGANISATIONNAME o, BOREHOLEDATASOURCE bds, GEOTECHCOMPANY g where g.GeotechCoID = bds.GeotechCoID and o.OrgCatID=4 and bds.GeotechCoID=" & rownew("GeotechCoID")
                    strdc = "Select o.Name from ORGANISATIONNAME o, BOREHOLEDATASOURCE bds, DRILLINGCOMPANY d where d.DrillingCoID = bds.DrillingCoID and o.OrgCatID=3 and bds.DrillingCoID=" & rownew("DrillingCoID")
                    strpn = "Select pu.ProjectName from PROJECTNAMELOOKUP pu, BOREHOLEDATASOURCE bds where pu.ProjectNameID = bds.ProjectNameID and bds.ProjectNameID=" & rownew("ProjectNameID")


                    Dim comfa As New OleDbCommand(strfa, myconnection)
                    'Dim readerfa As OleDbDataReader
                    'myconnection.Close()
                    'myconnection.Open()
                    myreader1 = comfa.ExecuteReader()
                    While (myreader1.Read())
                        rnew("FundAgc") = myreader1(0) & ""
                    End While
                    **************myreader1.Close()*************
                End While


Nag
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Read stream from USB Magnetic Card Reader tsadok Visual Basic 2005 Basics 8 September 3rd, 2008 03:02 PM
Invalid attempt to FieldCount when reader is close hwajunsong Crystal Reports 1 December 28th, 2005 01:18 PM
Invalid attempt to Read when reader is closed. nidy_online VS.NET 2002/2003 0 April 28th, 2004 11:02 AM
How to make stream reader read doc files? bcmaverik VB.NET 2002/2003 Basics 2 March 10th, 2004 10:28 AM



All times are GMT -4. The time now is 01:17 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.