2014-02-24 40 views
1

我有5個複選框列表。每個複選框在其內部具有6個複選框(下面是我的數據庫)如何避免重複的項目從一個CheckBoxList添加到另一個CheckBoxList(Asp.net vb.net)

checkboxlist1:ROWA

checkboxlist2:rowB中

checkboxlist3:ROWC

checkboxlist4:rowd

checkboxlist5:羅維

id:int, rowa:位, rowB中:位, ROWC:位, rowd:位, 羅維:位,

rowtext:爲nvarchar(50)

我的問題是,當添加從一個的CheckBoxList項到另一個CheckBoxList

,如何檢查項目是否已經存在於第二,第三,第四,第五個CheckBoxLists?

這是我的代碼

Protect sub_button1 

'A的CheckBoxList 對於作爲整數= 0至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爲整數= 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 

'CheckBoxList 對於d爲整數= 0要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 

回答

0

我只想用一個遞歸循環來檢查所有的箱子之前,我把它們放進DB。

我可以寫了一個分代碼..

 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 
+0

謝謝您的回答,但我編碼Web表單上,我想只顯示不重複的值,我該怎麼辦呢? – user3347082

+0

然後它的me.findcontrol和你的副本將在if語句中 – Ishey4

相關問題