468,289 Members | 1,867 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,289 developers. It's quick & easy.

Can Pass NameValueCollection as Parameter in WebMethod?

Hi
I want pass NameValueCollection as parameter in webmethod.
I try it but that give me error.
Here is Error.
You must implement the Add(System.String) method on
System.Collections.Specialized.NameValueCollection because it inherits from
ICollection.
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information about
the error and where it originated in the code.

Exception Details: System.InvalidOperationException: You must implement the
Add(System.String) method on
System.Collections.Specialized.NameValueCollection because it inherits from
ICollection.
Thanks
--
-MrDotNet MCAD, MCSD
Sr. Software Engineer / Lead Architect

Nov 23 '05 #1
2 6552
A NameValueCollection type cannot be used as a parameter to a Web Method.
For more information on which types can be serialized using XML see
http://msdn.microsoft.com/library/de...ebservices.asp

However, it is easy to achieve similar functionality e.g., by using the
following simple class:

public class NameValues
{
public NameValues()
{}

public NameValues(string name, string[] values)
{
Name = name;
Values = values;
}

public string Name;
public string[] Values;
}

Instead of trying to use a NameValueCollection as parameter you can use an
array of NameValues like so:

[WebMethod]
public void PassNameValues(NameValues[] namesAndValues)
{
foreach (NameValues nameValues in namesAndValues)
{
string name = nameValues.Name;
foreach (string value in nameValues.Values)
{
// ...
}
}
}

Regards,
Sami
"MrDotNet" <Mr******@discussions.microsoft.com> wrote in message
news:84**********************************@microsof t.com...
Hi
I want pass NameValueCollection as parameter in webmethod.
I try it but that give me error.
Here is Error.
You must implement the Add(System.String) method on
System.Collections.Specialized.NameValueCollection because it inherits
from
ICollection.
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about
the error and where it originated in the code.

Exception Details: System.InvalidOperationException: You must implement
the
Add(System.String) method on
System.Collections.Specialized.NameValueCollection because it inherits
from
ICollection.
Thanks
--
-MrDotNet MCAD, MCSD
Sr. Software Engineer / Lead Architect

Nov 23 '05 #2
and in general,
if you specify the web service interface using a W3C XML Schema (XSD), you
will always be able to transmit it, in a portable fashion.
This is sometimes called the "Contract First" or "Schema First" approach to
building web services.
For more info on the idea. . .

http://msdn.microsoft.com/vstudio/ja...p/default.aspx

or check out this cool tool from Christian Weyer
http://weblogs.asp.net/cweyer/archiv.../21/39070.aspx

-Dino

"Sami Vaaraniemi" <sa**********@pleasejippii.fi> wrote in message
news:eN**************@tk2msftngp13.phx.gbl...
A NameValueCollection type cannot be used as a parameter to a Web Method.
For more information on which types can be serialized using XML see
http://msdn.microsoft.com/library/de...ebservices.asp

However, it is easy to achieve similar functionality e.g., by using the
following simple class:

public class NameValues
{
public NameValues()
{}

public NameValues(string name, string[] values)
{
Name = name;
Values = values;
}

public string Name;
public string[] Values;
}

Instead of trying to use a NameValueCollection as parameter you can use an
array of NameValues like so:

[WebMethod]
public void PassNameValues(NameValues[] namesAndValues)
{
foreach (NameValues nameValues in namesAndValues)
{
string name = nameValues.Name;
foreach (string value in nameValues.Values)
{
// ...
}
}
}

Regards,
Sami
"MrDotNet" <Mr******@discussions.microsoft.com> wrote in message
news:84**********************************@microsof t.com...
Hi
I want pass NameValueCollection as parameter in webmethod.
I try it but that give me error.
Here is Error.
You must implement the Add(System.String) method on
System.Collections.Specialized.NameValueCollection because it inherits
from
ICollection.
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about
the error and where it originated in the code.

Exception Details: System.InvalidOperationException: You must implement
the
Add(System.String) method on
System.Collections.Specialized.NameValueCollection because it inherits
from
ICollection.
Thanks
--
-MrDotNet MCAD, MCSD
Sr. Software Engineer / Lead Architect


Nov 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Justin Crossley | last post: by
2 posts views Thread by christopherkilmer | last post: by
2 posts views Thread by Jaime Stuardo | last post: by
2 posts views Thread by ojinfo | last post: by
5 posts views Thread by David++ | last post: by
3 posts views Thread by shapper | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.