title: 'accessibilityobject' threw an exception of type 'system.invalidoperationexception'
body: <p>There are a lot of questions on SO about <code>'system.invalidoperationexception</code>' but none of them have helped me figure this out.</p>
  
  <p>I have a <code>datatable</code> (<code>tab2tableExtra</code>) that gets its values from another <code>datatable</code> (<code>tab2table</code>), which in turn gets is values from a database. <code>tab2tableExtra</code> only contains 4 rows and 8 columns and all its values (barring column [0]) are calculated from <code>tab2table</code>. Specifically two columns in <code>tab2table</code> are used to drive a for loop and those seem to be at the crux of the problem. These are <code>MLevel</code> and <code>RLevel</code>, which are shown in the code below.</p>
  
  <p>Everything works perfectly when both the <code>MLevel</code> column and <code>RLevel</code> column have some non-null values. But if one of those columns is all nulls (<code>DBNull.Value</code> -which can happen on occasion), then something seems to go wrong. Interestingly, it doesn't actually cause the code to break, but it seems to mess with the calculated values in <code>tab2tableExtra</code>, as the entire table is null (except for column [0], which is just 1, 2, 3, 4).</p>
  
  <p>Upon debugging, when either <code>MLevel</code> or <code>RLevel</code> (but not both) contains all null I see a 'system.invalidoperationexception<code>from</code>this`, as shown in the figure below. What causes the code to do is break out of the for loop after one iteration, and then not perform any of the rest of calculations.
  <a href="https://i.stack.imgur.com/PBsPq.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/PBsPq.png" alt="Error messages"></a></p>
  
  <p>This message comes from the following code:</p>
  
  <pre><code>// Add values to tab2tableExtra
                  // Add Levels columns
                  DataRow row = tab2tableExtra.NewRow();
                  for (int i = 1; i &lt; 5; i++)
                  {
                      row = tab2tableExtra.NewRow();
                      row["Level"] = i;
                      tab2tableExtra.Rows.Add(row);
                  }
                  // Add participation rate column values
                  DataRow dr = tab2tableExtra.Rows[0];
                  int rowCount = tab2table.Rows.Count;
  
                  /*int countnumM = tab2table.AsEnumerable().Where(x =&gt; int.Parse(x["MLevel"].ToString()) == 1 || 
                                  int.Parse(x["MLevel"].ToString()) == 2 || int.Parse(x["MLevel"].ToString()) == 3 || 
                                  int.Parse(x["MLevel"].ToString()) == 4).ToList().Count;*/
                  int countnumM = tab2table.AsEnumerable().Select(x =&gt; int.TryParse(x["MLevel"].ToString(), out var d) ? d : (int?)null).Where(x =&gt; x &gt;= 1 &amp;&amp; x &lt;= 4).Count();
                  int countnumRW = tab2table.AsEnumerable().Select(x =&gt; int.TryParse(x["RLevel"].ToString(), out var d) ? d : (int?)null).Where(x =&gt; x &gt;= 1 &amp;&amp; x &lt;= 4).Count();
  
                  /*int countnumRW = tab2table.AsEnumerable().Where(x =&gt; int.Parse(x["RLevel"].ToString()) == 1 || int.Parse(x["RLevel"].ToString()) == 2 ||
                                                      int.Parse(x["RLevel"].ToString()) == 3 || int.Parse(x["RLevel"].ToString()) == 4).ToList().Count;*/
  
                  for (int i = 1; i &lt; 5; i++)
                  {
                      if (countnumM &gt; 0)
                      {
                          float levelPercM = Convert.ToInt32(tab2table.Compute("COUNT(MLevel)", "MLevel =" + i.ToString()));
                          tab2tableExtra.Rows[i - 1][1] = Math.Round(100 * levelPercM / countnumM, 2);
                      }
                      else
                          tab2tableExtra.Rows[i - 1][1] = null;
                      if (countnumRW &gt; 0)
                      {
                          decimal levelPercRW = Convert.ToDecimal(tab2table.Compute("COUNT([RLevel])", "RLevel =" + i.ToString()));
                          tab2tableExtra.Rows[i - 1][2] = Math.Round(100 * levelPercRW / countnumRW, 2);
                      }
                      else
                          tab2tableExtra.Rows[i - 1][2] = null;
                  }
                  // Add the rest of the column values that only require a single number
                  tab2tableExtra.Rows[0][3] = rowCount;
                  if (countnumM &gt; 0)
                      tab2tableExtra.Rows[0][4] = 100*countnumM/rowCount;
                  else
                      tab2tableExtra.Rows[0][4] = null;
                  if (countnumRW &gt; 0)
                      tab2tableExtra.Rows[0][5] = 100*countnumRW/rowCount;
                  else
                      tab2tableExtra.Rows[0][5] = null;
                  decimal RWavg = Convert.ToDecimal(tab2table.Compute("AVG([ROverall])", ""));
                  decimal Mavg = Convert.ToDecimal(tab2table.Compute("AVG([MOverall])", ""));
  
                  tab2tableExtra.Rows[0][6] = RWavg;
                  tab2tableExtra.Rows[0][7] = Mavg;
  </code></pre>
  
  <p>For reference, when I debug with a <code>tab2table</code> that has at least one non-null value in both <code>MLevel</code> and <code>RLevel</code>, I see this:
  <a href="https://i.stack.imgur.com/wuEIk.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/wuEIk.png" alt="No Errors"></a></p>
  
  <p>If anyone knows how to solve this, or even just why this happening, it would be very useful.</p>