1.11M Members

Consuming a Web Service with a Class as the Result

 
0
 

I need to consume a web service in VS 2010 using VB.NET that gives a result of a class type.

I've already added the reference and called it with:

Dim mag As New Magento.ecommerce

I can create a variable as the class:

Dim Stockcodes As New List(Of Magento.clsStockcode)

I just don't understand now how to make my Stockcodes variable get the data from the Web Service.

I've tried variations of:

Stockcodes = mag.ReturnNewStockodes("admin", "password")

and

Stockcodes.Add(New Stockcodes(mag.ReturnNewStockodes("admin", "password"))

but really I'm not sure what I'm doing.

I'm guessing what I need is the result of the Web Service as a type of dataset but I'm not sure.

The class is:

<clsStockcode>
<_Stockcode>string</_Stockcode>
<_Description>string</_Description>
<_CostPrice>string</_CostPrice>
<_PriceExclusive>string</_PriceExclusive>
<_PriceInclusive>string</_PriceInclusive>
<_TaxClass>string</_TaxClass>
<_QtyOnHand>string</_QtyOnHand>
<_IsBlocked>string</_IsBlocked>
</clsStockcode>
 
0
 

Hi,
Do you have access to the source code of the class? Is the function ReturnNewStockodes public in the class? What should the ReturnNewStockodes return i.e. a single clsStockcode or a list of them?

Stockcodes = mag.ReturnNewStockodes("admin", "password")

Would imply a list of clsStockcodes where if you only expected a single one you would do something like this:

Stockcodes.Add(mag.ReturnNewStockodes("admin", "password"))

Also while I was typing I noticed you had used ReturnNewStockodes not ReturnNewStockCodes - just in case there is a typo...

 
0
 

Hi,

Yes, the class returns a list of stockcodes.

I made the typo when I was formatting the code for the question :)

I've taken out the main parts of the function here:

<WebMethod()> _
    Public Function ReturnNewStockodesByVal UserName As String, _
                                       ByVal Password As String) As List(Of clsStockcode)


            Dim Stockcodes As New List(Of clsStockcode)

            For Each dr As DataRow In Ds.Tables(0).Rows

                RecordsReturned += 1
                Stockcodes.Add(New clsStockcode(dr("generated_code") & "", dr("description") & "", dr("cost_price") & "", dr("selling_price_1") & "", _
                                                dr("sales_tax_group") & "", dr("qty_on_hand") & "", dr("is_blocked") & ""))
            Next

            Return Stockcodes

End Function

And the class for stockcodes:

Imports Microsoft.VisualBasic

Public Class clsStockcode

Public Property _Stockcode As String
Public Property _Description As String
Public Property _CostPrice As String
Public Property _PriceExclusive As String
Public Property _PriceInclusive As String
Public Property _TaxClass As String
Public Property _QtyOnHand As String
Public Property _IsBlocked As String

Public Sub New(ByVal Stockcode As String, ByVal Description As String, ByVal CostPrice As String, ByVal PriceExclusive As String,
               ByVal TaxClass As String, ByVal QtyOnHand As String, ByVal IsBlocked As String)

    _Stockcode = Stockcode
    _Description = Description
    _CostPrice = CostPrice
    _PriceExclusive = PriceExclusive
    _TaxClass = TaxClass
    _QtyOnHand = QtyOnHand
    _IsBlocked = IsBlocked

End Sub

Public Sub New()

End Sub
 
0
 

Hi,

If it returns a list, you should be able to navigate through it:

Dim Stockcodes As New List(Of magento.clsStockcode)
dim Stockcode as magento.clsStockcode
dim code as string

'Get your stockcodes
If Stockcodes.items.count > 0 then 'I think it's items.count, could just be .count
    For each Stockcode in StockCodes
        'work with the stockcode e.g.
        code = Stockcode._StockCode
    next
end if
 
0
 

Hi,

Thanks for the help so far!

The major thing that I'm not understanding is how to retrieve the list from the web service into a variable.

 
1
 

Hi,

Does this not work?

Dim mag As New Magento.ecommerce
Dim Stockcodes As New List(Of magento.clsStockcode) 
Stockcodes = mag.ReturnNewStockodes("admin", "password")

If not, try replacing magento.clsStockcode with mag.clsStockcode

 
0
 

Thanks for you help again!

I iterated through the returned list and all works perfectly now.

Dim mag As New Magento.ecommerce

        If TypeOf mag.ReturnNewStockodes("admin", "password") Is IEnumerable Then

            For Each o As Object In TryCast(mag.ReturnNewStockodes("admin", "password"), IEnumerable)
                Console.WriteLine(DirectCast(o, WindowsApplication1.Magento.clsStockcode)._Stockcode)
                'ETC
            Next
        Else
            For Each p As System.Reflection.PropertyInfo In mag.ReturnNewStockodes("admin", "password").GetType().GetProperties()
                If p.CanRead Then
                    Console.WriteLine("{0}: {1}", p.Name, p.GetValue(mag.ReturnNewStockodes("admin", "password"), Nothing))  'possible function
                End If
            Next
        End If
Question Answered as of 1 Year Ago by G_Waddell
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: