Re: Close not closing...

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



Put your close code inside a finally and add a catch. If you encounter an
exception prior to your close code, that code will not run

string connectionString;
connectionString = Properties.Settings.Default.DatabaseConnectionString +
databaseName;

// create a new SqlConnection object with the appropriate
connection string SqlConnection sqlConn = new
SqlConnection(connectionString);

using sqlConn
{
try
{
// open the connection
sqlConn.Open();

// create the command object
SqlCommand sqlComm = new SqlCommand(SQLCommands, sqlConn);
sqlComm.ExecuteNonQuery();

MessageBox.Show("Operation Completed");
}
catch
{
// handle exceptions here
}
finally
{
// close the connection
sqlConn.Close();
}
}


"theinvisibleGhost" <theinvisibleGhost@xxxxxxxxx> wrote in message
news:1170091502.205177.158340@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
I've written a small app which has 2 buttons.

The 1st button goes away to a directory full of SQL Scripts, and then
runs them against the database.

The 2nd button has code to do some modifications to the database,
looking like the code below:
_______________________________________________________________________
_________
string connectionString;
connectionString =
Properties.Settings.Default.DatabaseConnectionString + databaseName;

// create a new SqlConnection object with the appropriate
connection string
SqlConnection sqlConn = new
SqlConnection(connectionString);
try
{
// open the connection
sqlConn.Open();

// create the command object
SqlCommand sqlComm = new SqlCommand(SQLCommands,
sqlConn);
sqlComm.ExecuteNonQuery();

// close the connection
sqlConn.Close();
MessageBox.Show("Operation Completed");
}
_______________________________________________________________________
_________

One of the scripts run from the script folder Drops a user, and then
Re-Creates them.
If I hit the script button first, then everything works fine.
However if I run the code button first, and then hit the script
button, an exception is thrown
saying that the user can not be dropped since they are currently
logged in!
I'm guessing I'm missing something, but it appears the Close method on
the sqlConnection object isn't
actually closing the connection...

Terminating the application and starting again resets the problem,
however there is nothing in
the dispose methods which would do that.

I've tried exchanging Close for Dispose but it made no difference.
Any help would be appreciated.
Cheers
Chris.
saying that a user is



.


Quantcast