title: NullReferenceException after displaying a table on DataGridView more than once
body: <p>Edit 2: Turns out it only gives a <code>NullReferenceException</code> the first time. After that, it says that I am not allowed to change the <code>ConnectionString</code> property, even though I think I have closed it in the right places.</p>
  
  <p>Thanks for taking the time to read this.</p>
  
  <p>I am working on a WinForms application that uses an MS Access database, and I am currently having problems with an entry deletion feature I made.</p>
  
  <p>So, I have a <code>DataGridView</code> that switches between 3 tables on a button click, and I have a function that deletes a row on a table that is currently open by clicking a button that is at the end of the row.</p>
  
  <p>When I open my first table, and try to delete a row, it works just fine. However, if I open a different table afterwards and try to delete an entry, or even go back to the first table I opened, I get a <code>NullReferenceException</code> in the deletion function.</p>
  
  <p>Here is the code to display one of the tables in <code>DataGridView</code>.</p>
  
  <pre><code>public DataTable Read()
      {
          connection.ConnectionString = connectionString;
          OpenConnection(); //connection.Open() inside an if statement
          dataTable.Clear();
          OleDbCommand readStudentCommand = new OleDbCommand("select * from Students", connection); //display the whole list of students
  
          OleDbDataReader reader = readStudentCommand.ExecuteReader();
          dataTable.Load(reader);
          connection.Close();
          return dataTable;        
      }
  </code></pre>
  
  <p>Here is the code that deletes an entry</p>
  
  <pre><code>    private void MainDataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
      {
          connection.ConnectionString = connectionString;
          if (ConnectionState.Closed == connection.State)
          {
              connection.Open();
          }
          var senderGrid = (DataGridView)sender;
          if (e.RowIndex &gt;= 0 &amp;&amp; senderGrid.Columns[e.ColumnIndex] == MainDataGridView.Columns["Delete"])
          {
  //this function retrieves the first column value of the deleted row, which has the ID of the entry (works with any table). 
      DeleteEntry(MainDataGridView.Rows[e.RowIndex].Cells[0].Value.ToString()); //exception thrown here (System.Windows.Forms.DataGridViewCell.Value.get returned null)
          MainDataGridView.Rows.RemoveAt(e.RowIndex);
       }
  </code></pre>
  
  <p>and here is <code>DeleteEntry()</code></p>
  
  <pre><code>private void DeleteEntry(string deletedID)
      {
          string tableName = null;
          string idType = null;
          if (studentsDisplayed)
          {
              tableName = "Students";
              idType = "Student ID";
          }
          else if(booksDisplayed)
          {
              tableName = "Books";
              idType = "BookID";
          }
          else if(loansDisplayed)
          {
              tableName = "Loans";
              idType = "Loan ID";
          }
          string deleteCommand = String.Format("DELETE * FROM {0} WHERE [{1}] = {2}", tableName, idType, deletedID);
          OleDbCommand deleteEntryCommand = new OleDbCommand(deleteCommand, connection);
          deleteEntryCommand.ExecuteNonQuery();
          SaveData(); //this method just calls Update() in a dataAdapter of a relevant table
          connection.Close();
      }
  </code></pre>
  
  <p>Thank you!</p>
  
  <p>Edit:
  As per request, here is the code that switches the table. It simply references the first function and sets the returned <code>dataTable</code> as <code>DataSource</code>.</p>
  
  <pre><code>private void StudentButton_Click(object sender, EventArgs e) //display students
      {
          try
          {
              if (!studentsDisplayed)
              {
                  MainDataGridView.DataSource = studentDAL.Read(); //studentDAL is the class that works with the Students table of my DB.
                  studentsDisplayed = true; //these 3 are to avoid duplicated creation of the same table
                  booksDisplayed = false;
                  loansDisplayed = false;
                  ComboBoxChanger(); //don't mind this, it's for an irrelevant feature
                  CreateButtons(5);
              }
          }
          catch
          {
              throw;
          }
      }
  </code></pre>