Databinding Marshalled Data From WebService

B

Brad

I have a web service which returns a collection of class object (see below).
I want to consume this service in another web application by binding it to a
List control The data returns from the web service, however when I bind
the data I get the following error
****
DataBinder.Eval: 'UserServices.User' does not contain a property with the
name FirstName.
****
Debugging I see that the list controls data source is a System.Array with
all of my data; each array element does indeed have an object with the
correct properties and FirstName is a public property/string. What am I
missing?

Brad

(cross posted to microsoft.public.dotnet.framework.aspnet
&
microsoft.public.dotnet.framework.aspnet.webservices
)


Code examples
==================================
Web Service (example only...real code is more and pulls data from database)
==================================
Public Class User
Public UserID as integer
Public FirstName as string
End class

Public Class UserServices
Inherits System.Web.Services.WebService
<WebMethod()> _
Public Function GetUsers(ByVal appName As String) As User()
Dim myUsers(1) as New User
Dim userA as New User
userA.UserID = 1
userA.FirstName = "Fred"
myUsers(0) = userA

Dim userB as New User
userB.UserID = 2
userB.FirstName = "Barney"
myUsers(1) = userB
return myUsers
End Function


==================================
Consumer code
==================================

Dim svc As New Services.UserServices
Dim ui() As svc.UserInfo = ps.GetUsers()
UserList.DataTextField = "FirstName"
UserList.DataValueField = "UserID"
UserList.DataSource = ui
UserList.DataBind()
 
M

MSFT

Hi Brad,

You need to set "UserID" and "FirstName" as Property, not a field. For
example:

Public Class MyUser

Private _UserID As Integer
Private _FirstName As Integer

Public Property UserID() As Integer
Get
Return _UserID
End Get
Set(ByVal Value As Integer)
_UserID = Value
End Set
End Property
Public Property FirstName() As String
Get
Return _FirstName
End Get
Set(ByVal Value As String)

_FirstName = Value
End Set
End Property

End Class

Hope this help,

Luke
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
J

Jan Tielens

Hi,

Actually Luke is right. You can't bind to public fields, only to public
properties. The proxy classes generated by VS.NET have public fields, so you
can't bind to them. BUT there is a solution for this. I've built a wrapper
class the transforms public fields into propeties at runtime. Actually I've
written an article about this problem which you can find here:
http://www.microsoft.com/belux/nl/msdn/community/columns/jtielens/webservicewrapper.mspx
The data binding capabilities in .NET are great, you can bind many controls
to almost any type of data, but in some scenarios data binding has its
limitations. For example data binding is not possible when using custom
collections, instead of DataSets, coming from a Web Service. This article
explains the problem and a possible, easy-to-use, solution: a class that
dynamically builds wrapper classes at run time, which exposes the field
member of the proxy classes as properties.
 
C

Christian Weyer

J

Jan Tielens

Hi Christian

I saw your tool on your blog some time ago, nice work man! Do you have any
idea why MS implemented public fields instead of public properties on proxy
classes? I tested this in Whidbey, and there is still the same behaviour...
:-/

--
Greetz,
Jan
__________________________________
Read my weblog: http://weblogs.asp.net/jan
Christian Weyer said:
Or more easily just use a tool that automatically can generate the code you
http://www.gotdotnet.com/Community/...mpleGuid=6a6acbb4-79a4-4907-a11f-e001fac664a6
No need for wsdl.exe or Web Reference (only caveat for some users currently
is that the WSDL has to be present locally).

Cheers,
--
Christian Weyer
[Microsoft Regional Director, Germany]
[MVP ASP.NET & XML Web Services]

** XML Web Services: http://www.xmlwebservices.cc/
** Weblog: http://weblogs.asp.net/cweyer/



Jan Tielens said:
Hi,

Actually Luke is right. You can't bind to public fields, only to public
properties. The proxy classes generated by VS.NET have public fields, so you
can't bind to them. BUT there is a solution for this. I've built a wrapper
class the transforms public fields into propeties at runtime. Actually I've
written an article about this problem which you can find here:
http://www.microsoft.com/belux/nl/msdn/community/columns/jtielens/webservicewrapper.mspx
The data binding capabilities in .NET are great, you can bind many controls
to almost any type of data, but in some scenarios data binding has its
limitations. For example data binding is not possible when using custom
collections, instead of DataSets, coming from a Web Service. This article
explains the problem and a possible, easy-to-use, solution: a class that
dynamically builds wrapper classes at run time, which exposes the field
member of the proxy classes as properties.

--
Greetz,
Jan
__________________________________
Read my weblog: http://weblogs.asp.net/jan
it
am
 
C

Christian Weyer

Hi Jan,

hm, AFAIK, it then was just a matter of time (and not thinking enough about
the problem space?) - but I guess they will support public properties in the
Beta. Actually it is just three lines of code they have to change ;-)

Cheers,
--
Christian Weyer
[Microsoft Regional Director, Germany]
[MVP ASP.NET & XML Web Services]

** XML Web Services: http://www.xmlwebservices.cc/
** Weblog: http://weblogs.asp.net/cweyer/



Jan Tielens said:
Hi Christian

I saw your tool on your blog some time ago, nice work man! Do you have any
idea why MS implemented public fields instead of public properties on proxy
classes? I tested this in Whidbey, and there is still the same behaviour...
:-/

--
Greetz,
Jan
__________________________________
Read my weblog: http://weblogs.asp.net/jan
Christian Weyer said:
Or more easily just use a tool that automatically can generate the code you
http://www.gotdotnet.com/Community/...mpleGuid=6a6acbb4-79a4-4907-a11f-e001fac664a6
No need for wsdl.exe or Web Reference (only caveat for some users currently
is that the WSDL has to be present locally).

Cheers,
--
Christian Weyer
[Microsoft Regional Director, Germany]
[MVP ASP.NET & XML Web Services]

** XML Web Services: http://www.xmlwebservices.cc/
** Weblog: http://weblogs.asp.net/cweyer/



Jan Tielens said:
Hi,

Actually Luke is right. You can't bind to public fields, only to public
properties. The proxy classes generated by VS.NET have public fields,
so
you
can't bind to them. BUT there is a solution for this. I've built a wrapper
class the transforms public fields into propeties at runtime. Actually I've
written an article about this problem which you can find here:
http://www.microsoft.com/belux/nl/msdn/community/columns/jtielens/webservicewrapper.mspx
it I
bind with
the am
 

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