I am filling ddl from the xml file below is my code
public ListItemCollection bind()
{
ListItemCollection ddlitems=new ListItemCollection();
ProfileMasterDAL profileMasterDAL=new ProfileMasterDAL();
XmlNodeList list = profileMasterDAL.getdata().GetElementsByTagName("country");
foreach (XmlNode node in list)
{
ListItem item=new ListItem(node.InnerText,node.Attributes["name"].Value);// Object reference not set to an instance of an object.
ddlitems.Add(item);
}
return ddlitems;
}
but i am getting error like Object reference not set to an instance of an object so how to get rid of this?
protected void Page_Load(object sender, EventArgs e)
{
fillCountryList();
}
private void fillCountryList()
{
//loading your xml from the path
XDocument loaded = XDocument.Load(Server.MapPath(@"~/App_Data/YourXMLFileName.xml"));
//get country names from your xml
var countries = from c in loaded.Descendants("countrys").Descendants("country")
orderby (string)c.Element("name")
select (string)c.Element("name");
//binding dropdownlist
DropDownList1.DataSource = countries;
DropDownList1.DataBind();
}
.net_junkie
Member
297 Points
825 Posts
unable to bind xml data to ddl linq to xml
Jul 23, 2012 06:12 PM|LINK
I am filling ddl from the xml file below is my code
public ListItemCollection bind() { ListItemCollection ddlitems=new ListItemCollection(); ProfileMasterDAL profileMasterDAL=new ProfileMasterDAL(); XmlNodeList list = profileMasterDAL.getdata().GetElementsByTagName("country"); foreach (XmlNode node in list) { ListItem item=new ListItem(node.InnerText,node.Attributes["name"].Value);// Object reference not set to an instance of an object. ddlitems.Add(item); } return ddlitems; }but i am getting error like Object reference not set to an instance of an object so how to get rid of this?
<countrys> <country> <name>India</name> <state> <text>Maharashtra</text> <text>Kashmir</text> <text>Goa</text> </state> </country> <country> <name>Sri lanka</name> <state> <text>Kanady</text> <text>Colombo</text> <text>Galle</text> </state> </country> </countrys>Rohit Rao
Participant
1869 Points
357 Posts
Re: unable to bind xml data to ddl linq to xml
Jul 23, 2012 06:38 PM|LINK
protected void Page_Load(object sender, EventArgs e) { fillCountryList(); } private void fillCountryList() { //loading your xml from the path XDocument loaded = XDocument.Load(Server.MapPath(@"~/App_Data/YourXMLFileName.xml")); //get country names from your xml var countries = from c in loaded.Descendants("countrys").Descendants("country") orderby (string)c.Element("name") select (string)c.Element("name"); //binding dropdownlist DropDownList1.DataSource = countries; DropDownList1.DataBind(); }.net_junkie
Member
297 Points
825 Posts
Re: unable to bind xml data to ddl linq to xml
Jul 23, 2012 07:16 PM|LINK
I am using 3 tier architecture for this and how to write BAL and DAL code?