Home All Groups Group Topic Archive Search About

adding xml data to a combobox

Author
2 May 2007 2:17 PM
spowel4
Here's my code thus far:

Private Sub Form1_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
        Dim objxmldoc As New Xml.XmlDataDocument()
        'Load all customer numbers
        objxmldoc.Load("s:\sw7\nc\custnum.xml")
        Dim onode As Xml.XmlNode
        Dim custNum As Xml.XmlNodeList
        custNum =
objxmldoc.GetElementsByTagName("AR11_CUSTOMER_NUMBER")


        For Each onode In custNum
            If onode.HasChildNodes Then
                Dim inode, inodes As Integer
                inodes = onode.ChildNodes.Count
                For inode = 0 To inodes - 1

ComboBoxCustNum.Items.Add(onode.ChildNodes(inode).InnerText)
                Next
            End If
        Next
    End Sub

I'm using this to load data into a combobox and it's working but my
problem is there are duplicate entries in the xml file and so
duplicates are being created in the combobox.  How do I suppress/
eliminate duplicates?  This is with VB2005.

Author
2 May 2007 2:37 PM
rowe_newsgroups
On May 2, 10:17 am, spowel4 <spow***@gmail.com> wrote:
Show quoteHide quote
> Here's my code thus far:
>
> Private Sub Form1_Load(ByVal sender As Object, ByVal e As
> System.EventArgs) Handles Me.Load
>         Dim objxmldoc As New Xml.XmlDataDocument()
>         'Load all customer numbers
>         objxmldoc.Load("s:\sw7\nc\custnum.xml")
>         Dim onode As Xml.XmlNode
>         Dim custNum As Xml.XmlNodeList
>         custNum =
> objxmldoc.GetElementsByTagName("AR11_CUSTOMER_NUMBER")
>
>         For Each onode In custNum
>             If onode.HasChildNodes Then
>                 Dim inode, inodes As Integer
>                 inodes = onode.ChildNodes.Count
>                 For inode = 0 To inodes - 1
>
> ComboBoxCustNum.Items.Add(onode.ChildNodes(inode).InnerText)
>                 Next
>             End If
>         Next
>     End Sub
>
> I'm using this to load data into a combobox and it's working but my
> problem is there are duplicate entries in the xml file and so
> duplicates are being created in the combobox.  How do I suppress/
> eliminate duplicates?  This is with VB2005.

Try changing this

>         For Each onode In custNum
>             If onode.HasChildNodes Then
>                 Dim inode, inodes As Integer
>                 inodes = onode.ChildNodes.Count
>                 For inode = 0 To inodes - 1
> ComboBoxCustNum.Items.Add(onode.ChildNodes(inode).InnerText)
>                 Next
>             End If
>         Next

to something like this:

         For Each onode In custNum
             If onode.HasChildNodes Then
                 Dim inode, inodes As Integer
                 inodes = onode.ChildNodes.Count
                 For inode = 0 To inodes - 1
                     Dim item As Object =
onode.ChildNodes(inode).InnerText
                     If Not ComboBoxCustNum.Item.Contains(item) Then
                         ComboBoxCustNum.Items.Add(item)
                     End If
                 Next
             End If
         Next

That way it will look to see if the combobox contains the item before
adding it. I typed that in the message, so beware!

Thanks,

Seth Rowe
Author
2 May 2007 4:23 PM
spowel4
On May 2, 10:37 am, rowe_newsgroups <rowe_em***@yahoo.com> wrote:
Show quoteHide quote
> On May 2, 10:17 am, spowel4 <spow***@gmail.com> wrote:
>
>
>
> > Here's my code thus far:
>
> > Private Sub Form1_Load(ByVal sender As Object, ByVal e As
> > System.EventArgs) Handles Me.Load
> >         Dim objxmldoc As New Xml.XmlDataDocument()
> >         'Load all customer numbers
> >         objxmldoc.Load("s:\sw7\nc\custnum.xml")
> >         Dim onode As Xml.XmlNode
> >         Dim custNum As Xml.XmlNodeList
> >         custNum =
> > objxmldoc.GetElementsByTagName("AR11_CUSTOMER_NUMBER")
>
> >         For Each onode In custNum
> >             If onode.HasChildNodes Then
> >                 Dim inode, inodes As Integer
> >                 inodes = onode.ChildNodes.Count
> >                 For inode = 0 To inodes - 1
>
> > ComboBoxCustNum.Items.Add(onode.ChildNodes(inode).InnerText)
> >                 Next
> >             End If
> >         Next
> >     End Sub
>
> > I'm using this to load data into a combobox and it's working but my
> > problem is there are duplicate entries in the xml file and so
> > duplicates are being created in the combobox.  How do I suppress/
> > eliminate duplicates?  This is with VB2005.
>
> Try changing this
>
> >         For Each onode In custNum
> >             If onode.HasChildNodes Then
> >                 Dim inode, inodes As Integer
> >                 inodes = onode.ChildNodes.Count
> >                 For inode = 0 To inodes - 1
> > ComboBoxCustNum.Items.Add(onode.ChildNodes(inode).InnerText)
> >                 Next
> >             End If
> >         Next
>
> to something like this:
>
>          For Each onode In custNum
>              If onode.HasChildNodes Then
>                  Dim inode, inodes As Integer
>                  inodes = onode.ChildNodes.Count
>                  For inode = 0 To inodes - 1
>                      Dim item As Object =
> onode.ChildNodes(inode).InnerText
>                      If Not ComboBoxCustNum.Item.Contains(item) Then
>                          ComboBoxCustNum.Items.Add(item)
>                      End If
>                  Next
>              End If
>          Next
>
> That way it will look to see if the combobox contains the item before
> adding it. I typed that in the message, so beware!
>
> Thanks,
>
> Seth Rowe

Thanks for the tip Seth, here's my final solution that seems to be
working:

Dim item As Object
        custNum =
objxmldoc.GetElementsByTagName("AR11_CUSTOMER_NUMBER")

        For Each onode In custNum
            item = New Object
            If onode.HasChildNodes Then
                inodes = onode.ChildNodes.Count
                For inode = 0 To inodes - 1
                    item = onode.ChildNodes(inode).InnerText
                    If Not ComboBoxCustNum.Items.Contains(item) Then
                        ComboBoxCustNum.Items.Add(item)
                    End If
                Next
            End If
        Next

For some reason, if I declared the item variable within the For loop I
kept getting an error about the object was referencing a null value,
so I declared it further up and I guess the item = New Object line
basically initializes the variable each time?
I'm new to vb so hopefully I'm saying this right.