Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2014-11-18
1
Medium Priority
?
810 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 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";
        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

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

670 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