Thursday, March 3, 2016

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 VB.net


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

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

VB.net code:
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration

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

VB.net 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)
    {
        try
        {
            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;
                    }
                    else
                    {
                        str += "," + chkboxtech.Items[i].Text;
                    }
                }
            }
            cmd.Parameters.AddWithValue("@technology", str);
            con.Open();
            cmd.ExecuteNonQuery();
            Response.Write("<script>alert('Record Insert SUccessfully');</script>");
            chkboxtech.SelectedIndex = -1;
        }
        catch (Exception ex)
        { }
    }

VB.net code:
    Protected Sub btnsubmit_Click(sender As Object, e As System.EventArgs) Handles btnsubmit.Click
        Try
            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
                    Else
                        str += "," + chkboxtech.Items(i).Text
                    End If
                End If
            Next
            cmd.Parameters.AddWithValue("@technology", str)
            con.Open()
            cmd.ExecuteNonQuery()
            Response.Write("<script>alert('Record Insert SUccessfully');</script>")
            chkboxtech.SelectedIndex = -1
        Catch ex As Exception
        End Try
    End Sub 

No comments:

Post a Comment