Click here to Skip to main content
15,891,248 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a part of code as following and it keep showing an error "The connection was not closed. The connection's current state is open.

C#
private void load_gridview()
    {
        con.Open();
        SqlCommand cmd = new SqlCommand("SP_Display_Periodicals", con);
        cmd.CommandType = CommandType.StoredProcedure;
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable t1 = new DataTable();
        da.Fill(t1); 
        con.Close();

        if (t1.Rows.Count > 0)
        {
            GridView1.DataSource = t1.DefaultView;
            GridView1.DataBind();
        }

        else
        {
            GridView1.DataSource = null;
            GridView1.DataBind();
        }
        
    }
}






What I have tried:

I've closed the connection but it still shows an error "The connection was not closed. The connection's current state is open."
Posted
Updated 17-Mar-17 23:05pm

Check the rest of your code: somewhere you have left it open.
I would suggest two things:
1) Don't rely on a single "global" SqlConnection object: create them anew when you need them, and Dispose of them when you are done - a usingblock is a very good idea:
C#
using (SqlConnection con = new SqlConnection(strConnect))
   {
   con.Open();
   ...
   }
That way the system will handle Close and Dispose for you. You should also use using blocks with SqlCommand and other objects as well.
2) A try...catch...finally block is also a good idea!
 
Share this answer
 
this error means that you opened a connection and then try to open that Opened connection without closing the connections..
You need to check the connection State..

add this line before opening the connection.
if( con.State==ConnectionState.Closed)
           {
               con.Open();
           }
 
Share this answer
 
Quote:
error "The connection was not closed. The connection's current state is open.

the message tells you that you left the connection opened somewhere else in your code. Usually in the last part that opened one just before.
The debugger may help you.

When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900