Create XML file and Bind XML data to DropDownList using DataSet in asp.net

Create XML file and Bind DropDownList with Xml data in asp.netIntroduction: In previous articles i explained How to create XML file and Bind XML data to CheckBoxList using DataSet in asp.net  and How to create XML file and Bind XML data to RadioButtonList using DataSet in asp.net. In this article I will explain how to create XML file and Bind XML data to DropDownList using DataSet in asp.net.
  • First you need to create the XML file. So to create start visual studio-> File menu-> New -> Webiste -> Name the website and Click Ok.
  • 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 DropDownList using DataSet in asp.net

  • In the design page (.aspx) place a DropDownList control :
<fieldset style="width:160px;height:160px;">
            <legend>Select Qualification</legend>
    <asp:DropDownList ID="ddlQualification" AutoPostBack="true" runat="server" OnSelectedIndexChanged="ddlQualification_SelectedIndexChanged">
                           </asp:DropDownList>
             </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)
        {
            BindDropDownList();
        }
    }

    private void BindDropDownList()
    {
        DataSet ds = new DataSet();
        try
        {
            ds.ReadXml(Server.MapPath("Qualifications.xml"));
        ddlQualification.DataSource = ds;
        ddlQualification.DataTextField = "QualificationName";
        ddlQualification.DataValueField = "QualificationID";
        ddlQualification.DataBind();
        ddlQualification.Items.Insert(0,new ListItem("--Select Qualification--","0"));
        }
        catch(Exception ex)
        {
            Response.Write(ex.Message);
        }   
    }
    protected void ddlQualification_SelectedIndexChanged(object sender, EventArgs e)
    {
        Response.Write("Selected item: " + ddlQualification.SelectedItem.Text + " and Selected value: " + ddlQualification.SelectedValue);
    }

VB.Net Code to Create XML file and Bind XML data to DropDownList using DataSet in asp.net

  • In the design page (.aspx) place a DropDownList control:
<fieldset style="width:160px;height:160px;">
            <legend>Select Qualification</legend>
    <asp:DropDownList ID="ddlQualification" AutoPostBack="true" runat="server">
                           </asp:DropDownList>
             </fieldset>
  • 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
            BindDropDownList()
        End If
    End Sub
    Private Sub BindDropDownList()
        Dim ds As New DataSet()
        Try
            ds.ReadXml(Server.MapPath("Qualifications.xml"))
            ddlQualification.DataSource = ds
            ddlQualification.DataTextField = "QualificationName"
            ddlQualification.DataValueField = "QualificationID"
            ddlQualification.DataBind()
            ddlQualification.Items.Insert(0, New ListItem("--Select Qualification--", "0"))
        Catch ex As Exception
            Response.Write(ex.Message)
        End Try
    End Sub

    Protected Sub ddlQualification_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ddlQualification.SelectedIndexChanged
        Response.Write("Selected item: " & ddlQualification.SelectedItem.Text & " and Selected value: " & ddlQualification.SelectedValue)
    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..