Quantcast
Channel: DataSource Controls - SqlDataSource, ObjectDataSource, etc.
Viewing all articles
Browse latest Browse all 956

How to avoid duplicate items being added from one CheckBoxList to another CheckBoxList ? Urgently

$
0
0

I  got 4checkboxlists , each checkbox has 6 checkboxes inside it (below is my database)

checkboxlist1 : rowa

checkboxlist2:rowb

checkboxlist3:rowc

checkboxlist4:rowd

id       : int
rowa : bit
rowb : bit
rowc : bit
rowd : bit
rowtext:nvarchar(50)

My problem is , When adding items from one CheckBoxList  to another CheckBoxList

 ,how to check if the item is already present in the second  , third , forth  CheckBoxList ?

this is mycode 

 Protect sub_button1

'A CheckBoxList
        For a 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 For 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 For 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

<asp:Panel ID="Panel1" runat="server" Height="463px"><asp:CheckBoxList ID="CheckBoxList1" runat="server"><asp:ListItem>A1</asp:ListItem><asp:ListItem>A2</asp:ListItem><asp:ListItem>A3</asp:ListItem><asp:ListItem>A4</asp:ListItem><asp:ListItem>A5</asp:ListItem><asp:ListItem>A6</asp:ListItem></asp:CheckBoxList><br /><br /><br /><br /><br /><asp:CheckBoxList ID="CheckBoxList2" runat="server"><asp:ListItem>B1</asp:ListItem><asp:ListItem>B2</asp:ListItem><asp:ListItem>B3</asp:ListItem><asp:ListItem>B4</asp:ListItem><asp:ListItem>B5</asp:ListItem><asp:ListItem>B6</asp:ListItem></asp:CheckBoxList><asp:CheckBoxList ID="CheckBoxList3" runat="server"><asp:ListItem>C1</asp:ListItem><asp:ListItem>C2</asp:ListItem><asp:ListItem>C3</asp:ListItem><asp:ListItem>C4</asp:ListItem><asp:ListItem>C5</asp:ListItem><asp:ListItem>C6</asp:ListItem></asp:CheckBoxList><asp:CheckBoxList ID="CheckBoxList4" runat="server"><asp:ListItem>D1</asp:ListItem><asp:ListItem>D2</asp:ListItem><asp:ListItem>D3</asp:ListItem><asp:ListItem>D4</asp:ListItem><asp:ListItem>D5</asp:ListItem><asp:ListItem>D6</asp:ListItem></asp:CheckBoxList><br /><asp:Button ID="Button1" runat="server" Text="Save" OnClick="Button1_Click" /></asp:Panel>

Viewing all articles
Browse latest Browse all 956

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>