title: VB.Net Check if a checkbox list item is checked/unchecked
body: <p>I have a checkbox list which is filled with entries from my database on page load. I need to update an entry in my database when a item is checked and when an item is unchecked. Right now I am doing the following:</p>
  
  <pre><code>&lt;asp:CheckBoxList id="check1" AutoPostBack="True" TextAlign="Right" OnSelectedIndexChanged="Check" runat="server"&gt;
  
  &lt;/asp:CheckBoxList&gt;
  </code></pre>
  
  <p>And the function:</p>
  
  <pre><code>Sub Check(ByVal sender As Object, ByVal e As EventArgs)
      Dim sql As String
      If check1.SelectedItem.Selected = True Then
          sql = "UPDATE Customer SET unauthorized = 'True' WHERE ID = @ID"
          db.execUpdateQuery(sql, New SqlClient.SqlParameter("@ID", check1.SelectedItem.Value))
      Else
          sql = "UPDATE Customer SET unauthorized = 'False' WHERE ID = @ID"
          db.execUpdateQuery(sql, New SqlClient.SqlParameter("@ID", check1.SelectedItem.Value))
      End If
  End Sub
  </code></pre>
  
  <p>But when I uncheck a button the else case is never ran. Also, if I have 4 buttons checked and I uncheck all four buttons, when I uncheck the fourth button I get an error at the line:</p>
  
  <pre><code>If check1.SelectedItem.Selected = True Then
  </code></pre>
  
  <p>The error is: "Object reference not set to an instance of an object."</p>
  
  <p>Is there a better way to check if a list item is checked or unchecked? </p>
  
  <p>Thanks</p>