Solved

dataset error

Posted on 2006-06-23
3
197 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

860 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