Solved

How to convert string to NameValueCollection?

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

815 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

11 Experts available now in Live!

Get 1:1 Help Now