Mathematics
Mathematics

Reputation: 7618

I must need to open two connections to execute two different queries?

Currently I am executing two queries against two different tables and getting this exception,

The connection was not closed. The connection's current state is open.

This is what I am trying to do,

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int userID = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values["userID"].ToString());
        string deleteStatement = "Delete from Table1 where userID=@userID";
        string deleteStatement2 = "Delete from Table2 where userID=@userID";

        using (SqlConnection connection = new SqlConnection(CS()))
        using (SqlCommand cmd = new SqlCommand(deleteStatement, connection))
        {
            connection.Open();
            cmd.Parameters.Add(new SqlParameter("@userID", userID));
            cmd.ExecuteNonQuery();

            using (SqlCommand cmd2 = new SqlCommand(deleteStatement2, connection))
            {
                connection.Open();
                cmd2.Parameters.Add(new SqlParameter("@userID", userID));
                int result2 = cmd2.ExecuteNonQuery();

                if (result2 == 1)
                {
                    BindData();
                }
            }
        }
    }

I am doing this because Table2 has userID as foreign key and must be deleted before deleting user actually

Upvotes: 0

Views: 98

Answers (5)

Frederik Terstappen
Frederik Terstappen

Reputation: 82

I think this will work:

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int userID = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values["userID"].ToString());
        string deleteStatement = "Delete from Table1 where userID=@userID";
        string deleteStatement2 = "Delete from Table2 where userID=@userID";

        using (SqlConnection connection = new SqlConnection(CS()))
        using (SqlCommand cmd = new SqlCommand())
        {
            cmd.Connection = connection;
            cmd.CommandType = CommandType.Text;

            cmd.CommandText = deleteStatement;
            cmd.Parameters.Add(new SqlParameter("@userID", userID));
            cmd.ExecuteNonQuery();


            cmd.CommandText = deleteStatement2;
            int result = cmd.ExecuteNonQuery();

            if (result == 1) BindData();
        }
    }

Upvotes: 0

Shafqat Masood
Shafqat Masood

Reputation: 2570

 protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
  {
    int userID = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values["userID"].ToString());
    string deleteStatement = "Delete from Table1 where userID=@userID";
    string deleteStatement2 = "Delete from Table2 where userID=@userID";

    using (SqlConnection connection = new SqlConnection(CS()))
    {
    connection.Open();

    using (SqlCommand cmd = new SqlCommand(deleteStatement, connection))
    {
        cmd.Parameters.Add(new SqlParameter("@userID", userID));
        cmd.ExecuteNonQuery();

    }
      using (SqlCommand cmd2 = new SqlCommand(deleteStatement2, connection))
        {
            cmd2.Parameters.Add(new SqlParameter("@userID", userID));
            int result2 = cmd2.ExecuteNonQuery();

            if (result2 == 1)
            {
                BindData();
            }
        }
     }
}

Upvotes: 0

Servy
Servy

Reputation: 203821

The second connection.Open(); doesn't need to be there. The first one is enough; as the error message says, it's already open.

One connection can perform multiple queries, with only a single call to Open.

Upvotes: 1

John Woo
John Woo

Reputation: 263703

you are calling Open() twice. You can remove the second call Open().

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
    int userID = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values["userID"].ToString());
    string deleteStatement = "Delete from Table1 where userID=@userID";
    string deleteStatement2 = "Delete from Table2 where userID=@userID";

    using (SqlConnection connection = new SqlConnection(CS()))
    using (SqlCommand cmd = new SqlCommand(deleteStatement, connection))
    {
        connection.Open();
        cmd.Parameters.Add(new SqlParameter("@userID", userID));
        cmd.ExecuteNonQuery();

        using (SqlCommand cmd2 = new SqlCommand(deleteStatement2, connection))
        {
            // connection.Open(); // remove this line
            cmd2.Parameters.Add(new SqlParameter("@userID", userID));
            int result2 = cmd2.ExecuteNonQuery();

            if (result2 == 1)
            {
                BindData();
            }
        }
    }
}

Upvotes: 3

Nikhil Agrawal
Nikhil Agrawal

Reputation: 48558

The second connection.Open() is not required as it will be open from first statement.

Still to be on the safer side, you can use

if (connection.State == ConnectionState.Closed)
    connection.Open();

Upvotes: 1

Related Questions