Saturday, March 5, 2016

Asp.net: Get comma separated values from database and bind to checkboxlist

In this article I am going to explain how to get comma separated values (data) from database and bind to checkboxlist in ASP.Net using C# and VB.net

Implementation:
I have created a table Tb_Technology:
Id
int
Technology
varchar(50)

Insert the dummy data into table:

Insert into Tb_Technology(Technology) values('Asp.net,Php,Java')

Add a webform to project. Drag and drop the require control (checkboxlist) from toolbox to webform.


HTML Markup:
<fieldset style="margin-left: 40px;width:30%">
    <legend><strong>Bind comma separated values to CheckBoxList</strong></legend>
    <table>
    <tr><td>Select Technology :</td><td>
          <asp:CheckBoxList ID="CheckBoxList1" runat="server">
        </asp:CheckBoxList></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())



Bind the Checkboxlist
Create a method to bind the comma separated text to checkboxlist and call it on page load.
C# code:
protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindCheckBox();
        }
    }
    public void BindCheckBox()
    {
        try
        {
            SqlDataAdapter adp = new SqlDataAdapter("Select * from Tb_Technology", con);
            DataTable dt = new DataTable();
            adp.Fill(dt);
            if (dt.Rows.Count > 0)
            {
                string Chkvalues = dt.Rows[0]["Technology"].ToString();
                foreach (string tech in Chkvalues.Split(','))
                {
                    ListItem items = new ListItem();
                    items.Text = tech;
                    items.Value = tech;
                    CheckBoxList1.Items.Add(items);
                }
            }
        }
        catch (Exception ex)
        { }      
    }

VB.net code
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            BindCheckBox()
        End If
    End Sub
    Public Sub BindCheckBox()
        Try
            Dim adp As New SqlDataAdapter("Select * from Tb_Technology", con)
            Dim dt As New DataTable()
            adp.Fill(dt)
            If dt.Rows.Count > 0 Then
                Dim Chkvalues As String = dt.Rows(0)("Technology").ToString()
                For Each tech As String In Chkvalues.Split(","c)
                    Dim items As New ListItem()
                    items.Text = tech
                    items.Value = tech
                    CheckBoxList1.Items.Add(items)
                Next
            End If
        Catch ex As Exception
        End Try
    End Sub 

1 comment: