I am using VB.
I have code that works Inserting into a database individual records checked in CheckListBox control on a webform.
I need it to work on all the records selected, not just the first record, but I do not know how to write the code to loop through the selected records...
Any help would be most appreciated
I have code that works Inserting into a database individual records checked in CheckListBox control on a webform.
Code:
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If con.State = ConnectionState.Closed Then
con.Open()
End If
End Sub
Private Function GetCheckBoxListSelections() As String
Dim cblItems As String()
Dim cblSelections As New ArrayList()
For Each item As ListItem In CheckBoxList1.Items
If item.Selected Then
cblSelections.Add(item.Value)
End If
Next
cblItems = DirectCast(cblSelections.ToArray(GetType(String)), String())
Return String.Join(",", cblItems)
End Function
Protected Sub Button3_Click(sender As Object, e As EventArgs)
Dim str As [String] = ""
For i As Integer = 0 To CheckBoxList1.Items.Count - 1
If CheckBoxList1.Items(i).Selected Then
If str = "" Then
str = CheckBoxList1.Items(i).Value
Else
str += "," + CheckBoxList1.Items(i).Value
End If
End If
Next
Dim cmd As New SqlCommand("Insert into tblTagOnFly(CompanyID, TagOnFlyName) values('" & str & "','" & TextBox1.Text & "')", con)
cmd.ExecuteNonQuery()
Clear()
con.Close()
End Sub
Public Sub Clear()
CheckBoxList1.SelectedIndex = -1
End Sub
I need it to work on all the records selected, not just the first record, but I do not know how to write the code to loop through the selected records...
Any help would be most appreciated