Click here to Skip to main content
15,906,333 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
The output of this code is like

10 20 30. It wrong!!!!

I want an out like this

10
20
30

Here's my code!

What I have tried:

Try
Dim con As New SqlClient.SqlConnection("data source=DESKTOP-9NE9FIF\SQLEXPRESS;initial catalog=Sample1;Integrated Security=True")
Dim cmd As New SqlCommand()


cmd.Connection = con

con.Open()

Dim str, str1 As String
str = ""
str1 = ""

If CheckBox1.Checked = True Then
str &= Label1.Text + vbCrLf
str &= " "

End If

If CheckBox2.Checked = True Then
str &= Label2.Text + vbCrLf
str &= " "


End If
If CheckBox3.Checked = True Then
str &= Label3.Text + vbCrLf
str &= " "

End If
If CheckBox4.Checked = True Then
str &= Label4.Text + vbCrLf
str &= " "


End If

If str <> Nothing Then
MsgBox("Record is successfully saved!", MsgBoxStyle.Information, "Success")
End If
Dim chcboxes() As CheckBox = {CheckBox1, CheckBox2, CheckBox3, CheckBox4}
For i As Integer = 0 To chcboxes.Length - 1
If (chcboxes(i).CheckState = CheckState.Checked) Then
cmd.CommandText = "insert into array_tbl(array_id) values ( @arrayid)"
cmd.Parameters.AddWithValue("@arrayid", str)
cmd.ExecuteNonQuery()

End If

Next

Catch ex As Exception
'(ex.message)
End Try
Posted
Updated 27-Jan-19 15:33pm
Comments
The Cool Cat 27-Jan-19 21:03pm    
What datatypes are you using for the data you are saving?
Member 14127874 28-Jan-19 19:25pm    
The data type i've used is varchar but i will change it to integer because there is an corresponding number on the checkboxes. Like checkbox1 =21 and so on.
The Cool Cat 30-Jan-19 4:24am    
If you want to save your data like this :
10
20
30
Then try using Text as your datatype.
Because when you use Integer or any numeric datatype the output will be like this:
10 20 30 or 102030

1 solution

Try this One:

I use "Text" as the datatype in my Database to ensure that the numbers will be saved with new lines like what you wish to do.
10
20
30
...
Hope this helps.

Dim str As String
        str = _
        IIf(CheckBox1.Checked = True, Label1.Text & " ", "") & _
        IIf(CheckBox2.Checked = True, Label2.Text & " ", "") & _
        IIf(CheckBox3.Checked = True, Label3.Text & " ", "") & _
        IIf(CheckBox4.Checked = True, Label4.Text, "")
        str = Replace(str, " ", vbCrLf)

        con.Open()
        cmd.Connection = con
        cmd.CommandText = "INSERT INTO YourTableName(YourColumnName) Values(RTRIM(@str))"
        cmd.Parameters.AddWithValue("@str", str)
        cmd.ExecuteNonQuery()
        con.Close()


I make use of IIF Statement to determine if the checkboxes are in CHECKED state. I used RTRIM in the query to remove unnecessary white spaces. And I didn't use any loop to save the string in the database because I already merged the selected strings in the IIF code before proceeding in the saving of data.
 
Share this answer
 
v4
Comments
Member 14127874 27-Jan-19 22:32pm    
I want a loop and array solution. Because on original data, I have an eleven checkboxes.
The Cool Cat 27-Jan-19 22:36pm    
Try using this code for Looping :
https://www.codeproject.com/Questions/1275368/How-to-save-the-checkboxes-that-is-beeing-checked

In the Solution 1

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