Getting Overload error with ASP.NET using VB

M

multisync

I'm getting the following error:

Overload resolution failed because no accessible 'Add' can be called
with these arguments: 'Public Sub Add(item As
System.Web.UI.WebControls.ListItem)': Value of type 'Admin.clsSites'
cannot be converted to 'System.Web.UI.WebControls.ListItem'

This is my code:
Public Sub LoadSites()
'Cursor.Current = System.Windows.Forms.Cursors.WaitCursor
'cboSites.BeginUpdate()
Dim xDoc As New XmlDocument
xDoc.Load(Server.MapPath("\config.sites.xml"))
Dim xNodelist As XmlNodeList =
xDoc.SelectNodes("/sitedata/site")

For Each xNode As XmlNode In xNodelist
Dim Site As New clsSites
Site.Name = xNode.Attributes.GetNamedItem("name").Value
Site.Server = xNode.ChildNodes.Item(0).InnerText
Me.cboSites.Items.Add(Site) <---ERROR HERE

Next
cboSites.EndUpdate()
Cursor.Current = System.Windows.Forms.Cursors.Default
End Sub

Anyone know how to fix this?
 
M

multisync

Public Class clsSites
Public Name As String
Public Server As String
Sub New()
MyBase.New()
End Sub
Public Overrides Function ToString() As String
Return Me.Name
End Function

End Class
 
M

Marina

Yes - don't try to call methods that don't exist.

There is no method Add in ListItemCollection that takes an instance of
clsSite. And how could there possibly be? That is a class you wrote! It's
not like Microsoft could possibly know about it and define an overload just
for you.
 
C

Chris Dunaway

multisync said:
For Each xNode As XmlNode In xNodelist
Dim Site As New clsSites
Site.Name = xNode.Attributes.GetNamedItem("name").Value
Site.Server = xNode.ChildNodes.Item(0).InnerText
Me.cboSites.Items.Add(Site) <---ERROR HERE
Next

The problem is that 'Site' is not an object of type 'ListItem' which is
what the cboSites.Items.Add method is expecting.

Try this (untested):

Me.cboSites.Items.Add(New ListItem(Site.Name, Site.Server))

What this does is create a new ListItem object, assign the properties
of your Site object to the text and value properties of the ListItem
and then finally adds the ListItem to the combo.
 
C

Cor Ligthert [MVP]

multisync,

I assume that the cbo is a dropdownlist.

As Marina wrote already can you not add an object to it, in a not databound
situation is it subpossed to get a string.

You can however use the datasource, the datatextfield and the
datavaluefield, however that is a complete other way of doing this.

I hope this helps,

Cor
 
Top