adding xml data to a combobox

S

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.
 
R

rowe_newsgroups

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
 
S

spowel4

Try changing this


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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top