Insert multiple selected values of checkboxlist to database as comma separated in ASP.Net - ASPMANTRA | Asp.Net,MVC,AngularJs,Jquery,Javascript,Sql Server AND WCF snippets and tutorial

Insert multiple selected values of checkboxlist to database as comma separated in ASP.Net

In this article I am going to explain how to Insert multiple selected values of checkboxlist in database as comma separated in ASP.Net using C# and

HTML Markup:
<fieldset style="margin-left: 40px;width:30%">
    <legend>Insert multiple selected values of checkboxlist </legend>
    <tr><td>Select Technology :</td><td> <asp:CheckBoxList ID="chkboxtech" runat="server" RepeatDirection="Horizontal" RepeatLayout="Flow">
    </asp:CheckBoxList>  </td></tr>
    <tr><td></td><td> <asp:Button ID="btnsubmit" runat="server" Text="Submit" /></td></tr>

Add the namespace
C# code:
using System.Data;
using System.Data.SqlClient;
using System.Configuration; code:
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration

Create sqlconnection
C# code:
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ToString()); code:
Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("connection").ToString())

On button click write the given code
C# code:
protected void btnsubmit_Click(object sender, EventArgs e)
            SqlCommand cmd = new SqlCommand("Insert into Tb_Technology(Technology) values(@technology)", con);

            String str = "";
            for (int i = 0; i <= chkboxtech.Items.Count - 1; i++)
                if (chkboxtech.Items[i].Selected)
                    if (str == "")
                        str = chkboxtech.Items[i].Text;
                        str += "," + chkboxtech.Items[i].Text;
            cmd.Parameters.AddWithValue("@technology", str);
            Response.Write("<script>alert('Record Insert SUccessfully');</script>");
            chkboxtech.SelectedIndex = -1;
        catch (Exception ex)
        { }
    } code:
    Protected Sub btnsubmit_Click(sender As Object, e As System.EventArgs) Handles btnsubmit.Click
            Dim cmd As New SqlCommand("Insert into Tb_Technology(Technology) values(@technology)", con)
            Dim str As [String] = ""
            For i As Integer = 0 To chkboxtech.Items.Count - 1
                If chkboxtech.Items(i).Selected Then
                    If str = "" Then
                        str = chkboxtech.Items(i).Text
                        str += "," + chkboxtech.Items(i).Text
                    End If
                End If
            cmd.Parameters.AddWithValue("@technology", str)
            Response.Write("<script>alert('Record Insert SUccessfully');</script>")
            chkboxtech.SelectedIndex = -1
        Catch ex As Exception
        End Try
    End Sub 

What do you think about this article?

If you found this article useful, please share and follow on Facebook, Twitter, Google Plus and other social media websites. To get free updates subscribe to newsletter. Please put your thoughts and feedback in comments section.