Please disable your adblock and script blockers to view this page
 

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

IntroductionIn previous 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 CheckBoxList using DataSet in asp.net  and Bind RadioButtonList from Sql server table in asp.net
In this article I will explain how to create XML file and Bind XML data to RadioButtonList using DataSet in asp.net.

Create XML file and Bind RadioButtonList with XML data in asp.net
Implementation: Let's create an example to see the concept in actin. 
  • 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 RadioButtonList

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

<fieldset style="width:200px;">
            <legend>Select Qualification</legend>
          <asp:RadioButtonList ID="rblQualifications" runat="server" RepeatColumns="3" RepeatDirection="Horizontal"></asp:RadioButtonList>
             </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)
        {
            BindRadioButtonList();
        }
    }
    private void BindRadioButtonList()
    {
        DataSet ds = new DataSet();
        try
        {
            ds.ReadXml(Server.MapPath("Qualifications.xml"));
            rblQualifications.DataSource = ds;
            rblQualifications.DataTextField = "QualificationName";
            rblQualifications.DataValueField = "QualificationID";
            rblQualifications.DataBind();
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
    }

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

  • In the code behind file (.aspx.vb) write the code:
First include following 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
            BindRadioButtonList()
        End If
    End Sub
    Private Sub BindRadioButtonList()
        Dim ds As New DataSet()
        Try
            ds.ReadXml(Server.MapPath("Qualifications.xml"))
            rblQualifications.DataSource = ds
            rblQualifications.DataTextField = "QualificationName"
            rblQualifications.DataValueField = "QualificationID"
            rblQualifications.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.. ConversionConversion EmoticonEmoticon