• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 247
  • Last Modified:

webservice and arraylist

I have a webservice that needs to return paired lists.  I have tried arraylist and several other collections, but get an error about not being able to seralize idictionary or collections.  How do i get the webservice to return a paired list, such as created in an arraylist?

code*********************

Dim lvSqlString = "SELECT * FROM PRODUCTS"
        Dim myData As New ArrayList

        Dim lvSqlCmd As New MySqlCommand
        Dim SqlConnection1 As New MySqlConnection
        Dim rdr As MySqlDataReader
        lvSqlCmd = SqlConnection1.CreateCommand
        SqlConnection1.ConnectionString = gvConnectString
        SqlConnection1.Open()
        rdr = lvSqlCmd.ExecuteReader()
        Dim bozo As String = ""
        Try
            Do While rdr.Read
                myData.Add("ProductID")
                myData.Add(rdr("ProductID"))
                myData.Add("ProductDesc")
                myData.Add(rdr("ProductDesc"))
                myData.Add("ProductGraphic")
                myData.Add(rdr("ProductGraphic"))
            Loop
        Finally
            If Not (rdr Is Nothing) Then
                rdr.Close()
                rdr = Nothing
            End If
            If Not (SqlConnection1 Is Nothing) Then
                SqlConnection1.Close()
                SqlConnection1 = Nothing
            End If
        End Try

        Return myData

end code*********************

Thanks for yoour help!
0
everettwright
Asked:
everettwright
  • 3
  • 3
1 Solution
 
razorback041Commented:
if you are running .net 2.0 you can do it with generics..its nice

public class PairedList()

public property ProductID1() as string
get/set

public Property ProductID2() as string
get/set

public Property ProductDesc1() as string
get/set

etc...

end class

private class PairedListCollection()
inherits list(of PairedList)
end class

then have a class to pass accross the service as an array

public class PassingList()
public Products() as string
end class

then build your collection

dim myDataCollection as PairedListCollection = new PairedListCollection

  Do While rdr.Read
                dim myData as PairedList = new PairedList
                myData.ProductID1("ProductID")
                myData.ProductID2(rdr("ProductID"))
                myData.ProductDesc1("ProductDesc")
                myData.ProductDesc2rdr("ProductDesc"))
       etc..
MyDataCollection.add(MyData)
            Loop

return the collection as a class
dim myList as PassingList = new PassingList

mylist.Products = MyDataCollection.toarray

that serializes perfectly

This code wasn't tested...but the basics of generics are there...don't forget to import system.collections.generic

and sorry,but .net 2.0 is the only thing i have done with webservices and xml....so it's all i know , and this is/was very easy to implement



0
 
everettwrightAuthor Commented:
razorback041 , thanks for your help.
I have read the microsoft doc on this (shudder) and would appreciate some clarification.  I assume that I have to create a seperate class file since I get errors trying to put the PairedList class in the webservice form.  I also assume that the property code should look like:

Public Class PairedList
    Private productId, productIdValue,  ProductDesc, ProductDescValue, ProductGraphic, ProductGraphicValue
    Public Property ProductID1() As String
        Get
            Return productId
        End Get
        Set(ByVal value As String)
            productId = value
        End Set
    End Property
Public Property ProductID2() As String
        Get
            Return productIdValue
        End Get
        Set(ByVal value As String)
            productIdValue= value
        End Set
    End Property

Public Property ProductDesc1() As String
        Get
            Return ProductDesc
        End Get
        Set(ByVal value As String)
            ProductDesc= value
        End Set
    End Property
Public Property ProductDesc2() As String
        Get
            Return ProductDescValue
        End Get
        Set(ByVal value As String)
            ProductDescValue= value
        End Set
    End Property

Public Property ProductGraphic1() As String
        Get
            Return ProductGraphic
        End Get
        Set(ByVal value As String)
            ProductGraphic= value
        End Set
    End Property
Public Property ProductGraphic2() As String
        Get
            Return ProductGraphicValue
        End Get
        Set(ByVal value As String)
            ProductGraphicValue = value
        End Set
    End Property


Then in the collection it would be
myData.ProductID1 = ("ProductID")
myData.ProductID2= rdr("ProductID")

myData.ProductDesc1= ("ProductDesc")
myData.ProductDesc2= rdr("ProductDesc")

myData.ProductGraphic1= ("ProductGraphic")
myData.ProductGraphic2= rdr("ProductGraphic")

Then to return the data -

Dim myList As PassingList = New PassingList

        myList.Products = myDataCollection.ToArray

        Return myList

The error I get is on myDataCollection.ToArray Value of type '1-dimensional array of PairedList' cannot be converted to '1-dimensional array of String' because 'PairedList' is not derived from 'String'

and on Return myList Value of type 'PassingList' cannot be converted to 'System.Collections.ArrayList'.      

What am I missing?




0
 
razorback041Commented:
The private class declaration should be a subclass in the class where you are actually building the collection

so something like

public class Collectionbuilder()
have your sql reader etc in here

private class PairedListCollection()
inherits list(of PairedList)
end class

end class

I Think thats what you are asking there?


Do While rdr.Read
                dim myData as PairedList = new PairedList
                myData.ProductID1("ProductID")
                myData.ProductID2(rdr("ProductID"))
                myData.ProductDesc1("ProductDesc")
                myData.ProductDesc2rdr("ProductDesc"))
       etc..
MyDataCollection.add(MyData)
            Loop


on this part above...im not sure exactly what i meant...it should be something like

 myData.ProductID1 = sqlreader("ColumnName") <~ each column should have a different name...
            the ones above look the same...is rdr your reader?  I need more code to completely understand what was happening here


and finally..my screw up...this should be changed from

public class PassingList()
public Products() as string
end class

to
public class PassingList()
public Products() as PairedList
end class

typing to quickly..

try those, and report back, as I might have missed something again, as I am doing this all from my head without vs...

0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
everettwrightAuthor Commented:
Thanks for your help -
This is the class1.vb file code
Public Class PairedList
    Private vProductId, vProductIdValue, vProductDesc, vProductDescValue, vProductGraphic, vProductGraphicValue

    Public Property ProductIdLabel() As String
        Get
            Return vProductId
        End Get
        Set(ByVal value As String)
            vProductId = value
        End Set
    End Property
    Public Property ProductIdValue() As String
        Get
            Return vProductIdValue
        End Get
        Set(ByVal value As String)
            vProductIdValue = value
        End Set
    End Property

    Public Property ProductDescLabel() As String
        Get
            Return vProductDesc
        End Get
        Set(ByVal value As String)
            vProductDesc = value
        End Set
    End Property
    Public Property ProductDescValue() As String
        Get
            Return vProductDescValue
        End Get
        Set(ByVal value As String)
            vProductDescValue = value
        End Set
    End Property

    Public Property ProductGraphicLabel() As String
        Get
            Return vProductGraphic
        End Get
        Set(ByVal value As String)
            vProductGraphic = value
        End Set
    End Property
    Public Property ProductGraphicValue() As String
        Get
            Return vProductGraphicValue
        End Get
        Set(ByVal value As String)
            vProductGraphicValue = value
        End Set
    End Property

End Class



This is the service.vb Code

Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.Data
Imports System.IO
Imports System.ComponentModel
Imports System.Web.Mail
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls
Imports MySql.Data.MySqlClient
Imports system.collections.generic



<WebService(Namespace:="http://tempuri.org/")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Public Class Service
    Inherits System.Web.Services.WebService
    Public gvConnectString As String = ConfigurationSettings.AppSettings("connectString")


    <WebMethod()> _
    Public Function getMyData() As ArrayList
        Dim lvSqlString = "SELECT * FROM PRODUCTS"
        'Dim myData As New ArrayList
        Dim myList As PassingList = New PassingList
        Dim lvSqlCmd As New MySqlCommand
        Dim SqlConnection1 As New MySqlConnection
        Dim rdr As MySqlDataReader
        lvSqlCmd = SqlConnection1.CreateCommand
        SqlConnection1.ConnectionString = gvConnectString
        SqlConnection1.ConnectionString = "server='localhost'; user id='root'; password='admin'; database='simmons'"
        lvSqlCmd.CommandText = lvSqlString
        SqlConnection1.Open()
        rdr = lvSqlCmd.ExecuteReader()
        Dim myDataCollection As PairedListCollection = New PairedListCollection

        Try
            Do While rdr.Read
                Dim myData As PairedList = New PairedList
                myData.ProductIdLabel = ("ProductID")
                myData.ProductIdValue = rdr("ProductID")

                myData.ProductDescLabel = ("ProductDesc")
                myData.ProductDescValue = rdr("ProductDesc")

                myData.ProductGraphicLabel = ("ProductGraphic")
                myData.ProductGraphicValue = rdr("ProductGraphic")

                myDataCollection.Add(myData)
            Loop
        Finally
            If Not (rdr Is Nothing) Then
                rdr.Close()
                rdr = Nothing
            End If
            If Not (SqlConnection1 Is Nothing) Then
                SqlConnection1.Close()
                SqlConnection1 = Nothing
            End If
        End Try


        myList.Products = myDataCollection.ToArray

        Return myList



    End Function
    Private Class PairedListCollection
        Inherits List(Of PairedList)
    End Class
    Public Class PassingList
        Public Products() As PairedList
    End Class

End Class


Changing "public Products() as string" to "public Products() as PairedList" FIXED the "'1-dimensional array of PairedList'..." error however I am still getting the "'PassingList' cannot be converted to 'System.Collections.ArrayList'." error on the "Return myList" for the function.

thanks again!


0
 
razorback041Commented:
Public Function getMyData() As ArrayList

change to

Public Function getMyData() As PassingList

and that will be the class recieved by the calling app.  so that app would just do something like

 myService.Timeout = 300000(or whatever you need)

        myService.Credentials = System.Net.CredentialCache.DefaultCredentials ' set credentials
        Dim myReturn As WebService.PassingList = myService.getMyData() ' dim the passlist as the webservice reference.class = webservice.webmethod

then

myReturn.products = an array of products that you can parse out however you want

again..post back the results..and we will go from there.

good luck!
0
 
everettwrightAuthor Commented:
Thanks for your very timely assistance!

Now I have to figure out how to work around the fact that the application is looking for <anyType xsi:type="Structhouse"> </anytype> while the xml I am sending out has  <PairedList> </PairedList>

Thank you for your help.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now