Related Posts Plugin for WordPress, Blogger...

About

Follow Us

Thursday, 12 February 2015

Introduction: 

 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>BSc</QualificationName>
  </Qualification>
<Qualification>
    <QualificationID>2</QualificationID>
    <QualificationName>B.Tech</QualificationName>
  </Qualification>
<Qualification>
    <QualificationID>3</QualificationID>
    <QualificationName>B.Sc Biotech</QualificationName>
  </Qualification>
<Qualification>
    <QualificationID>4</QualificationID>
    <QualificationName>BBA</QualificationName>
  </Qualification>
<Qualification>
    <QualificationID>5</QualificationID>
    <QualificationName>B.Sc BioInfo</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("XMLFile.xml"));
            ddlQualification.DataSource = ds;
            ddlQualification.DataTextField = "QualificationName";
            ddlQualification.DataValueField = "QualificationID";
            ddlQualification.DataBind();
            ddlQualification.Items.Insert(0, new ListItem("--Select Course--", "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 Course</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("Xmlfile.xml"))
            ddlQualification.DataSource = ds
            ddlQualification.DataTextField = "QualificationName"
            ddlQualification.DataValueField = "QualificationID"
            ddlQualification.DataBind()
            ddlQualification.Items.Insert(0, New ListItem("--Select Course--", "0"))
        Catch ex As Exception
            Response.Write(ex.Message)
        End Try
    End Sub

    Protected Sub ddlQualification_SelectedIndexChanged(sender As Object, e As EventArgs) HandlesddlQualification.SelectedIndexChanged
        Response.Write("Selected item: " & ddlQualification.SelectedItem.Text & " and Selected value: " & ddlQualification.SelectedValue)

    End Sub
Categories: , ,

0 comments:

Post a Comment