Solved

dataset error

Posted on 2006-06-23
3
198 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
FInd Image Control Gridview 3 37
Asp.Net Session Question 2 36
vb.net deleting excel sheet in workbook 11 35
Create a button class for use in all vb.net apps 5 19
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
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…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

733 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