Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

How to convert string to NameValueCollection?

Posted on 2008-10-24
4
1,255 Views
Last Modified: 2012-05-05
Hi, I have a string with querystring format(x=3&z=2&o=9) I want to convert to NameValueCollection to process it, how can I do that?
0
Comment
Question by:Abdu_Allah
  • 2
4 Comments
 
LVL 3

Expert Comment

by:TechSinger
ID: 22795983
The appears to be the Query String that you are sending to another page as part of the URL.  ASP .NET is able to process this string very easily using Request.QueryString
Let's use the example you inclued ("x=3&z=2&o=9") as the query string, so the URL would look something like
 http://myserver.mycompany.com/mypage.aspx?x=3&z=2&o=9
I have attached the code that would be used to process this query string.  This would most likely be done during the Page's Load event.
The code I have attached is in Visual Basic.

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    myTextBoxx.Text=Request.QueryString("x")
    myTextBoxz.Text=Request.QueryString("z")
    myTextBoxo.Text=Request.QueryString("o")
EndSub

Open in new window

0
 
LVL 3

Author Comment

by:Abdu_Allah
ID: 22796069
No I'm not sending a querystring.
I suggest everyone before responding to any question to read the auther's Knowledge level that provided with the question.
As you can see my knowledge level is intermediate, that means any intermediate ASP.NET developer should know how to process querystring.
0
 
LVL 20

Accepted Solution

by:
informaniac earned 500 total points
ID: 22797070
string str = "x=3&z=2&o=9";

string []str1 = str.Split("&");

NameValueCollection nvc = new NameValueCollection();

foreach(STring str2 in str1)
{
        string[] st = str2.split("=");
         nvc.add(st[0],st[1]);
}
0
 
LVL 3

Expert Comment

by:TechSinger
ID: 22797223
Sorry for the misunderstanding.  If I would have read your questiong a litt more thuroughly, I would probably have realized that this was not an actual URL query string.
I have attache a function that would treat this string as you mentioned as the Request.QueryString method would the URL querystring in a web page.  This function accepts the raw data as the first paramater and the variable desired as the second variable.
The function uses the String.Split method twice to parse out the & and the =.
I hope this helps.

Function myQueryString(ByVal qString As String, ByVal qVar As String) As String
        Dim i As Integer
        Dim ret As String = ""
        Dim sTemp() As String = qString.Split("&")
        For i = 0 To sTemp.Length - 1
            Dim varString() As String = sTemp(i).Split("=", 2, StringSplitOptions.None)
            If varString(0) = qVar Then
                ret = varString(1)
                Exit For
            End If
        Next
        Return ret
    End Function

Open in new window

0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question