oshirowanen
oshirowanen

Reputation: 15935

Why is the webservice returning null's?

I have the following web-service:

<%@ WebService Language="VB" Class="WebService" %>

Imports System.Web
Imports System.Web.Script.Services
Imports System.Web.Services

' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
<System.Web.Script.Services.ScriptService()> _
<WebService(Namespace:="http://tempuri.org/")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
Public Class WebService
    Inherits System.Web.Services.WebService

    Public Class Person
        Public FirstName As String
        Public LastName As String

        Public Sub New(ByVal m_FirstName As String, ByVal m_LastName As String)
        End Sub
    End Class

    <ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
    <WebMethod()> _
    Public Function GetPersons() As List(Of Person)
        Dim lst As List(Of Person) = New List(Of Person)

        lst.Add(New Person("firstname_1", "surname_1"))
        lst.Add(New Person("firstname_2", "surname_2"))

        Return lst
    End Function
End Class

But it is returning:

{"d":[{"FirstName":null,"LastName":null},{"FirstName":null,"LastName":null}]}

It should be returning:

{"d":[{"FirstName":"firstname_1","LastName":"surname_1"},{"FirstName":"firstname_2","LastName":"surname_2"}]}

What am I doing wrong?

Upvotes: 0

Views: 420

Answers (2)

Brad Bruce
Brad Bruce

Reputation: 7827

My VB is a little rusty, but I don't see the FirstName and LastName properties in your Person class. I only see variables. (Along with the missing assignment that was previously mentioned)

Upvotes: 0

Jonathan van de Veen
Jonathan van de Veen

Reputation: 1026

You never assing FirstName and LastName in the constructor, so their values will stay null. Add the following to your constructor:

FirstName = m_FirstName
LastName = m_LastName

Upvotes: 2

Related Questions