Solved

dataset error

Posted on 2006-06-23
3
195 Views
Last Modified: 2010-04-23
Hello Experts,

I have a question about datasets. I connect to several databases  and load the query result into a dataset that I write to an XML file. The first connection works fine but when I try it a second time in the same sub I get this error:

"There is already an open DataReader associated with this Connection which must be closed first."



Posted below is the code that executes:

Try
            conn.Open()
            mycommand.Connection = conn

            mycommand.CommandText = "SELECT * FROM test WHERE testNum= " & Textbox1.Text & " AND Type = 'Contract'"
            myRead = mycommand.ExecuteReader

            While myRead.Read()
                ...assign variables...
            End While

            myRead.Close()
            conn.Close()

            myAdapter.SelectCommand = mycommand
            myAdapter.Fill(myData)
            myData.WriteXml("C:\Dataset.xml", XmlWriteMode.WriteSchema)
            myData.Clear()

            '****************Second connection*********************

            conn.ConnectionString = "server=192.16.......~"
            conn.Open()
            mycommand.Connection = conn
            mycommand.CommandText = "SELECT * FROM test2 WHERE test2Num= " & Textbox1.Text
            myRead = mycommand.ExecuteReader

            While myRead.Read()
              ...assign varibales...
            End While

            myAdapter.SelectCommand = mycommand
            myAdapter.Fill(myData)
            myData.WriteXml("C:\totalNumberDataset.xml", XmlWriteMode.WriteSchema) <----HERE is where the exception is thrown

        Catch ex As MySqlException
            MessageBox.Show(ex.Message)
        Finally
            conn.Close()
        End Try

Any help is greatly appreciated


Thanks,

Zac
0
Comment
Question by:sacul
  • 2
3 Comments
 
LVL 7

Accepted Solution

by:
pradeepsudharsan earned 250 total points
ID: 16969124
While myRead.Read()
              ...assign varibales...
            End While
myRead.Close() 'Close the reader and use adapter
0
 
LVL 7

Expert Comment

by:pradeepsudharsan
ID: 16969177
       myRead.Close()
            conn.Close()
You missed these lines in Second connection

While myRead.Read()
              ...assign varibales...
            End While
myRead.Close() 'Close the reader and use adapter
 conn.Close() is not needed
0
 
LVL 1

Author Comment

by:sacul
ID: 16970105
Awesome...

I just missed that part. You spend enough hours looking at it and you go code blind thats for sure

Thanks for the help.

Thanks,

Zac
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now