Friday, 24 May 2013

How to Bind DropDownList from XML file in asp.net



Description:-

In this example I will explain that how to bind dropdown list with xml file. Binding dropdown list with values from database is common requirement ,but sometime we may have a requirement like binding the data from xml file or other files with dropdown list.


You can easily add the value into a DropDownList. But the main problem is that if there are any more items then you have to go to the .aspx file or .cs file and you have to add the new item(s).

So the best way is to add your DropDownList item data into a separate XML file. And bind data item from this XML file to the Dropdownlist .the main bebefit is that If any change is required then you just have to add or modify the new item into the XML file. That's it. All the DropDownList controls that uses that XML file gets the new item data automatically and you do not have change in all .aspx or .cs page but you just have to change in one XMl file or whatever any other file that you use to bind the data in Dropdownlist.


 We all Know Bind Country and state in DropdownList is Very Easy task with two table one is country and second is state and we can give relationship with each other.

in this Example we can Bind Country and State from XML file that is very Difficult task then simply Bind from Sql Database.

To Retrieve Data Frm the XML File First you Have open its in Read Mode and Fetch Data like CountryName and State Name From it and then Bind to the DropDownList.

Here is a Code How to Open or Use XML File in our aspx.cs page

DataSet ds = new DataSet();

        string apppath = Request.PhysicalApplicationPath;

        ds.ReadXml(MapPath("~\\" + "HelperClass\\XML_Files\\CountryOrState.xml"));

        DataTable dt = ds.Tables[0];

        ddlCountry.DataTextField = "Name";

        ddlCountry.DataValueField = "ConId";

        ddlCountry.DataSource = dt;

        ddlCountry.DataBind();

        ddlCountry.Items.Insert(0, "Select Country");

you can also perform Insert,Update,Delete Operation in Xml File or Binding Data to Gridview Easily. To Look this Program Click Here  insert upadate Delete in XML File in ASsp.net

the main Advantage of this Example is that you can Reduce the Load on the database because we can bind data from the XML file so there is no load on Sqlserver.

            But,problem is that when bind country and state from xml file not from sql database.so how to bind country and state from xml file are as shown below.


How Read and Write File in Asp.Net Read/Write File in Asp.Net with C#

Dynamically Create DataTable and Bind to Gridview Dyanmic creation of rows

Code For CountryorState XML File:-


<?xml version="1.0" encoding="utf-8" ?>
<Root>
 <Country Name="Afghanistan"  Currency="Afghani" ConId="1">
  <State Name="Badakhshan" stateId="1" ConId="1"></State>
  <State Name="Badghis" stateId="2" ConId="1"></State>
  <State Name="Baghlan" stateId="3" ConId="1"></State>
  <State Name="Balkh" stateId="4" ConId="1"></State>
  <State Name="Bamian" stateId="5" ConId="1"></State>
  <State Name="Farah" stateId="6" ConId="1"></State>
  <State Name="Faryab" stateId="7" ConId="1"></State>
  <State Name="Ghazni" stateId="8" ConId="1"></State>
  <State Name="Ghowr" stateId="9" ConId="1"></State>
  <State Name="Helmand" stateId="10" ConId="1"></State>
  <State Name="Herat" stateId="11" ConId="1"></State>
  <State Name="Jowzjan" stateId="12" ConId="1"></State>
  <State Name="Kabul" stateId="13" ConId="1"></State>
  <State Name="Kandahar" stateId="14" ConId="1"></State>
  <State Name="Kapisa" stateId="15" ConId="1"></State>
  <State Name="Kapisa" stateId="16" ConId="1"></State>
  <State Name="Konarha" stateId="17" ConId="1"></State>
  <State Name="Kondoz" stateId="18" ConId="1"></State>
  <State Name="Laghman" stateId="19" ConId="1"></State>
  <State Name="Lowgar" stateId="20" ConId="1"></State>
  <State Name="Nangarhar" stateId="21" ConId="1"></State>
  <State Name="Nimruz" stateId="22" ConId="1"></State>
  <State Name="Paktia" stateId="23" ConId="1"></State>
  <State Name="Paktika" stateId="24" ConId="1"></State>
  <State Name="Parwan" stateId="25" ConId="1"></State>
  <State Name="Samangan" stateId="26" ConId="1"></State>
  <State Name="Sar-e Pol" stateId="27" ConId="1"></State>
  <State Name ="Takhar" stateId="28" ConId="1"></State>
  <State Name="Uruzgan" stateId="29" ConId="1"></State>
  <State Name="Wardak" stateId="30" ConId="1"></State>
  <State Name="Zabul" stateId="31" ConId="1"></State>
 </Country>
 <Country Name="Albania" Currency="Lek" ConId="2">
  <State Name="Berat" stateId="1" ConId="2"></State>
  <State Name="Bulqize" stateId="2" ConId="2"></State>
  <State Name="Delvine" stateId="3" ConId="2"></State>
  <State Name="Devoll" stateId="4" ConId="2"></State>
  <State Name="Diber" stateId="5" ConId="2"></State>
  <State Name="Durres" stateId="6" ConId="2"></State>
  <State Name="Elbasan" stateId="7" ConId="2"></State>
  <State Name="Fier" stateId="8" ConId="2"></State>
  <State Name="Gjirokaster" stateId="9" ConId="2"></State>
  <State Name="Gramsh" stateId="10" ConId="2"></State>
  <State Name="Has" stateId="11" ConId="2"></State>
  <State Name="Kavaje" stateId="12" ConId="2"></State>
  <State Name="Kolonje" stateId="13" ConId="2"></State>
  <State Name="Korce" stateId="14" ConId="2"></State>
  <State Name="Kruje" stateId="15" ConId="2"></State>
  <State Name="Kucove" stateId="16" ConId="2"></State>
  <State Name="Kukes" stateId="17" ConId="2"></State>
  <State Name="Lac" stateId="18" ConId="2"></State>
  <State Name="Lezhe" stateId="19" ConId="2"></State>
  <State Name="Librazhd" stateId="20" ConId="2"></State>
  <State Name="Lushnje" stateId="21" ConId="2"></State>
  <State Name="Malesia e Madhe" stateId="22" ConId="2"></State>
  <State Name="Mallakaster" stateId="23" ConId="2"></State>
  <State Name="Mat" stateId="24" ConId="2"></State>
  <State Name="Mirdite" stateId="25" ConId="2"></State>
  <State Name="Peqin" stateId="26" ConId="2"></State>
  <State Name="Permet" stateId="27" ConId="2"></State>
  <State Name="Pogradec" stateId="28" ConId="2"></State>
  <State Name="Puke" stateId="29" ConId="2"></State>
  <State Name="Sarande" stateId="30" ConId="2"></State>
  <State Name="Shkoder" stateId="31" ConId="2"></State>
  <State Name="Skrapar" stateId="32" ConId="2"></State>
  <State Name ="Tepelene" stateId="33" ConId="2"></State>
  <State Name="Tirane" stateId="34" ConId="2"></State>
  <State Name="Tropoje" stateId="35" ConId="2"></State>
  <State Name="Vlore" stateId="36" ConId="2"></State>
 </Country>
 <Country Name="American Samoa" Currency="Dollar" ConId="3" >
  <State Name="Alabama" stateId="1" ConId="3"></State>
  <State Name="Alaska" stateId="2" ConId="3"></State>
  <State Name="Arizona" stateId="3" ConId="3"></State>
  <State Name="Arkansas" stateId="4" ConId="3"></State>
  <State Name="California" stateId="5" ConId="3"></State>
  <State Name="Colorado" stateId="6" ConId="3"></State>
  <State Name="Connecticut" stateId="7" ConId="3"></State>
  <State Name="Delaware" stateId="8" ConId="3"></State>
  <State Name="Florida" stateId="9" ConId="3"></State>
  <State Name=" Georgia" stateId="10" ConId="3"></State>
  <State Name="Hawaii" stateId="11" ConId="3"></State>
  <State Name="Idaho" stateId="12" ConId="3"></State>
  <State Name="Illinois" stateId="13" ConId="3"></State>
  <State Name="Indiana" stateId="14" ConId="3"></State>
  <State Name="Iowa" stateId="15" ConId="3"></State>
  <State Name="Kansas" stateId="16" ConId="3"></State>
  <State Name="Kentucky" stateId="17" ConId="3"></State>
  <State Name="Louisiana" stateId="18" ConId="3"></State>
 </Country>
 <Country Name="Angola" Currency="Kwanza" ConId="4" >
  <State Name=" Luanda" stateId="1" ConId="4"></State>
  <State Name="Bengo" stateId="2" ConId="4"></State>
  <State Name="Benguela" stateId="3" ConId="4"></State>
  <State Name="Bie" stateId="4" ConId="4"></State>
  <State Name="Cabinda" stateId="5" ConId="4"></State>
  <State Name="Cuando Cubango" stateId="6" ConId="4"></State>
  <State Name="Cuanza Norte" stateId="7" ConId="4"></State>
  <State Name="Cuanza Sul"  stateId="8" ConId="4"></State>
  <State Name="Cunene" stateId="9" ConId="4"></State>
 </Country>
 <Country Name="Antarctica" Currency="Krone" ConId="5" >
  <State Name="Alabama" stateId="1" ConId="5"></State>
  <State Name="Alaska" stateId="2" ConId="5"></State>
  <State Name="Arizona" stateId="3" ConId="5"></State>
  <State Name="Arkansas" stateId="4" ConId="5"></State>
  <State Name="California" stateId="5" ConId="5"></State>
  <State Name="Colorado" stateId="6" ConId="5"></State>
  <State Name="Connecticut" stateId="7" ConId="5"></State>
  <State Name="Delaware" stateId="8" ConId="5"></State>
  <State Name="Florida" stateId="9" ConId="5"></State>
  <State Name=" Georgia" stateId="10" ConId="5"></State>
  <State Name="Hawaii" stateId="11" ConId="5"></State>
  <State Name="Idaho" stateId="12" ConId="5"></State>
  <State Name="Illinois" stateId="13" ConId="5"></State>
  <State Name="Indiana" stateId="14" ConId="5"></State>
  <State Name="Iowa" stateId="15" ConId="5"></State>
  <State Name="Kansas" stateId="16" ConId="5"></State>
  <State Name="Kentucky" stateId="17" ConId="5"></State>
  <State Name="Louisiana" stateId="18" ConId="5"></State>
 </Country>
 <Country Name="Argentina" Currency="Nuevo Peso" ConId="6" >
  <State Name="Buenos Aires" stateId="1" ConId="6"></State>
  <State Name="Catamarca" stateId="2" ConId="6"></State>
  <State Name="Chaco" stateId="3" ConId="6"></State>
  <State Name="Chubut" stateId="4" ConId="6">></State>
  <State Name="Cordoba" stateId="5" ConId="6"></State>
  <State Name ="Corrientes" stateId="6" ConId="6"></State>
  <State Name ="Entre Rios" stateId="7" ConId="6"></State>
  <State Name ="Formosa" stateId="8" ConId="6"></State>
  <State Name="Jujuy" stateId="9" ConId="6"></State>
  <State Name="La Pampa" stateId="10" ConId="6"></State>
  <State Name="La Rioja" stateId="11" ConId="6"></State>
  <State Name="Mendoza" stateId="12" ConId="6"></State>
 </Country>
 <Country Name="Armenia" Currency="Dram" ConId="7" >
  <State Name="Yerevan" stateId="1" ConId="7"></State>
  <State Name="Aragatsotn" stateId="2" ConId="7"></State>
  <State Name="Ararat" stateId="3" ConId="7"></State>
  <State Name="Armavir" stateId="4" ConId="7"></State>
  <State Name="Geghark" stateId="5" ConId="7"></State>
  <State Name="Kotayk" stateId="6" ConId="7"></State>
  <State Name="Lorri" stateId="7" ConId="7"></State>
 </Country>
 <Country Name="Australia" Currency="Dollar" ConId="8" >
  <State Name="Canberra" stateId="1" ConId="8"></State>
  <State Name="Australian Capital Territory" stateId="2" ConId="8"></State>
  <State Name="New South Wales" stateId="3" ConId="8"></State>
  <State Name="Northern Territory" stateId="4" ConId="8"></State>
  <State Name="Queensland" stateId="5" ConId="8"></State>
  <State Name="South Australia" stateId="6" ConId="8"></State>
  <State Name="Tasmania" stateId="7" ConId="8"></State>
  <State Name="Victoria" stateId="8" ConId="8"></State>
  <State Name="Western Australia" stateId="9" ConId="8"></State>
 </Country>
 <Country Name="Antarctica"  Currency="krone" ConId="9">
  <State Name="Amundsen-Scott" stateId="1" ConId="9"></State>
 </Country>
 <Country Name="Austria" Currency="Euro" ConId="10" >
  <State Name="Vienna" stateId="1" ConId="10"></State>
  <State Name="Burgenland" stateId="2" ConId="10"></State>
  <State Name="Kaernten" stateId="3" ConId="10"></State>
  <State Name="Niederoesterreich" stateId="4" ConId="10"></State>
  <State Name="Oberoesterreich" stateId="5" ConId="10"></State>
  <State Name="Salzburg" stateId="6" ConId="10"></State>
 </Country>
 <Country Name="Azerbaijan" Currency="Manat" ConId="11" >
  <State Name="Baku" stateId="1" ConId="11"></State>
  <State Name="Abseron" stateId="2" ConId="11"></State>
  <State Name="Agcabadi" stateId="3" ConId="11"></State>
  <State Name="Agdam" stateId="4" ConId="11"></State>
  <State Name="Agdas" stateId="5" ConId="11"></State>
  <State Name="Agstafa" stateId="6" ConId="11"></State>
  <State Name="Agsu" stateId="7" ConId="11"></State>
  <State Name="Ali Bayramli" stateId="8" ConId="11"></State>
  <State Name="Astara" stateId="9" ConId="11"></State>
 </Country>

</Root>

Code For .aspx.cs File:-



if (!IsPostBack)

        {
            BindCountry(drpdwn_country);
    }



 //BindCountry
        private void BindCountry(DropDownList ddlCountry)
        {
            try
            {
                DataSet ds = new DataSet();
                string apppath = Request.PhysicalApplicationPath;
                ds.ReadXml(MapPath("~\\" + "HelperClass\\XML_Files\\CountryOrState.xml"));
                DataTable dt = ds.Tables[0];
                ddlCountry.DataTextField = "Name";
                ddlCountry.DataValueField = "ConId";
                ddlCountry.DataSource = dt;
                ddlCountry.DataBind();
                ddlCountry.Items.Insert(0, "Select Country");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }




    // Bind State According to Selected Country
        protected void drpdwn_country_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindState(drpdwn_state, drpdwn_country.SelectedItem.Text);
        }




    //BindState
        private void BindState(DropDownList ddlState, string CountryName)
        {
            try
            {
                if (drpdwn_country.SelectedIndex != 0)
                {
                    DataSet ds1 = new DataSet();
                    ds1.ReadXml(MapPath("~\\" + "HelperClass\\XML_Files\\CountryOrState.xml"));
                    DataTable dt1 = new DataTable();
                    dt1 = ds1.Tables[1];
                    var query = (from myRow in dt1.AsEnumerable()
                                 where myRow.Field<string>("ConId") == drpdwn_country.SelectedValue.ToString()
                                 select new
                                 {
                                     Name = myRow.Field<string>("Name"),
                                     stateid = myRow.Field<string>("stateId")
                                 }).ToList();
                    ddlState.DataSource = query.ToList();
                    ddlState.DataValueField = "stateid";
                    ddlState.DataTextField = "Name";
                    ddlState.DataBind();
                    ddlState.Items.Insert(0, "Select State");

                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }



0 comments:

Post a Comment