Click here to Skip to main content
15,895,777 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
message system.invalidoperationexception: the connection was not closed current state is open. at system.data.providerbase.dbconnectioninternal.openconnection(dbconnection outerconnection,dbconnectionfactory connectionfactory) at system.data.sqlclient.sqlconnection.open() at pay_roll.show_in_datagridview()
Posted
Updated 14-Nov-11 0:17am
v2
Comments
Mehdi Gholam 14-Nov-11 5:53am    
And your question is?
Richard MacCutchan 14-Nov-11 6:18am    
Well there is enough information there to tell you what method and program caused the error. Check your source code and see if you can understand what is going on.
Smithers-Jones 14-Nov-11 7:29am    
Not a question.

Add this code
C#
if (conn.State == ConnectionState.Open)
                conn.Close();


before you open the connection.
 
Share this answer
 
as I understood, you are going to fill a grid and getting the error.

as per the error description, you are trying to open a already opened sql connection inside your method - > pay_roll.show_in_datagridview().

so add a check before opening the connection

C#
SqlConnection myConnection = new ...(...);
if (myConnection.State == ConnectionState.Closed)
{
    myConnection.Open();
}


alternatively you can use using keyword while working with unmanaged resources like sqlConnection, which will enforce the propper closing of the resources after use.

mark as answer if satisfied... :) it motivates.. :)
 
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