• Register
0 votes
42 views

Problem :

I am beginner in C#. I am trying to write the Function using C#. But while executing my code I am facing below error

The connection was not closed. The connection's current state is open.

How to fix above error?

8 4 2
2,300 points

Please log in or register to answer this question.

1 Answer

0 votes

Solution:

I guess you have declared your connection variable inside your class so it gets opened when you first tried to use it and then it stays that way. If you are trying to click the button after your connection is opened then I can tell you are trying to open connection again.

Best way is to use a local connection variable and open your connection as late as possible in your code and close connection with the help of using() as early as possible. Please have a look at below code sample.

using (SqlConnection connection = new SqlConnection(connStr))
{
    connection.Open();
    // Do your main work here
}
// Do your other work here

If you are sharing connection object and then you are keeping it idle for some time in that case also you can get the exceptions when you try to use it. So checking the State will not help as it stays as opened until you explicitly change the state using Close() or you try to use it in some or the other way. After you get the exception your State will be updated.

5 2 1
4,980 points

Related questions

0 votes
1 answer 444 views
444 views
Problem : When I am attempting to connect to MSSQL database via ASP.NET online, I will often get the below error when two or more people try to connect simultaneously: ExecuteReader requires an open and available Connection. The connection's current state is Connecting. My site works fine on the localhost server. May I know what might have gone wrong with my site and how can I fix the error?
asked Jan 31, 2020 jwilliam 3.9k points
0 votes
1 answer 55 views
55 views
Problem : I am having my radcombo machines on aspx page and whenever I try to select any option from them then it gives me following `Server Error in '/' Application. Exception Details: System.InvalidOperationException: Operation is not valid due to the current state of the object. My Version Information: Microsoft .NET Framework Version: 2.0.50727.3634 ASP.NET Version: 2.0.50727.3634
asked Dec 2, 2019 alecxe 7.5k points
0 votes
1 answer 95 views
95 views
Problem : After compiling my C# code I am getting below error: error: An unhandled exception of type 'System.Net.WebException' occurred in System.dll Additional information: The underlying connection was closed: An unexpected error occurred on a receive. At the line HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse(); please let me know if anybody has solution on it.
asked Jan 18, 2020 jwilliam 3.9k points
0 votes
1 answer 74 views
74 views
Problem : My Query is giving the strange error as follows there is already an open datareader associated with this command which must be closed first.
asked Nov 15, 2019 peterlaw 6.9k points
0 votes
1 answer 185 views
185 views
Problem : I am getting following error in my error log file [18-Nov-2019 19:09:55 UTC] PHP Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [1040] Too many connections' in /home/root/products/db.php:2 Stack trace: /home/ ... ', 'root', '**somePassword**'); $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
asked Nov 20, 2019 peterlaw 6.9k points
0 votes
2 answers 292 views
292 views
Problem : Currently I am migrating from the Identity 1.0.0 to Identity 2.0.1. But the migrations code generated is nothing about the new IdentityUser. And It does not even add the new columns. So I tried to make the new project and also I ... current context At below line IdentityResult result = await UserManager.CreateAsync(user, model.Password); Any clue about the solution on my above problem?
asked Dec 25, 2019 alecxe 7.5k points
0 votes
1 answer 13 views
13 views
Can anyone help me with how to solve this issue? Any help is greatly appreciated.
asked Dec 27, 2020 TeamScript 9.5k points
0 votes
1 answer 25 views
25 views
Problem: I am a noob and I recently started learning programming. I was working with variables when I encountered an error that said: “name does not exists in current context”. I resolved the error but I want to know that in what circumstances this error can occur?
asked Nov 3, 2020 Code Learner 5.5k points
0 votes
1 answer 553 views
553 views
Problem : I am using the Visual Studio 2010 in a debug mode and I have "optimize code" unchecked. I am unable to quick watch (or hover on) any variable in my debugger. I encountered following error "Cannot evaluate expression because the code of the current method ... ' as it is not available at this instruction pointer, possibly because it has been optimized away." Am I missing any setting here ?
asked Jan 18, 2020 jwilliam 3.9k points