Solved

dataset error

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
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…
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 is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.

947 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

20 Experts available now in Live!

Get 1:1 Help Now