I have two list boxes. one of theme is populated from an XML document. The other is to become populated from the same XML document (Customers.xml) based on what you choose in the first list box.
I receive an "Object reference not set to an instance of an object error". The error happens in the additem function.
This is probably easy for all of you, so I'm sorry if it sounds like a dumb question. I thought about putting it in the newbie questions, but decided to put it in code.Thanks for any and all help!
This is customers.xml(taken from somewhere online)
<?xml version="1.0" encoding="UTF-8"?>
<Customers>
<Customer>
<CustomerID>ALFKI</CustomerID>
<CompanyName>Alfreds Futterkiste</CompanyName>
<ContactName>Maria Anderss</ContactName>
<ContactTitle>Sales Representative</ContactTitle>
<Address>Obere Str. 57</Address>
<City>Berlin</City>
<PostalCode>12209</PostalCode>
<Country>Germany</Country>
<Phone>030-0074321</Phone>
<Fax>030-0076545</Fax>
</Customer>
<Customer>
<CustomerID>ANATR</CustomerID>
<CompanyName>Ana Trujillo Emparedados y helados</CompanyName>
<ContactName>Ana Trujillo</ContactName>
<ContactTitle>Owner</ContactTitle>
<Address>Avda. de la Constituc</Address>
<City>Mxico D.F.</City>
<PostalCode>05021</PostalCode>
<Country>Mexico</Country>
<Phone>(5) 555-4729</Phone>
<Fax>(5) 555-3745</Fax>
</Customer>
</Customers>
This is a combination of code from different sites online(also just a sample from somewhere online..i'm trying to learn something here!):
private void AddItem(string itemId, string itemName)
{
XPathNavigator DOM = this.DataSources["Customers"].CreateNavigator();
XPathNavigator group1 = DOM.SelectSingleNode("//Customers", NamespaceManager);
XPathNavigator field1 = DOM.SelectSingleNode("/my:myFields/my:CustomerName2", NamespaceManager);
XPathNavigator newNode = field1.Clone();
newNode.SelectSingleNode("value").SetValue(itemId);
//newNode.SelectSingleNode("displayname").SetValue(itemName);
//group1.AppendChild(newNode);
}
public void SelectCustomer_Changed(object sender, XmlEventArgs e)
{
try
{
// Get a reference to the Customers secondary data source,
// and create an XPathNavigator object for it.
XPathNavigator Customers =this.DataSources["Customers"].CreateNavigator();
// Create an XPathNavigator object positioned on the current
// "site", which is the SelectCustomer node in the
// main data source.
XPathNavigator myForm = e.Site.CreateNavigator();
// Move to the parent of the SelectCustomer node,
// so that the code can access the CustomerName node.
myForm.MoveToParent();
// Check if the user selected the blank value from the
// Drop-Down List Box after previously selecting a CustomerID.
if (e.Site.InnerXml == "")
{
// Clear previously selected value from CustomerName field.
myForm.SelectSingleNode("my:CustomerName",this.NamespaceManager).DeleteSelf();
}
else
{
// Get the Company name from the Customers data source
// using the value in the CustomerID node.
string custName = Customers.SelectSingleNode("/Customers/Customer[CustomerID = '" + e.Site.InnerXml +"']/CompanyName", this.NamespaceManager).InnerXml;
// Select the CustomerName node from the current row and
// set its value.
// myForm.SelectSingleNode("my:CustomerName",this.NamespaceManager).SetValue(custName);
AddItem(custName, custName);
}
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex.Message);
}