connectseekers.com

Tuesday, 7 October 2014

Bind Asp.net DropdownList Control by XML File Data Using DataSet in C#.Net

10/07/2014 - By Pranav Singh 0

This article will show you how you can bind an asp.net dropdownlist by xml file using c#.net. In this we will read the xml file into dataset and then bind this dataset to the dropdownlist.


So for this article first we will create a new asp.net application, and we will create an xml file and add the below xml code as shown below format.

xml version="1.0" encoding="utf-8" ?>
<CountryModel>
  <Country>
    <id>1</id>
    <name>India</name>
  </Country>
  <Country>
    <id>2</id>
    <name>Pakistan</name>
  </Country>
  <Country>
    <id>3</id>
    <name>Sri Lanka</name>
  </Country>
  <Country>
    <id>4</id>
    <name>Nepal</name>
  </Country>
  <Country>
    <id>5</id>
    <name>Bhutan</name>
  </Country>
</CountryModel>

After this activity add the below code into your .aspx page.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Bind Asp.net DropdownList Control by XML File Data Using DataSet in C#.Net</title>
    <link href="Styles/Site.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table width="100%" class="content">
            <tr>
                <td>
                    Country List
                    <br />
                    <asp:DropDownList ID="DropDownList1" runat="server" Width="200px">
                    </asp:DropDownList>
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>

In above code I have added a dropdown list. In which I will bind the xml data. now just check the .cs code to bind the record to dropdown list.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetData();
            }
        }
        public void GetData()
        {
            try
            {
                DataSet dsResult = new DataSet();
                dsResult.ReadXml(Server.MapPath("~/XML/XMLFile1.xml"));
                if (dsResult.Tables.Count != 0)
                {
                    if (dsResult.Tables[0].Rows.Count > 0)
                    {
                        DropDownList1.DataSource = dsResult.Tables[0];
                        DropDownList1.DataTextField = "name";
                        DropDownList1.DataValueField = "id";
                        DropDownList1.DataBind();
                    }
                }
            }
            catch (Exception ex)
            {

            }
        }
    }
}

In above code I have used dataset ReadXml function to read the xml file. After getting record in dataset I have passed as data source to the dropdown list.

Now we have done. Run the application to check the output.



About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com
Email Id : contact@aspdotnet-pools.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Top jQuery Plugins

Global Classified : Connectseekers.com
© 2014
back to top