Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C# 

Getting an error when filling 2 datagrids with Singleton

I'm trying to fill 2 datagrids with 2 different tables from one database.

I had an error first saying I got an open connection so I changed this:

Code
conn.Open();

to this:

Code
if (conn.State == ConnectionState.Closed)
               {
                   conn.Open();
               }

But now I'm getting the error:

Code
There is already an open DataReader associated with this command which must be closed first

Researched on internet but have no clue how to fix this.

If you might be able to help me, hit me up. I would appreciate it.

Thanks!
I'm going to have to guess a few things here because the code samples isn't really showing the full picture.

I presume you aren't disposing properly here. .Net will do quite a bit of black magic and trickery with connection pooling for you, however, it won't deal with multiple active results unless you set MultipleActiveResultSets=True in your connectionstring. Even with that I would strongly encourage you to look at wrapping these in using statements.

Code
using (SqlConnection con = new SqlConnection(connectionString))
        {
            //
            // Open the SqlConnection.
            //
            con.Open();
            //
            // This code uses an SqlCommand based on the SqlConnection.
            //
            using (SqlCommand command = new SqlCommand("SELECT TOP 2 * FROM Dogs1", con))
            using (SqlDataReader reader = command.ExecuteReader())
            {
                while (reader.Read())
                {
                    Console.WriteLine("{0} {1} {2}",
                        reader.GetInt32(0), reader.GetString(1), reader.GetString(2));
                }
            }
        }
(May 1, 2020 10:14 AM)Shadow_Cloud Wrote:  I'm going to have to guess a few things here because the code samples isn't really showing the full picture.  

I presume you aren't disposing properly here.  .Net will do quite a bit of black magic and trickery with connection pooling for you, however, it won't deal with multiple active results unless you set MultipleActiveResultSets=True in your connectionstring.  Even with that I would strongly encourage you to look at wrapping these in using statements.

Code
using (SqlConnection con = new SqlConnection(connectionString))
       {
           //
           // Open the SqlConnection.
           //
           con.Open();
           //
           // This code uses an SqlCommand based on the SqlConnection.
           //
           using (SqlCommand command = new SqlCommand("SELECT TOP 2 * FROM Dogs1", con))
           using (SqlDataReader reader = command.ExecuteReader())
           {
               while (reader.Read())
               {
                   Console.WriteLine("{0} {1} {2}",
                       reader.GetInt32(0), reader.GetString(1), reader.GetString(2));
               }
           }
       }

Thanks man, just added "using" before and everything works now.

+3
(May 1, 2020 10:17 AM)Kαѕρer Wrote:  Thanks man, just added "using" before and everything works now.

+3

Awesome, if you need to read up more on using statement, you can start here >

https://docs.microsoft.com/en-us/dotnet/...-statement

Word of warning though, don't do this for httpClient. Its one of those traps I've fallen into before.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C#] by itcorpJan 16, 2019 03:46 PM 7 339 Jan 16, 2019 03:46 PM
Last Post: itcorp
[.NET] by zna213Feb 24, 2018 04:35 AM 3 145 Feb 24, 2018 04:35 AM
Last Post: RiperFish
[C] by DarkScientistJan 5, 2017 10:08 AM 0 144 Jan 5, 2017 10:08 AM
Last Post: DarkScientist