Solved

How to convert string to NameValueCollection?

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

771 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

12 Experts available now in Live!

Get 1:1 Help Now