-1

データベースにチェックボックス項目を保存するプログラムを開発しています。しかし、それはchar単位で保存され、1つのアイテムしか取りません

私のチェックボックスの値は、データベースの文字ごとに保存されます。その問題を解決する方法

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    For Each item As Object In CheckedListBox1.SelectedItem
        qur = "insert into list(list) values('" & item.ToString & "')"
        cmd = New SqlCommand(qur, con)
        con.Open()
        cmd.ExecuteNonQuery()
        MsgBox("insert sucessfully")
        cmd.Dispose()
        con.Close()
    Next
End Sub
4

1 に答える 1

0

The SelectedItem property only gives first selected item and there is no SelectedItems property so you need to iterate over all items and check which item is selected:

Try
    con.Open()

    For Each item As ListItem In CheckedListBox1.Items
        If item.Selected Then
            qur = "insert into list(list) values('" & item.Text & "')"
            cmd = New SqlCommand(qur, con)
            cmd.ExecuteNonQuery()
            ...
        End If
    Next
Catch ex As Exception
    ...
Finally
    con.Close()
End Try

You also need to edit your database handling code as pointed out in comment.

于 2013-09-13T10:06:34.540 に答える