Solved

How to convert string to NameValueCollection?

Posted on 2008-10-24
4
1,226 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

895 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now