• Register
0 votes
30 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

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 269 views
269 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 jwilliam 3.9k points
0 votes
1 answer 48 views
48 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 75 views
75 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 jwilliam 3.9k points
0 votes
1 answer 65 views
65 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
2 answers 212 views
212 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