Solved

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

Posted on 2014-11-18
1
697 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
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…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

705 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now