我有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
謝謝您的回答,但我編碼Web表單上,我想只顯示不重複的值,我該怎麼辦呢? – user3347082
然後它的me.findcontrol和你的副本將在if語句中 – Ishey4