Click here to Skip to main content
15,896,497 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
It saving now if check 1 checkbox but if checked 2 checkboxes, the output display only 1 value. Thanks

What I have tried:

VB
Dim chcboxes() As CheckBox = {CheckBox1, CheckBox2, CheckBox3}

For i As Integer = 0 To chcboxes.Length - 1

 If (chcboxes(i).CheckState = CheckState.Checked = True) Then
       i += 1
            cmd.CommandText = "insert into array(array) values ( @array)"
            cmd.Parameters.AddWithValue("@array", i)
            cmd.ExecuteNonQuery()
        End If
    Next
Posted
Updated 25-Jan-19 22:16pm
v2
Comments
Bryian Tan 24-Jan-19 22:24pm    
Where is the code doing the display?
Member 14127874 24-Jan-19 22:37pm    
I dont include it there.
Bryian Tan 24-Jan-19 23:28pm    
Were the selected checkbox being inserted correctly into the table?
Member 14127874 25-Jan-19 0:24am    
Yes
I want an out put like checkbox1 = 1, chekbox2=2 and so on.


checkbox1-1
checkbox2-2
Member 14127874 25-Jan-19 0:25am    
My code display

checkbox1 - 1 2. Its wrong. It should be like this.

Checkbox1-1
Checkbox1-2

1 solution

Try This One :

You must put all your Checkboxes in a Control Container like a PANEL, in here I used a Panel.

Dim ChckboxValue As String

        For Each Control In Panel1.Controls
            If TypeOf Control Is CheckBox Then
                If CType(Control, CheckBox).Checked Then
                    ChckboxValue = CType(Control, CheckBox).Text
/*--------------- P U T  Y O U R  Q U E R Y H E R E ------------*/
                End If
            End If
        Next


Put your Query inside the Loop. In this code you don't need to declare the checkboxes inside an array.
 
Share this answer
 
v3
Comments
The Cool Cat 27-Jan-19 21:57pm    
https://www.codeproject.com/Answers/1275600/How-to-save-checkbox-in-SQL-server#answer1
CHill60 6-Feb-19 6:25am    
Why do the checkboxes have to be in a panel? The form can be the container
The Cool Cat 6-Feb-19 19:29pm    
The form itself would be great, but if he puts another checkbox that was not included to be passed in the query then other container can be used. But it also depends on how he will use it. It's his choice though.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900