Solved

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

Posted on 2014-11-18
1
754 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
[X]
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
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

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

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…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

751 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