---
title: "SQL Connection Open Exception"  
description: "SQL Connection Open Exception"  
author: "Anonymous User"  
published: 2014-12-11  
updated: 2014-12-11  
canonical: https://www.mindstick.com/forum/12779/sql-connection-open-exception  
category: "asp.net"  
tags: ["database", "sql server"]  
reading_time: 2 minutes  

---

# SQL Connection Open Exception

I am working on a [project](https://www.mindstick.com/articles/105927/how-to-excel-at-managing-multiple-projects) which, up [until today](https://answers.mindstick.com/qa/35860/until-today-what-are-the-greatest-advances-in-ai-artificial-inteligence), has been fine. However now when I run it and it goes through a few different [Stored Procedure](https://www.mindstick.com/articles/803/using-stored-procedure-in-asp-dot-net) calls it is throwing an InvalidOperationException with the message The [connection](https://www.mindstick.com/articles/13012/what-makes-ethernet-connection-better-than-wifi) was not closed. The connection's [current state](https://www.mindstick.com/forum/158105/what-is-the-current-state-of-research-and-development-in-nanotechnology) is open.

I get that I could put in a check to see if the connection is already open, but this code hasn't changed (it is under [Version Control](https://www.mindstick.com/articles/333367/version-control-best-practices-with-tfs) and isn't modified) so I'm looking for other potential explanations.

Could there be some lock in SQL which isn't being released? Is there a [process](https://yourviews.mindstick.com/story/1525/7-important-factors-that-may-affect-the-learning-process) which I should look out for and kill?

I can't really post the code as there is a lot of it, and it is split up into smaller [methods](https://www.mindstick.com/articles/13060/runny-nose-remedy-methods-that-work-best) which makes it harder to pull out individual items. E.g:

```
public SqlConnection Connection{    get    {        this._connection.Open();        return _connection;    }} public IDataReader RetrieveRecord(int Id){    //SP    SqlCommand cmd = this.Connection.CreateCommand();cmd.CommandText = "SelectRecord";    cmd.CommandType = CommandType.StoredProcedure;     //Parameters     cmd.Parameters.Add(new SqlParameter("@tID", Id));     //instruct the data reader to close its connection when its Close method is         called by passing the CommandBehavior.CloseConnection     return cmd.ExecuteReader(CommandBehavior.CloseConnection);}
```

Nothing massively complex, I just don't understand why this connection is now throwing an exception.

## Replies

### Reply by Allen Scott

When you are talking about killing process, I think you know c# well.

You should always use

```
using(){ }Like: using(SqlConnection con=new SqlConnection("connectionString")){  // Do something with con     using(SqlCommand cmd=new SqlCommand("cmdText",con))     {        // Do something with cmd     }}
```

You know that SqlCommand and SqlConnection implement IDisposable

So when you put those objects within using, the connection closing and clean up job is automatically done.

No need to close the connection manually in the code, since using will do the work for you.

### Reply by Anonymous User

Problem is with the line

this._connection.Open();

Because you are trying to open an already opened connection.

Try this to check before opening a connection:

if (this._connection.State == ConnectionState.Closed)

this._connection.Open();


---

Original Source: https://www.mindstick.com/forum/12779/sql-connection-open-exception

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
