8

I'm using Visual Studio 2008, and my database is SQL Server 2000.

I want to add a connection to the Server Explorer in VS. The Data source is Microsoft SQL Server (SqlClient). After entering in all my information and I click Test Connection, it is successful.

But when I click OK, I get the error:

Unable to add data connection. ExecuteScalar requires an open and available connection. The connection's current state is closed.

2
  • That worked...what's the story behind that? Commented Sep 13, 2010 at 19:13
  • 2
    restarting VS fixed it for me Commented Oct 14, 2010 at 1:05

3 Answers 3

11

Restart Visual Studio. Then, restart your computer.

Sign up to request clarification or add additional context in comments.

3 Comments

restert VS first, that should be enough
For me restarting VS was enough
Same here. Restarted VS and all went fine. Thanx!
0

You can open Server Explorer (View -> Server Explorer) to re-connect the connection.

You may delete the current connection to open the same one again.

Comments

0

For those who are using a SQL Command and are getting the error "Unable to add data connection. ExecuteScalar requires an open and available connection. The connection's current state is closed." try this:

      using (SqlConnection conn = new SqlConnection(connString))
        {
            using (SqlCommand comm = new SqlCommand())
            {
                // query to select all the rows whose column name is the same as id
                comm.CommandText = "SELECT COUNT(*) from tableName where colName like @val1";
                comm.Connection = conn;
                conn.Open();  // <---- adding this line fixed the error for me
                comm.Parameters.AddWithValue("@val1", id);
                // retrieve how many rows are returned after executing the query
                count = (int)comm.ExecuteScalar();  // < --- where the error originally occurred
            }
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.