Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

How to populate textbox with the xml attribute value based on the combobox selection using c#.net

Posted on 2014-11-18
1
732 Views
Last Modified: 2014-11-23
Hi Friends,

 I have a XML file with the  fields country name, pin, state, currency. The Sample XML is shown below

<?xml version="1.0" encoding="utf-8" ?>
  <Countries>
  <Country Name="India" State = "TN" Currency ="RS" PIN = "123456" />
  <Country Name = "USA" State = "TX" Currency ="$" PIN= "123456" />
  </Countries>

Now how to bind  all the names of the countries in the combo box, assume that I am choosing India in the combo box, and I want populate all the attributes from the xml in to the text box,for example the state's value should be displayed in the state text box and the Currency value should be displayed in the Currency text box and so on.

Could someone please help me out here ?

Thanks you in advance...
0
Comment
Question by:mannevenu26
1 Comment
 
LVL 16

Accepted Solution

by:
Vikram Singh Saini earned 500 total points
ID: 40451717
Now how to bind  all the names of the countries in the combo box

In code behind file -

protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack) return;

        ddlCountries.DataSource = GetDataFromXml();
        ddlCountries.DataTextField = "Name";
        ddlCountries.DataValueField = "Name";
        ddlCountries.DataBind();

        // Default first item
        ddlCountries.Items.Insert(0, new ListItem("-- Select Country --", "0"));
    }

 /// <summary>
    /// Read data from xml and return back dataTable.
    /// </summary>
    private DataTable GetDataFromXml()
    {
        DataTable table;

        // Countries.xml is the xml file in App_Data folder with same content as you had shared
        var filePath = Server.MapPath("~/App_Data/Countries.xml");
        using (var dsCountries = new DataSet())
        {
            dsCountries.ReadXml(filePath);
            table = dsCountries.Tables[0];
        }

        return table;
    }

Open in new window


for example the state's value should be displayed in the state text box and the Currency value should be displayed in the Currency text box and so on.

protected void ddlCountries_SelectedIndexChanged(object sender, EventArgs e)
    {
        var table = GetDataFromXml();

        // Filter data as per selection in dropdownlist
        var filter = string.Format("Name='{0}'", ddlCountries.SelectedValue);
        var record = table.Select(filter);

        // Bind retrieved records respectively
        txtState.Text = Convert.ToString(record[0]["State"]);
        txtCurrency.Text = Convert.ToString(record[0]["Currency"]);
        txtPin.Text = Convert.ToString(record[0]["PIN"]);
    }

Open in new window

0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question