Connecting Tech Pros Worldwide Forums | Help | Site Map

Databinding Marshalled Data From WebService

Brad
Guest
 
Posts: n/a
#1: Nov 18 '05
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.webservic es
)


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()



MSFT
Guest
 
Posts: n/a
#2: Nov 18 '05

re: Databinding Marshalled Data From WebService


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

Jan Tielens
Guest
 
Posts: n/a
#3: Nov 18 '05

re: Databinding Marshalled Data From WebService


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/ms...cewrapper.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
"Brad" <nospam@co.lane.or.us> schreef in bericht
news:enhdNRFwDHA.3216@TK2MSFTNGP11.phx.gbl...[color=blue]
> I have a web service which returns a collection of class object (see[/color]
below).[color=blue]
> I want to consume this service in another web application by binding it to[/color]
a[color=blue]
> 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.webservic es
> )
>
>
> Code examples
> ==================================
> Web Service (example only...real code is more and pulls data from[/color]
database)[color=blue]
> ==================================
> 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()
>
>[/color]


Brad
Guest
 
Posts: n/a
#4: Nov 18 '05

re: Databinding Marshalled Data From WebService


Great article, Jan. It clearly explains what and why. Thank you.

Brad



"Jan Tielens" <jan@no.spam.please.leadit.be> wrote in message
news:uEK4YzMwDHA.3116@TK2MSFTNGP11.phx.gbl...[color=blue]
> 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[/color]
you[color=blue]
> 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[/color]
I've[color=blue]
> written an article about this problem which you can find here:
>[/color]
http://www.microsoft.com/belux/nl/ms...cewrapper.mspx[color=blue]
> The data binding capabilities in .NET are great, you can bind many[/color]
controls[color=blue]
> 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
> "Brad" <nospam@co.lane.or.us> schreef in bericht
> news:enhdNRFwDHA.3216@TK2MSFTNGP11.phx.gbl...[color=green]
> > I have a web service which returns a collection of class object (see[/color]
> below).[color=green]
> > I want to consume this service in another web application by binding it[/color][/color]
to[color=blue]
> a[color=green]
> > List control The data returns from the web service, however when I[/color][/color]
bind[color=blue][color=green]
> > the data I get the following error
> > ****
> > DataBinder.Eval: 'UserServices.User' does not contain a property with[/color][/color]
the[color=blue][color=green]
> > name FirstName.
> > ****
> > Debugging I see that the list controls data source is a System.Array[/color][/color]
with[color=blue][color=green]
> > 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[/color][/color]
I[color=blue][color=green]
> > missing?
> >
> > Brad
> >
> > (cross posted to microsoft.public.dotnet.framework.aspnet
> > &
> > microsoft.public.dotnet.framework.aspnet.webservic es
> > )
> >
> >
> > Code examples
> > ==================================
> > Web Service (example only...real code is more and pulls data from[/color]
> database)[color=green]
> > ==================================
> > 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()
> >
> >[/color]
>
>[/color]


Christian Weyer
Guest
 
Posts: n/a
#5: Nov 18 '05

re: Databinding Marshalled Data From WebService


Or more easily just use a tool that automatically can generate the code you
need:
http://www.gotdotnet.com/Community/U...f-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" <jan@no.spam.please.leadit.be> wrote in message
news:uEK4YzMwDHA.3116@TK2MSFTNGP11.phx.gbl...[color=blue]
> 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[/color]
you[color=blue]
> 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[/color]
I've[color=blue]
> written an article about this problem which you can find here:
>[/color]
http://www.microsoft.com/belux/nl/ms...cewrapper.mspx[color=blue]
> The data binding capabilities in .NET are great, you can bind many[/color]
controls[color=blue]
> 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
> "Brad" <nospam@co.lane.or.us> schreef in bericht
> news:enhdNRFwDHA.3216@TK2MSFTNGP11.phx.gbl...[color=green]
> > I have a web service which returns a collection of class object (see[/color]
> below).[color=green]
> > I want to consume this service in another web application by binding it[/color][/color]
to[color=blue]
> a[color=green]
> > List control The data returns from the web service, however when I[/color][/color]
bind[color=blue][color=green]
> > the data I get the following error
> > ****
> > DataBinder.Eval: 'UserServices.User' does not contain a property with[/color][/color]
the[color=blue][color=green]
> > name FirstName.
> > ****
> > Debugging I see that the list controls data source is a System.Array[/color][/color]
with[color=blue][color=green]
> > 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[/color][/color]
I[color=blue][color=green]
> > missing?
> >
> > Brad
> >
> > (cross posted to microsoft.public.dotnet.framework.aspnet
> > &
> > microsoft.public.dotnet.framework.aspnet.webservic es
> > )
> >
> >
> > Code examples
> > ==================================
> > Web Service (example only...real code is more and pulls data from[/color]
> database)[color=green]
> > ==================================
> > 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()
> >
> >[/color]
>
>[/color]


Jan Tielens
Guest
 
Posts: n/a
#6: Nov 18 '05

re: Databinding Marshalled Data From WebService


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" <cw@no.spam_eyesoft_sucks.de> schreef in bericht
news:OL4Ja3PwDHA.1660@TK2MSFTNGP09.phx.gbl...[color=blue]
> Or more easily just use a tool that automatically can generate the code[/color]
you[color=blue]
> need:
>[/color]
http://www.gotdotnet.com/Community/U...f-e001fac664a6[color=blue]
> No need for wsdl.exe or Web Reference (only caveat for some users[/color]
currently[color=blue]
> 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" <jan@no.spam.please.leadit.be> wrote in message
> news:uEK4YzMwDHA.3116@TK2MSFTNGP11.phx.gbl...[color=green]
> > 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[/color]
> you[color=green]
> > can't bind to them. BUT there is a solution for this. I've built a[/color][/color]
wrapper[color=blue][color=green]
> > class the transforms public fields into propeties at runtime. Actually[/color]
> I've[color=green]
> > written an article about this problem which you can find here:
> >[/color]
>[/color]
http://www.microsoft.com/belux/nl/ms...cewrapper.mspx[color=blue][color=green]
> > The data binding capabilities in .NET are great, you can bind many[/color]
> controls[color=green]
> > 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[/color][/color]
article[color=blue][color=green]
> > 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
> > "Brad" <nospam@co.lane.or.us> schreef in bericht
> > news:enhdNRFwDHA.3216@TK2MSFTNGP11.phx.gbl...[color=darkred]
> > > I have a web service which returns a collection of class object (see[/color]
> > below).[color=darkred]
> > > I want to consume this service in another web application by binding[/color][/color][/color]
it[color=blue]
> to[color=green]
> > a[color=darkred]
> > > List control The data returns from the web service, however when I[/color][/color]
> bind[color=green][color=darkred]
> > > the data I get the following error
> > > ****
> > > DataBinder.Eval: 'UserServices.User' does not contain a property with[/color][/color]
> the[color=green][color=darkred]
> > > name FirstName.
> > > ****
> > > Debugging I see that the list controls data source is a System.Array[/color][/color]
> with[color=green][color=darkred]
> > > all of my data; each array element does indeed have an object with the
> > > correct properties and FirstName is a public property/string. What[/color][/color][/color]
am[color=blue]
> I[color=green][color=darkred]
> > > missing?
> > >
> > > Brad
> > >
> > > (cross posted to microsoft.public.dotnet.framework.aspnet
> > > &
> > > microsoft.public.dotnet.framework.aspnet.webservic es
> > > )
> > >
> > >
> > > Code examples
> > > ==================================
> > > Web Service (example only...real code is more and pulls data from[/color]
> > database)[color=darkred]
> > > ==================================
> > > 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()
> > >
> > >[/color]
> >
> >[/color]
>
>[/color]


Christian Weyer
Guest
 
Posts: n/a
#7: Nov 18 '05

re: Databinding Marshalled Data From WebService


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" <jan@no.spam.please.leadit.be> wrote in message
news:#u1knMQwDHA.1596@TK2MSFTNGP10.phx.gbl...[color=blue]
> 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[/color]
proxy[color=blue]
> classes? I tested this in Whidbey, and there is still the same[/color]
behaviour...[color=blue]
> :-/
>
> --
> Greetz,
> Jan
> __________________________________
> Read my weblog: http://weblogs.asp.net/jan
> "Christian Weyer" <cw@no.spam_eyesoft_sucks.de> schreef in bericht
> news:OL4Ja3PwDHA.1660@TK2MSFTNGP09.phx.gbl...[color=green]
> > Or more easily just use a tool that automatically can generate the code[/color]
> you[color=green]
> > need:
> >[/color]
>[/color]
http://www.gotdotnet.com/Community/U...f-e001fac664a6[color=blue][color=green]
> > No need for wsdl.exe or Web Reference (only caveat for some users[/color]
> currently[color=green]
> > 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" <jan@no.spam.please.leadit.be> wrote in message
> > news:uEK4YzMwDHA.3116@TK2MSFTNGP11.phx.gbl...[color=darkred]
> > > Hi,
> > >
> > > Actually Luke is right. You can't bind to public fields, only to[/color][/color][/color]
public[color=blue][color=green][color=darkred]
> > > properties. The proxy classes generated by VS.NET have public fields,[/color][/color][/color]
so[color=blue][color=green]
> > you[color=darkred]
> > > can't bind to them. BUT there is a solution for this. I've built a[/color][/color]
> wrapper[color=green][color=darkred]
> > > class the transforms public fields into propeties at runtime. Actually[/color]
> > I've[color=darkred]
> > > written an article about this problem which you can find here:
> > >[/color]
> >[/color]
>[/color]
http://www.microsoft.com/belux/nl/ms...cewrapper.mspx[color=blue][color=green][color=darkred]
> > > The data binding capabilities in .NET are great, you can bind many[/color]
> > controls[color=darkred]
> > > to almost any type of data, but in some scenarios data binding has its
> > > limitations. For example data binding is not possible when using[/color][/color][/color]
custom[color=blue][color=green][color=darkred]
> > > collections, instead of DataSets, coming from a Web Service. This[/color][/color]
> article[color=green][color=darkred]
> > > explains the problem and a possible, easy-to-use, solution: a class[/color][/color][/color]
that[color=blue][color=green][color=darkred]
> > > dynamically builds wrapper classes at run time, which exposes the[/color][/color][/color]
field[color=blue][color=green][color=darkred]
> > > member of the proxy classes as properties.
> > >
> > > --
> > > Greetz,
> > > Jan
> > > __________________________________
> > > Read my weblog: http://weblogs.asp.net/jan
> > > "Brad" <nospam@co.lane.or.us> schreef in bericht
> > > news:enhdNRFwDHA.3216@TK2MSFTNGP11.phx.gbl...
> > > > 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[/color][/color]
> it[color=green]
> > to[color=darkred]
> > > a
> > > > List control The data returns from the web service, however when[/color][/color][/color]
I[color=blue][color=green]
> > bind[color=darkred]
> > > > the data I get the following error
> > > > ****
> > > > DataBinder.Eval: 'UserServices.User' does not contain a property[/color][/color][/color]
with[color=blue][color=green]
> > the[color=darkred]
> > > > name FirstName.
> > > > ****
> > > > Debugging I see that the list controls data source is a System.Array[/color]
> > with[color=darkred]
> > > > all of my data; each array element does indeed have an object with[/color][/color][/color]
the[color=blue][color=green][color=darkred]
> > > > correct properties and FirstName is a public property/string. What[/color][/color]
> am[color=green]
> > I[color=darkred]
> > > > missing?
> > > >
> > > > Brad
> > > >
> > > > (cross posted to microsoft.public.dotnet.framework.aspnet
> > > > &
> > > > microsoft.public.dotnet.framework.aspnet.webservic es
> > > > )
> > > >
> > > >
> > > > 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()
> > > >
> > > >
> > >
> > >[/color]
> >
> >[/color]
>
>[/color]


Closed Thread