如何避免重复项从一个CheckBoxList添加到另一个CheckBoxList(Asp.net vb.net)

时间:2014-02-24 14:37:43

标签: asp.net vb.net

我有5个复选框列表。每个复选框里面有6个复选框(下面是我的数据库)

checkboxlist1:rowa

checkboxlist2:rowB中

checkboxlist3:ROWC

checkboxlist4:rowd

checkboxlist5:罗

  

id:int,   罗达:有点,   rowb:位,   rowc:bit,   rowd:位,   rowe:bit,

rowtext:为nvarchar(50)

我的问题是,将一个CheckBoxList中的项添加到另一个CheckBoxList

,如何检查项目是否已存在于第二,第三,第四,第五个CheckBoxLists中?

这是我的代码

    Protect sub_button1

'一个CheckBoxList         对于As Integer = 0 To CheckBoxList1.Items.Count - 1

        If CheckBoxList1.Items(a).Selected Then
            rowtext = CheckBoxList1.Items(a).Text
            rowa = 1
            rowb = 0
            rowc= 0
            rowd= 0

            Dim insertSql As String = "INSERT INTO tbtest(rowtext,rowa,rowb,rowc,rowd,) VALUES(@rowtext,@rowa,@rowb,@rowc,@rowd)"

            Using myConnection As New SqlConnection(connectionString)
                myConnection.Open()
                Dim myCommand As New SqlCommand(insertSql, myConnection)
                myCommand.Parameters.AddWithValue("@rowtext", SqlDbType.Bit).Value = rowtext
                myCommand.Parameters.AddWithValue("@rowa", SqlDbType.Bit).Value = rowa
                myCommand.Parameters.AddWithValue("@rowb", SqlDbType.Bit).Value = rowb
                myCommand.Parameters.AddWithValue("@rowc", SqlDbType.Bit).Value = rowc
                myCommand.Parameters.AddWithValue("@rowd", SqlDbType.Bit).Value = rowd
                myCommand.ExecuteNonQuery()
                myConnection.Close()
            End Using
        End If

    Next

    'B CheckBoxList
    For b As Integer = 0 To CheckBoxList2.Items.Count - 1

        If CheckBoxList2.Items(b).Selected Then
            rowtext = CheckBoxList2.Items(b).Text
             rowa = 0
            rowb = 1
            rowc= 0
            rowd= 0
            Dim insertSql As String = "INSERT INTO tbtest(rowtext,rowa,rowb,rowc,rowd) VALUES(@rowtext,@rowa,@rowb,@rowc,@rowd)"

            Using myConnection As New SqlConnection(connectionString)
                myConnection.Open()
                Dim myCommand As New SqlCommand(insertSql, myConnection)
                myCommand.Parameters.AddWithValue("@rowtext", SqlDbType.Bit).Value = rowtext
                myCommand.Parameters.AddWithValue("@rowa", SqlDbType.Bit).Value = rowa
                myCommand.Parameters.AddWithValue("@rowb", SqlDbType.Bit).Value = rowb
                myCommand.Parameters.AddWithValue("@rowc", SqlDbType.Bit).Value = rowc
                myCommand.Parameters.AddWithValue("@rowd", SqlDbType.Bit).Value = rowd
                myCommand.ExecuteNonQuery()
                myConnection.Close()
            End Using
        End If

    Next

'C CheckBoxList         对于c As Integer = 0 To CheckBoxList3.Items.Count - 1

        If CheckBoxList3.Items(c).Selected Then
            rowtext = CheckBoxList3.Items(c).Text
             rowa = 0
            rowb = 0
            rowc= 1
            rowd= 0
            Dim insertSql As String = "INSERT INTO tbtest(rowtext,rowa,rowb,rowc,rowd) VALUES(@rowtext,@rowa,@rowb,@rowc,@rowd)"

            Using myConnection As New SqlConnection(connectionString)
                myConnection.Open()
                Dim myCommand As New SqlCommand(insertSql, myConnection)
                myCommand.Parameters.AddWithValue("@rowtext", SqlDbType.Bit).Value = rowtext
                myCommand.Parameters.AddWithValue("@rowa", SqlDbType.Bit).Value = rowa
                myCommand.Parameters.AddWithValue("@rowb", SqlDbType.Bit).Value = rowb
                myCommand.Parameters.AddWithValue("@rowc", SqlDbType.Bit).Value = rowc
                myCommand.Parameters.AddWithValue("@rowd", SqlDbType.Bit).Value = rowd
                myCommand.ExecuteNonQuery()
                myConnection.Close()
            End Using
        End If

    Next

'D CheckBoxList         对于d As Integer = 0 To CheckBoxList4.Items.Count - 1

        If CheckBoxList4.Items(d).Selected Then
            rowtext = CheckBoxList4.Items(d).Text
             rowa = 0
            rowb = 0
            rowc= 0
            rowd= 1
            Dim insertSql As String = "INSERT INTO tbtest(rowtext,rowa,rowb,rowc,rowd) VALUES(@rowtext,@rowa,@rowb,@rowc,@rowd)"

            Using myConnection As New SqlConnection(connectionString)
                myConnection.Open()
                Dim myCommand As New SqlCommand(insertSql, myConnection)
                myCommand.Parameters.AddWithValue("@rowtext", SqlDbType.Bit).Value = rowtext
                myCommand.Parameters.AddWithValue("@rowa", SqlDbType.Bit).Value = rowa
                myCommand.Parameters.AddWithValue("@rowb", SqlDbType.Bit).Value = rowb
                myCommand.Parameters.AddWithValue("@rowc", SqlDbType.Bit).Value = rowc
                myCommand.Parameters.AddWithValue("@rowd", SqlDbType.Bit).Value = rowd
                myCommand.ExecuteNonQuery()
                myConnection.Close()
            End Using
        End If

    Next



    Catch ex As Exception


    End Try

End Sub

1 个答案:

答案 0 :(得分:0)

我会在将它们放入数据库之前使用递归循环来检查所有框。

我可以在最短的时间内编写代码。

     Dim listofCheckBoxLists As New List(Of CheckedListBox)
'first to gather the info
For i = 1 To 4
    Dim checkboxlists As CheckedListBox = Me.Controls.Find("CheckedListBox" & i, True)(0)
    listofCheckBoxLists.Add(checkboxlists)
Next

'now to go through it all
For a = 0 To listofCheckBoxLists.Count - 1
    For b = 0 To listofCheckBoxLists(a).Items.Count - 1
        For c = a To listofCheckBoxLists.Count - 1
            For d = 0 To listofCheckBoxLists(c).Items.Count - 1
                If listofCheckBoxLists(a).Items(b) = listofCheckBoxLists(c).Items(d) Then
                    '''here should be where if they are the same you can do something
                End If
            Next
        Next
    Next
Next