Note that there are some explanatory texts on larger screens.

plurals
  1. POBeginInvoke() occasionally breaks (System.InvalidOperationException) with Invalid attempt to call Read when reader is closed
    primarykey
    data
    text
    <p>In following code snippet, <code>AddRow()</code> is called from a non-UI thread:</p> <pre><code> public partial class Form1 : Form { public delegate void InvokeDelegate(); ... SqlConnection mSqlConnection = new SqlConnection("Data Source=" + Environment.MachineName + "\\SQLEXPRESS; Initial Catalog=orderDB; Integrated Security=TRUE; MultipleActiveResultSets=True;"); DataSet mDataSet = new DataSet(); SqlDataAdapter mSqlDataAdapter = new SqlDataAdapter(); ... private void UpdateGridView() { if (mSqlConnection.State == ConnectionState.Closed) mSqlConnection.Open(); mSqlDataAdapter.SelectCommand = new SqlCommand("SELECT * FROM customerTable", mSqlConnection); mDataSet.Clear(); mSqlDataAdapter.Fill(mDataSet); dataGridView1.DataSource = mDataSet.Tables[0]; if (mSqlConnection.State == ConnectionState.Open) mSqlConnection.Close(); } public void AddRow(int field1, int field2, int field3) { mSqlDataAdapter.InsertCommand = new SqlCommand("INSERT INTO customerTable VALUES(@field1, @field2, @field3)", mSqlConnection); mSqlDataAdapter.InsertCommand.Parameters.Add("@field1", SqlDbType.Int).Value = field1; mSqlDataAdapter.InsertCommand.Parameters.Add("@field2", SqlDbType.Int).Value = field2; mSqlDataAdapter.InsertCommand.Parameters.Add("@field3", SqlDbType.Int).Value = field3; mSqlConnection.Open(); mSqlDataAdapter.InsertCommand.ExecuteNonQuery(); dataGridView1.BeginInvoke(new InvokeDelegate(UpdateGridView)); // UpdateGridView() won't work from a non-UI thread mSqlConnection.Close(); } } </code></pre> <p>Before having to call <code>AddRow()</code> from a non-UI thread, I had <code>UpdateGridView()</code> being called directly and it worked flawlessly. But now <code>AddRow()</code> is no longer guaranteed to be called from a UI thread and so I replaced the direct call with <code>dataGridView1.BeginInvoke()</code>.</p> <p>As soon as I did that, my form-based application started throwing a <code>System.InvalidOperationException</code> every several <code>AddRow()</code> calls, breaking on the <code>mSqlDataAdapter.Fill(mDataSet);</code> statement (<strong>!</strong>) with the following message:</p> <blockquote> <p>Invalid attempt to call Read when reader is closed</p> </blockquote> <p>My question is why?</p> <ol> <li>What reader? the DataAdapter's? The SqlConnection's? The DataGridView's data source?</li> <li>I am taking care of surrounding <code>BeginInvoke()</code> with mSqlConnection's <em>Open()</em> and <em>Close()</em> and I even open mSqlConnection (again!) if it isn't open, so how come I'm receiving this "closed" error?</li> <li>What is the correct way to solve this problem? (i.e. updating DataGridView from a non-UI thread)</li> </ol>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload