i am new to ASP.NET,
i am making Country, state dropdownlist.
for eg: For particular country, i will read states of that country from XML file.
i am unable to fetch states of required country in my Dropdownlist...
here is my code snippet in XMLFile.xml
<?xml version="1.0" encoding="utf-8" ?>
<countrys>
<country name="India">
<state value1="Maharashtra"></state>
<state value2="Kashmir"></state>
<state value3="Goa"></state>
</country>
<country name="Sri Lanka">
<state value1="Kanady"></state>
<state value2="Colombo"></state>
<state value3="Galle"></state>
</country>
<country name="Australia">
<state valu1e="Sydney"></state>
<state value2="Perth"></state>
<state value3="Melbourne"></state>
</country>
<country name="South Africa">
<state value1="Capetown"></state>
<state value2="Johanusburg"></state>
<state value3="Durban"></state>
</country>
</countrys>
and code in Country.aspx.cs
public partial class Country : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
LoadDropdown();
}
}
protected void LoadDropdown()
{
DataSet ds = new DataSet();
ds.ReadXml (Server.MapPath("XMLFile.xml"));
DropDownListCountry.DataTextField = "country_text";
DropDownListCountry.DataSource = ds;
DropDownListCountry.DataBind();
DropDownListCountry.Items.Insert(0,new ListItem(" Select ","0"));
}
}
protected void DropDownListCountry_SelectedIndexChanged(object sender, EventArgs e)
{
string st = (DropDownListCountry.SelectedIndex).ToString();
XDocument main = XDocument.Load(@"XMLFile.xml");
var query = from state in doc.Descendants("countrys").Elements("country")
where st == state.Value
select state.NextNode;
DropDownListState.DataSource = query;
DropDownListState.DataBind();
}
}
ERROR : Object reference not set to an instance of an object.
Thanks In Advance !!
querygives me null value.. can you plz check that..."country_text"b) Your states are not within the country element.. c)"state"has no value but"text"inside state has value..Please modify your xml properly before working with it<country name="country1"><state name="state1"/><state name="state2"/></country>suits better