XML-Combobox VB.net Probelm

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • fremenusul

    #1

    XML-Combobox VB.net Probelm

    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/att...l/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>Carwel l</company>
    <address>Stre et Address</address>
    <city>City</city>
    <state>State</state>
    <zip>Zip</zip>
    </products>

    My load function (which works)

    Dim xmlFile As String = "..\Products.xm l"
    Dim xmlDoc As XmlDocument
    Private Sub Zadig_Load(ByVa l sender As System.Object, ByVal e As
    System.EventArg s) Handles MyBase.Load
    Dim xmlTr As New XmlTextReader(x mlFile)

    While xmlTr.Read
    If xmlTr.Name = "product" AndAlso xmlTr.NodeType =
    XmlNodeType.Ele ment Then
    cmbProducts.Ite ms.Add(xmlTr.Re adString)
    End If
    End While
    xmlTr.Close()

    xmlDoc = New XmlDocument
    xmlDoc.Load(xml File)

    End Sub

    My selection method
    Private Sub cmbProducts_Sel ectionChangeCom mitted(ByVal sender As Object,
    ByVal e As System.EventArg s) Handles cmbProducts.Sel ectionChangeCom mitted
    DisplayProduct( cmbProducts.Sel ectedIndex + 1)
    End Sub

    Here is where the problem begins

    Sub DisplayProduct( ByVal position As Integer)
    Dim node As XmlNode = xmlDoc.SelectSi ngleNode( _
    "/products/product[" & position & "]")
    lblCompany.Text = node.SelectSing leNode("company ").InnerTex t

    End Sub

    Everything compiles fine but when I run and then select a product I get.
    An unhandled exception of type 'System.NullRef erenceException ' occurred in
    Zadig.exe

    Additional information: Object reference not set to an instance of an object.

    The error says it is on line "lblCompany.Tex t =
    node.SelectSing leNode("company ").InnerTex t" but I have no idea what the real
    problem is.

    Please advise.

    Michael

  • fremenusul

    #2
    RE: XML-Combobox VB.net Probelm

    My connection was flaky, sorry for the double post.

    "fremenusul " wrote:
    [color=blue]
    > 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/att...l/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>Carwel l</company>
    > <address>Stre et Address</address>
    > <city>City</city>
    > <state>State</state>
    > <zip>Zip</zip>
    > </products>
    >
    > My load function (which works)
    >
    > Dim xmlFile As String = "..\Products.xm l"
    > Dim xmlDoc As XmlDocument
    > Private Sub Zadig_Load(ByVa l sender As System.Object, ByVal e As
    > System.EventArg s) Handles MyBase.Load
    > Dim xmlTr As New XmlTextReader(x mlFile)
    >
    > While xmlTr.Read
    > If xmlTr.Name = "product" AndAlso xmlTr.NodeType =
    > XmlNodeType.Ele ment Then
    > cmbProducts.Ite ms.Add(xmlTr.Re adString)
    > End If
    > End While
    > xmlTr.Close()
    >
    > xmlDoc = New XmlDocument
    > xmlDoc.Load(xml File)
    >
    > End Sub
    >
    > My selection method
    > Private Sub cmbProducts_Sel ectionChangeCom mitted(ByVal sender As Object,
    > ByVal e As System.EventArg s) Handles cmbProducts.Sel ectionChangeCom mitted
    > DisplayProduct( cmbProducts.Sel ectedIndex + 1)
    > End Sub
    >
    > Here is where the problem begins
    >
    > Sub DisplayProduct( ByVal position As Integer)
    > Dim node As XmlNode = xmlDoc.SelectSi ngleNode( _
    > "/products/product[" & position & "]")
    > lblCompany.Text = node.SelectSing leNode("company ").InnerTex t
    >
    > End Sub
    >
    > Everything compiles fine but when I run and then select a product I get.
    > An unhandled exception of type 'System.NullRef erenceException ' occurred in
    > Zadig.exe
    >
    > Additional information: Object reference not set to an instance of an object.
    >
    > The error says it is on line "lblCompany.Tex t =
    > node.SelectSing leNode("company ").InnerTex t" but I have no idea what the real
    > problem is.
    >
    > Please advise.
    >
    > Michael
    >[/color]

    Comment

    Working...