How to create XML file and Bind XML data to CheckBoxList using DataSet in asp.net

IntroductionIn previous related articles i explained How to Create XML file and Bind XML data to DropDownList using DataSet in asp.net and How to create XML file and Bind XML data to RadioButtonList using DataSet in asp.net and How to validate CheckBoxList using JavaScript and Example to validate asp.net CheckBoxList using jQuery and Get CheckBoxList selected items in comma separated format 
In this article I will explain how to create XML file and Bind XML data to CheckBoxList using DataSet in asp.net.

Create XML file and Bind CheckBoxList with XML data in asp.net

Implementation
: Let's create an example to see it in action.
  •  First you need to create the XML file. To create open website menu-> add new item-> select XML file and name it Qualifications.xml and add the following tags in side it:
 <Qualifiations>
  <Qualification>
    <QualificationID>1</QualificationID>
    <QualificationName>MCA</QualificationName>
  </Qualification>
<Qualification>
    <QualificationID>2</QualificationID>
    <QualificationName>B.Tech</QualificationName>
  </Qualification>
<Qualification>
    <QualificationID>3</QualificationID>
    <QualificationName>M.Sc</QualificationName>
  </Qualification>
<Qualification>
    <QualificationID>4</QualificationID>
    <QualificationName>MBA</QualificationName>
  </Qualification>
<Qualification>
    <QualificationID>5</QualificationID>
    <QualificationName>M.Sc</QualificationName>
  </Qualification>
<Qualification>
    <QualificationID>6</QualificationID>
    <QualificationName>BCA</QualificationName>
  </Qualification>
</Qualifiations>

Save the file in the root folder.

C#.NET Code to create XML file and Bind XML data to CheckBoxList

  • In the design page (.aspx) place a CheckBoxList control:

<fieldset style="width:190px;">
            <legend>Select Qualification</legend>
        <asp:CheckBoxList ID="cblQualifications" runat="server" RepeatColumns="3" RepeatDirection="Horizontal"></asp:CheckBoxList>
             </fieldset>

  • In the code behind file (.aspx.cs) write the code:
First include following namespaces:

using System.Data;
using System.Data.SqlClient;
using System.Configuration;

then write code:


   protected void Page_Load(object sender, EventArgs e)

    {
        if (!Page.IsPostBack)
        {
            BindCheckBoxList();
        }
    }
    private void BindCheckBoxList()
    {
        DataSet ds = new DataSet();
        try
        {
            ds.ReadXml(Server.MapPath("Qualifications.xml"));
            cblQualifications.DataSource = ds;
            cblQualifications.DataTextField = "QualificationName";
            cblQualifications.DataValueField = "QualificationID";
            cblQualifications.DataBind();          
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
    }


VB.NET Code to create XML file and Bind XML data to CheckBoxList

  • In the code behind file (.aspx.vb) write the code:
First import the following required namespaces:

Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration

Then write the code:


Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            BindCheckBoxList()
        End If
    End Sub
    Private Sub BindCheckBoxList()
        Dim ds As New DataSet()
        Try
            ds.ReadXml(Server.MapPath("Qualifications.xml"))
            cblQualifications.DataSource = ds
            cblQualifications.DataTextField = "QualificationName"
            cblQualifications.DataValueField = "QualificationID"
            cblQualifications.DataBind()
        Catch ex As Exception
            Response.Write(ex.Message)
        End Try
    End Sub

Now over to you:
"If you like my work; you can appreciate by leaving your comments, hitting Facebook like button, following on Google+, Twitter, Linked in and Pinterest, stumbling my posts on stumble upon and subscribing for receiving free updates directly to your inbox . Stay tuned and stay connected for more technical updates."
Previous
Next Post »

If you have any question about any post, Feel free to ask.You can simply drop a comment below post or contact via Contact Us form. Your feedback and suggestions will be highly appreciated. Also try to leave comments from your account not from the anonymous account so that i can respond to you easily..