Resolved Open DataReader must be close first

Socarsky

Well-known member
Joined
Dec 27, 2012
Messages
173
Location
Jakarta/Indonesia
Programming Experience
Beginner
I am face with this error "There is already an open DataReader associated with this Command which must be closed first." What should I do to fix that?
Adapters conn. string below :
SqlDataAdapter1 connection string : Data Source=SOCARSKY\sqlexpress;Initial Catalog=NORTHWND;Integrated Security=True
SqlDataAdapter2 connection string : Data Source=SOCARSKY\sqlexpress;Initial Catalog=NORTHWND;Integrated Security=True
Btw, I tried to add this "MultipleActiveResultSets=True" to the conn. script but fail.


Public Class frmCustomers
    Private Sub frmCustomers_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ' Load the Combo Box
        DsCountries1.Clear()
        SqlDataAdapter2.Fill(DsCountries1, "Customers")
    End Sub

    Private Sub cboCountry_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboCountry.SelectedIndexChanged
        ' Get the Parameter object and Set value
        With SqlDataAdapter1.SelectCommand.Parameters
            .Item("@CountryParam").Value = cboCountry.SelectedValue
        End With
        ' Clear the dataset
        DsCustomers1.Clear()
        ' Load the dataset using the parameter value
        SqlDataAdapter1.Fill(DsCustomers1, "Customers")
    End Sub
End Class
 
Last edited:
You might want to handle the SelectionChangeCommitted event instead of SelectedIndexChanged. That way you'll only execute the second query when the user makes a selection in the UI and not when you make a change in code.

By the way, don't use two separate DataSets. The whole point of a DataSet is that it can contain multiple DataTables. Just use one DataSet or don't use a DataSet at all. You might also try meaningful names for your data adapters while you're at it.
 
You might want to handle the SelectionChangeCommitted event instead of SelectedIndexChanged. That way you'll only execute the second query when the user makes a selection in the UI and not when you make a change in code.

By the way, don't use two separate DataSets. The whole point of a DataSet is that it can contain multiple DataTables. Just use one DataSet or don't use a DataSet at all. You might also try meaningful names for your data adapters while you're at it.
Thanks for information, I actually only wanted to make that tutorials on MSDN web page : Data Binding with Windows Forms and ADO.NET MSDN binding data tutorial show the way me to do. But I must tell that I don't like that way to do.
 
That article was written in 2001. Noone would do it that way in recent versions of VB.NET. It might be similar but definitely not the same. You might like to follow the Data Walkthroughs link in my signature and try some of those, like Forms Over Data.
 
jmcilhinney,
What's the difference between normal MS Sql Server and MS SQL Express as ADO connection?
And really wonder that I developed a small database program which works well with my laptop because Sql Express on my local but If I share the program with someone to run it even we are the same network segment then the program does not work and thrown a sql connection network exception. Why?
 
What's the difference between normal MS Sql Server and MS SQL Express as ADO connection?
If you're connecting to an attached database, i.e. using the Initial Catalog attribute in the connection string, then there's no difference at all. SQL Server Express also allows you to attach a database file on demand using the AttachDbFilename attribute, which full SQL Server doesn't.
And really wonder that I developed a small database program which works well with my laptop because Sql Express on my local but If I share the program with someone to run it even we are the same network segment then the program does not work and thrown a sql connection network exception. Why?
You'd have to be more specific about exactly how you're connecting and what information the exception provides.
 
That captured image hopes explain something enough that you want to expect.
21221373.png
 
Did you actually read what the dialogue in your screen shot says? I don't know what you want me to tell you that that doesn't already. I specifically asked for information about how you're connecting and you haven't bothered to provide it. I'm not psychic. If you want help then provide the relevant information, especially when it's specifically requested.
 
I got an idea now, and let check my server features which are related remote connections from networks. Thanks
 
That captured image hopes explain something enough that you want to expect.
21221373.png

I solved this issue finally after a week as I haven't given good attention to figure out that issue and I trusted my conn. string so much because it has been working all the time when I run it on my laptop and also there is a big reason which ppl say it's really important to add sqlexpress suffix if your sql server is an express edition then you need to put sqlexpress statement after your netbios name with a back slash. Then I made a test by creating another project and notice the difference with add data component as automatically by Visual Studio 2012 and then finally solved my issue with not adding back slash and sqlexpress statement after my pc's netbios name. Now It work well and tested it with a couple pc in my network segment.
Now I wanted to share this with a couple pictures to show those details with screen of Visual Studio 2012.
below has a sqlconnection component and you can see my string in its feature.
http://imageshack.us/a/img600/7095/myproject1.jpg

below shows my whole code and conn. string too, this project runs well finally, thanks goodness. :)
http://imageshack.us/a/img707/6603/myproject2.jpg
 
Back
Top