G
Guest
am trying to have XML data show results in a combobox (Which works) and
when the combobox has a selection, change the text in a label field.
I am following the video from
http://msdn.microsoft.com/vbasic/atthemovies/dataandxml/default.aspx (reading
XML data) almost step by step (changing to match my xmlfiles, etc)
Here is an example of my XML
<products>
<product>CP-90, T-32</product>
<company>Carwell</company>
<address>Street Address</address>
<city>City</city>
<state>State</state>
<zip>Zip</zip>
</products>
My load function (which works)
Dim xmlFile As String = "..\Products.xml"
Dim xmlDoc As XmlDocument
Private Sub Zadig_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim xmlTr As New XmlTextReader(xmlFile)
While xmlTr.Read
If xmlTr.Name = "product" AndAlso xmlTr.NodeType =
XmlNodeType.Element Then
cmbProducts.Items.Add(xmlTr.ReadString)
End If
End While
xmlTr.Close()
xmlDoc = New XmlDocument
xmlDoc.Load(xmlFile)
End Sub
My selection method
Private Sub cmbProducts_SelectionChangeCommitted(ByVal sender As Object,
ByVal e As System.EventArgs) Handles cmbProducts.SelectionChangeCommitted
DisplayProduct(cmbProducts.SelectedIndex + 1)
End Sub
Here is where the problem begins
Sub DisplayProduct(ByVal position As Integer)
Dim node As XmlNode = xmlDoc.SelectSingleNode( _
"/products/product[" & position & "]")
lblCompany.Text = node.SelectSingleNode("company").InnerText
End Sub
Everything compiles fine but when I run and then select a product I get.
An unhandled exception of type 'System.NullReferenceException' occurred in
Zadig.exe
Additional information: Object reference not set to an instance of an object.
The error says it is on line "lblCompany.Text =
node.SelectSingleNode("company").InnerText" but I have no idea what the real
problem is.
Please advise.
Michael
when the combobox has a selection, change the text in a label field.
I am following the video from
http://msdn.microsoft.com/vbasic/atthemovies/dataandxml/default.aspx (reading
XML data) almost step by step (changing to match my xmlfiles, etc)
Here is an example of my XML
<products>
<product>CP-90, T-32</product>
<company>Carwell</company>
<address>Street Address</address>
<city>City</city>
<state>State</state>
<zip>Zip</zip>
</products>
My load function (which works)
Dim xmlFile As String = "..\Products.xml"
Dim xmlDoc As XmlDocument
Private Sub Zadig_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim xmlTr As New XmlTextReader(xmlFile)
While xmlTr.Read
If xmlTr.Name = "product" AndAlso xmlTr.NodeType =
XmlNodeType.Element Then
cmbProducts.Items.Add(xmlTr.ReadString)
End If
End While
xmlTr.Close()
xmlDoc = New XmlDocument
xmlDoc.Load(xmlFile)
End Sub
My selection method
Private Sub cmbProducts_SelectionChangeCommitted(ByVal sender As Object,
ByVal e As System.EventArgs) Handles cmbProducts.SelectionChangeCommitted
DisplayProduct(cmbProducts.SelectedIndex + 1)
End Sub
Here is where the problem begins
Sub DisplayProduct(ByVal position As Integer)
Dim node As XmlNode = xmlDoc.SelectSingleNode( _
"/products/product[" & position & "]")
lblCompany.Text = node.SelectSingleNode("company").InnerText
End Sub
Everything compiles fine but when I run and then select a product I get.
An unhandled exception of type 'System.NullReferenceException' occurred in
Zadig.exe
Additional information: Object reference not set to an instance of an object.
The error says it is on line "lblCompany.Text =
node.SelectSingleNode("company").InnerText" but I have no idea what the real
problem is.
Please advise.
Michael