0

I have two CheckBoxList controls (chkListVideoMedia and chkListAudioMedia) on my page that I want to capture information from and insert the records into the database. I have it working for one of the controls, I just need help modifying my code below to include the second CBL

Dim values As New ArrayList()
For counter As Integer = 0 To chkListVideoMedia.Items.Count - 1
    If chkListVideoMedia.Items(counter).Selected Then
        MyTextBox.Text = chkListVideoMedia.Items(counter).Value
        values.Add(newId)
    End If
Next
If values.Count > 0 Then
  For item As Integer = 0 To values.Count - 1
    If item = 0 Then
      MyMedia1.Text = values(item).ToString
    End If
    If item = 1 Then
      MyMedia2.Text = values(item).ToString
    End If
    If item = 2 Then
      MyMedia3.Text = values(item).ToString
    End If
    If item = 3 Then
      MyMedia4.Text = values(item).ToString
    End If
  Next
End If

Thanks, James

5
  • I'm not sure I understand the question - you need help adding the code for the second control in what way? Are you trying to do it all in one loop? I don't think you can. Commented Sep 30, 2012 at 19:27
  • Are you wanting to put them in the same ArrayList? Commented Sep 30, 2012 at 20:18
  • @Tim I have one check box list for Videos files and one for Audio files, The user will check 2 from video and 2 from audio for example. When they click submit, the Selected Values of the checked items are inserted into my database. The above code works perfectly for the Video CBL, I just need to include the Audio CBL in the same record insertion. Another example is, they might only choose audio items. Commented Sep 30, 2012 at 20:46
  • @MarkHall - yes all in the one array, please see my comment above this one :) Commented Sep 30, 2012 at 20:47
  • Why not just simply repeat the code (with necessary changes) for the second CBL? Commented Sep 30, 2012 at 21:51

1 Answer 1

1

You can find out which Collection has the most Items, then check to make sure that count is not greater than the maximum Items in each collection. Something like this.

Dim values As New ArrayList()
Dim counter As Integer
If chkListVideoMedia.Items.Count > chkListAudioMedia.Items.Count Then
    counter = chkListVideoMedia.Items.Count - 1
Else
    counter = chkListAudioMedia.Items.Count - 1
End If
For x = 0 To counter
    If Not (counter > chkListVideoMedia.Items.Count - 1) Then
        'Do your work here
    End If
    If Not (counter > chkListAudioMedia.Items.Count - 1) Then
        'Do your work here
    End If
Next
Sign up to request clarification or add additional context in comments.

1 Comment

+1 Nice solution. The only thing I could come up with was brute force :)

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.