Hack Forums
[C#] Getting an error when filling 2 datagrids with Singleton - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [C#] Getting an error when filling 2 datagrids with Singleton (/showthread.php?tid=6073568)



Getting an error when filling 2 datagrids with Singleton - Kαѕρer - May 1, 2020

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!


RE: Getting an _error_ when filling 2 datagrids with Singleton - Shadow_Cloud - May 1, 2020

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));
                }
            }
        }



RE: Getting an _error_ when filling 2 datagrids with Singleton - Kαѕρer - May 1, 2020

(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


RE: Getting an _error_ when filling 2 datagrids with Singleton - Shadow_Cloud - May 1, 2020

(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/csharp/language-reference/keywords/using-statement

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