Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to convert string to NameValueCollection?

Posted on 2008-10-24
4
Medium Priority
?
1,443 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 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Screencast - Getting to Know the Pipeline
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

926 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