We help IT Professionals succeed at work.

Request.QueryString (sort of)

David C
David C asked
on
Hi Experts,

Is there a way to get the value of a variable given a label with text "rd=5&key=stock&c0=False" to use the same functionality as Request.QueryString("rd") but using the label instead?

Thanks
Comment
Watch Question

Developer
Top Expert 2015
Commented:
Hi,

To get all values

string data = "rd=5&key=stock&c0=False";
                 string Key;
                string value;
            foreach (string val in data.Split('&'))
            {
                string[] values = val.Split('=');
                

                if (values[0] != null)
                    Key = values[0]; // rd, Key, c0

                if (values[1] != null)
                    value = values[1]; // 5, stock, False

            }

Open in new window


To get only rd values

string data = "rd=5&key=stock&c0=False";
            string Key;
            string value;
           
            foreach (string val in data.Split('&'))
            {
                if(val.StartsWith("rd"))
                {
                    string[] values = val.Split('=');
                    if (values[0] != null)
                        Key = values[0];

                    if (values[1] != null)
                        value = values[1];
                }
   }

Open in new window

David CIT Projects Manager

Author

Commented:
Thanks very much, this is doing the job but I just took a snippet of the string. The actual string is much longer i.e. "a=Tom&b=Casey&rd=5&key=stock&c0=False&c1=False" so your code is only getting the first key and value
Lokesh B RDeveloper
Top Expert 2015

Commented:
Hi,

In ForEach loop you get all the values.

string data = "a=Tom&b=Casey&rd=5&key=stock&c0=False&c1=False";
            string Key;
            string value;

            foreach (string val in data.Split('&'))
            {
                string[] values = val.Split('=');

                if (values[0] != null)
                    Key = values[0];

                if (values[1] != null)
                    value = values[1];

            }

Open in new window

David CIT Projects Manager

Author

Commented:
Excellent!