Click here to Skip to main content
16,015,594 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i create a form and every time connection open and close but make a error asp.net


error: connection was closed when connection is already open
Posted

can you put your code so we can analze what is the problem
 
Share this answer
 
hi kamlesh

try this code


Sqlconnetion con;;
try
{
con=new Sqlconnection("connectiion string");
con.open();
}
catch
{
con.close();
}
finally
{}


If you get your answer then please rating me

:)
thanks
 
Share this answer
 
v2
hi kamlesh try this code out
add this namespace first

using System.Data.SqlClient;

sqlconnection con = new sqlconnection();
con.connectionstring="Data Source=.;Initial Catalog=ur database name;Integrated Security=True";
con.Open();
//Actions that u want to perform
con.Close();


use intelliscence help for writing name of the classes

What i feel kamlesh tat u might be closing a connection before any command executes

Please paste the code so that i can look and tell u.

Thanks & Regards
Radix :-\
 
Share this answer
 
Check the connection status before opening or closing that. code like below.
C#
Sqlconnetion sqlCon;
try
{
  sqlCon=new Sqlconnection("sql connection string");
  if(con.State!=ConnectionState.Open)
  {
    sqlCon.open();
  }
}
catch
{
  if(con.State!=ConnectionState.Closed)
  {
    sqlCon.close();
  }
}
finally
{
  if(con.State!=ConnectionState.Closed)
  {
    sqlCon.close();
  }
}
 
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