Go Premium for a chance to win a PS4. Enter to Win


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

Posted on 2014-11-18
Medium Priority
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" ?>
  <Country Name="India" State = "TN" Currency ="RS" PIN = "123456" />
  <Country Name = "USA" State = "TX" Currency ="$" PIN= "123456" />

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...
Question by:mannevenu26
1 Comment
LVL 16

Accepted Solution

Vikram Singh Saini earned 2000 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";

        // 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())
            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


Featured Post

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Suggested Courses

971 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