0

I would like to feed data to a proxy which creates a soap XML request formatted as below:

<dat:MusicCollection>
           <!--Zero or more repetitions:-->
           <dat:Song>
              <dat:songUserkey>TakemeHome</dat:songUserkey>
           </dat:Song>
</dat:MusicCollection>

I have written the file to call the service and provide the details as below:

dim ucizi1 as SongRequest 'this is the request class in the proxy
dim Songs as Song = New Song
Songs.songUserKey = "TakeMeHome"
dim ucz
ucz = Songs.SongUserKey
ucizi1.SongCollection.Add(ucz)

The MusicCollection class is as follows:

<System.Diagnostics.DebuggerStepThroughAttribute(),  _
 System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0"),  _
 System.Runtime.Serialization.CollectionDataContractAttribute(Name:="ProductCollection", [Namespace]:="http://ucizi.Musicservice/DataContracts", ItemName:="Song")>  _
Public Class SongCollection
    Inherits System.Collections.Generic.List(Of ucizi.Music.DataContracts.Song)
End Class

The song class is as follows:

<System.Diagnostics.DebuggerStepThroughAttribute(),  _
 System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0"),  _
 System.Runtime.Serialization.DataContractAttribute(Name:="Product", [Namespace]:="http://Ucizi.Music/DataContracts")>  _
Partial Public Class Product
    Inherits Object
    Implements System.Runtime.Serialization.IExtensibleDataObject

    Private extensionDataField As System.Runtime.Serialization.ExtensionDataObject

    Private SongUserkeyField As String

    Public Property ExtensionData() As System.Runtime.Serialization.ExtensionDataObject Implements System.Runtime.Serialization.IExtensibleDataObject.ExtensionData
        Get
            Return Me.extensionDataField
        End Get
        Set
            Me.extensionDataField = value
        End Set
    End Property

    <System.Runtime.Serialization.DataMemberAttribute(IsRequired:=true)>  _
    Public Property SongUserkey() As String
        Get
            Return Me.SongUserkeyField
        End Get
        Set
            Me.SongUserkeyField = value
        End Set
    End Property
End Class

However, when I run this code, I get an error: unable to cast object of type 'system.string' to type 'ucizi.music.DataContracts.Song'.

I cant see where this error is coming from, can some1 please help me and advise how I can correct this.

John Saunders
  • 160,644
  • 26
  • 247
  • 397

2 Answers2

0

In lines

Songs.songUserKey = "TakeMeHome"
dim ucz
ucz = Songs.SongUserKey
ucizi.SongCollection.Add(ucz)

you set ucz to be SongUserKey - which is string.

Then, you add it to collection SongCollection

Public Class SongCollection
    Inherits System.Collections.Generic.List(Of ucizi.Music.DataContracts.Song)
End Class

Which is expected Song

evgenyl
  • 7,837
  • 2
  • 27
  • 32
  • Ok I get where the error is coming from, I have tried to add songs to the songcollection instead of adding ucz but i am now getting an error 'object reference not set to an instance of an object'. – user2211448 Apr 17 '13 at 03:33
  • 1
    Probably you need to init collection of ucizi1.SongCollection before calling Add - like ucizi1.SongCollection = new List(); – evgenyl Apr 17 '13 at 04:25
  • 1
    Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders Apr 17 '13 at 04:31
  • I have sorted the issue, Thanks guys. – user2211448 Apr 17 '13 at 06:11
  • @user2211448: you should post an answer showing how you "sorted it" – John Saunders Apr 17 '13 at 14:07
0

added the following code after the Songs.songUserKey = "TakeMeHome"

dim ucizi2 as songCollection
ucizi2.Add(song)
ucizi1.songcollection = ucizi2

This sorted the problem smoothly. You guys opened my eyes.