1

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:

<asp:CheckBoxList id="check1" AutoPostBack="True" TextAlign="Right" OnSelectedIndexChanged="Check" runat="server">

</asp:CheckBoxList>

And the function:

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

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:

If check1.SelectedItem.Selected = True Then

The error is: "Object reference not set to an instance of an object."

Is there a better way to check if a list item is checked or unchecked?

Thanks

3
  • With old school HTML unchecked checkboxes don't return values to the server...maybe that's what you're running into here? With nothing checked there would be no SelectedItem in your checkboxlist. Commented May 14, 2012 at 17:27
  • Shouldn't you be basing this on ItemCheck and not SelectedIndexChange? Commented May 14, 2012 at 17:28
  • @APrough I tried that out and it doesn't run the function Commented May 14, 2012 at 17:31

2 Answers 2

1
for (int i = 0; i < check1.Items.Count; i++)
  if (check1.GetItemChecked(i))
    // Do selected stuff
  else
    // Do unselected stuff

If the the check is in indeterminate state, this will still return true. You may want to replace

if (check1.GetItemChecked(i))

with

if (check1.GetItemCheckState(i) == CheckState.Checked)

if you want to only include actually checked items.

Sign up to request clarification or add additional context in comments.

7 Comments

Is there anyway to avoid the use of a for loop?
@ewein Why do you want to avoid a For loop?
its not very efficient to loop through each item and do a database update on each iteration.
I just need something that allows me to get the item that was checked or unchecked then I can do a single update.
Now check the modified answer. clbIncludes was the check box list.
|
1

You can try this also

   For i As Integer = 0 To CheckedListBox1.Items.Count - 1
        If CheckedListBox1.GetItemChecked(i) = 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
    Next

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.