I have an error messege. There is already an open DataReader associated with this Command which must be closed first [closed]

StackOverflow https://stackoverflow.com/questions/22138253

  •  19-10-2022
  •  | 
  •  

Question

 private void button6_Click(object sender, EventArgs e)
    {
        SqlConnection MySqlConnection;
        SqlDataReader m_dr;
        SqlCommand command;
        DataTable p_table = new DataTable();

        MySqlConnection = new SqlConnection("Data Source=A-A-PC\\MSSQLSERVER1;Initial Catalog=lights and sounds;User ID=sa;Password=itexpert;");

        MySqlConnection.Open();
        command = new SqlCommand("SELECT * FROM inventory  WHERE package='" + textBox7.Text + "'", MySqlConnection);
        m_dr = command.ExecuteReader();

        if (m_dr.HasRows)
        {
            SqlCommand command1 = new SqlCommand("Select * from inventory", MySqlConnection);
            p_table.Clear();
            SqlDataAdapter m_d = new SqlDataAdapter("Select * from inventory", MySqlConnection);

            m_d.Fill(p_table);
            listView1.Items.Clear();

            for (int i = 0; i < p_table.Rows.Count; i++)
            {
                DataRow drow = p_table.Rows[i];
                if (drow.RowState != DataRowState.Deleted)
                {
                    ListViewItem lvi = new ListViewItem(drow["id"].ToString());
                    lvi.SubItems.Add(drow["package"].ToString());
                    lvi.SubItems.Add(drow["number"].ToString());
                    listView1.Items.Add(lvi);
                }
            }
        }
    }

No correct solution

OTHER TIPS

You must enable Multiple Active Result Sets on your connection:

MySqlConnection = new SqlConnection("Data Source=A-A-PC\\MSSQLSERVER1;Initial Catalog=lights and sounds;User ID=sa;Password=itexpert;MultipleActiveResultSets=True");

Cheers

Add "MultipleActiveResultSets=True; in your connection string

Multiple Active Result Sets (MARS):

Multiple Active Result Sets(MARS) is a feature that works with SQL Server to allow the execution of multiple batches on a single connection. When MARS is enabled for use with SQL Server, each command object used adds a session to the connection.

Using Multiple Active Result Sets (MARS):

SQL Server 2005 introduced support for multiple active result sets (MARS) in applications accessing the Database Engine. In earlier versions of SQL Server, database applications could not maintain multiple active statements on a connection. When using SQL Server default result sets, the application had to process or cancel all result sets from one batch before it could execute any other batch on that connection. SQL Server 2005 introduced a new connection attribute that allows applications to have more than one pending request per connection, and in particular, to have more than one active default result set per connection.

Enabling Multiple Active Result Sets :

SQL Server 2005 has so many new features that in my opinion if you read only BOL for a year you'd find something new every day. One of those is Multiple Active Result Sets or MARS. Multiple Active Result Sets is a new SQL Server 2005 feature that, putting it simply, allows the user to run more than one SQL batch on an open connection at the same time.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top