NameValueCollection / QueryString

M

mroffey

Ok here's my problem. I'm using screenscrape to get some values from a
webpage that only displays name/value pairs e.g.
UserExists=1&HasDOB=1&FirstName=Mark

I then want to get the individual values from these and split them up
and use in some stored procs. Is there a way I can pass these values
into a querystring object and then request them?

e.g.
NameValueCollection coll = Request.QueryString;

This puts all querystring paramaters into the object, but it doesn't
accept a string


e.g.
NameValueCollection coll = "UserExists=1&HasDOB=1&FirstName=Mark"

Any help would be great, at the minute I'm using a split to get the
individual values, and I can't believe that there's not a better more
efficient way of doing it.
 
M

mroffey

Ah If only it was that easy! No basically I have the equivalent of
querystring parameters, but actually just as a string. To make my self
clearer, imagine that I had just pulled those values out of a database
and they weren't appended to a URL, and now need to query the string as
a name/value pair.

What I guess I need to do is convert the string, into a URL object or
somerthing.
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

No really, you need to parse the string,

string[] pairs = theString.Split( new char[] { '&'} );
foreach(string pair in pairs )
{
string[] values = pair.Split( new char[] {'='} );
theCollection[ values[0] ] = values[1];
}


cheers,
 
G

Guest

I did something like this once.


void myFoo(string sNVPairs)
{

String[] sbNameValuePair;
String[] sbNameValuePairs;

//split into name-value pairs: "name=value"
sbNameValuePairs = sNVPairs.Split('&');

for (int i = 0; i < sbNameValuePairs.Length; i++)
{
//split the name value pair: var1=name, var2=value
sbNameValuePair = sbNameValuePairs.Split('=');
//decode the values and add the pair to a collection
m_colNVPairs.Add (System.Web.HttpUtility.UrlDecode(sbNameValuePair[0]),
System.Web.HttpUtility.UrlDecode(sbNameValuePair[1]));
}
}
 
M

mroffey

Cheers people. I've opted to stick with the split option. I just
thought there might be another way of passing a string directly into
the object.

The split works just as well. I guess I was just trying to be super
efficient. Oh well there's a first time for everything!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top