473,799 Members | 2,665 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

QueryString Manipulation Using VB.NET

3 New Member
This function will give you 100% control over the QueryString -- perfect for programmers who use QueryString manipulation a lot.

I wrote this function when I kept having to change values of the QueryString and remove some, add some, etc., etc..

It takes four (4) parameters:

oldUrl = the current QueryString you would like manipulated
qsName = the name of the QueryField you would like to modify/remove/add
newValue = the new value you would like for the QueryField. set to "" if removing
del = an optional integer bit flag; if set to 1 it will remove the QueryField/QueryFieldValue from the QueryString.

Here is the code, enjoy! (I am always welcome to new suggestions)

Expand|Select|Wrap|Line Numbers
  1. Function ChangeValue(ByVal oldUrl As String, ByVal qsName As String, ByVal newValue As String, Optional ByVal del As Integer = 0) As String
  2.  
  3.     Dim newUrl As String = ""
  4.  
  5.     ' Check if the [qsName] is currently in the [oldUrl]
  6.     If InStr(oldUrl, qsName & "=")
  7.  
  8.         oldUrl += "&"
  9.  
  10.         Dim pos1 As Integer, pos2 As Integer
  11.  
  12.         If del = 1
  13.             pos1 = oldUrl.IndexOf(qsName & "=")
  14.             pos2 = oldUrl.IndexOf("&", pos1) + 1
  15.         Else
  16.             pos1 = oldUrl.IndexOf(qsName & "=") + qsName.Length + 1
  17.             pos2 = oldUrl.IndexOf("&", pos1)
  18.         End If
  19.  
  20.         Dim chunk_1 As String = oldUrl.SubString(0, pos1)
  21.         Dim chunk_2 As String = oldUrl.SubString(pos2)
  22.  
  23.         If del = 1
  24.             newUrl = chunk_1 & chunk_2
  25.         Else
  26.             newUrl = chunk_1 & newValue & chunk_2
  27.         End If
  28.  
  29.         newUrl = newUrl.SubString(0, (newUrl.Length - 1))
  30.  
  31.     Else
  32.  
  33.         If del = 1
  34.             Return oldUrl
  35.         End If
  36.  
  37.         ' Append the new value to the [oldUrl] and make it a [newUrl]
  38.         If oldUrl.EndsWith("?")
  39.             newUrl = oldUrl & qsName & "=" & newValue
  40.         ElseIf InStr(oldUrl, "?") AndAlso oldUrl.EndsWith("?") = False
  41.             If oldUrl.EndsWith("&")
  42.                 newUrl = oldUrl & qsName & "=" & newValue
  43.             Else
  44.                 newUrl = oldUrl & "&" & qsName & "=" & newValue
  45.             End If
  46.         Else
  47.             newUrl = oldUrl & "?" & qsName & "=" & newValue
  48.         End If
  49.  
  50.     End If
  51.  
  52.     Return newUrl
  53.  
  54. End Function
  55.  
Here is an example:
We have the querystring
Expand|Select|Wrap|Line Numbers
  1. http://www.yourdomain.com/search.aspx?fulltext=cat&author=bill+nye&type=scientist&age=45
Now I want to change the "fulltext" to "dog" and the "author" to "bob dole"
Also, I want to remove "type" from the QueryString.
Expand|Select|Wrap|Line Numbers
  1. Dim newUrl = Request.RawUrl.ToString()
  2. newUrl = ChangeValue(newUrl, "fulltext", "dog")
  3. newUrl = ChangeValue(newUrl, "author", "bob dole")
  4. newUrl = ChangeValue(newUrl, "type", "", 1)
  5.  
The QueryString would now look like this:
Expand|Select|Wrap|Line Numbers
  1. http://www.yourdomain.com/search.aspx?fulltext=dog&author=bob+dole&age=45
Yes! It's that easy!
Aug 23 '07 #1
0 10953

Sign in to post your reply or Sign up for a free account.

Similar topics

3
17291
by: Arpan | last post by:
A link has the following URL: <a href="Page1.asp?cname=<%= Request.QueryString("cname") %>&cadd1=<%= Request.QueryString("cadd1") %>&cadd2=<%= Request.QueryString("cadd2") %>&cplace=<%= Request.QueryString("cplace") %>">Click</a> Suppose the names in the above querystring have the following values: cname="Danny" cadd1="House 97"
1
6751
by: lion | last post by:
my Problem: a query string passed into a html page doesn't display correctly on a mac I am just using html and javascript (no ASP, PHP or server side scripting) This is the query string: popup.html?pImage=30_leilani_dowding_b_060.jpg&ordRef=1000&Title=Leilani&nbsp;Dowding using the QueryString function below, the pImage variable should be
12
5293
by: Alex | last post by:
I have a question about determining if one QueryString keys exists. The idea is, if this key exists, than its presence is enough to indicate that its value is true. For example ... www.something.com/main.aspx?client Client is the QueryString, but no value is given. This would mean to me that client exists, so its value is true. In the code behind, I can tell that client exists using Request.QueryString.Keys.Count, which equals 1....
7
2537
by: fox | last post by:
Hi, Lacking javascript knowledge, I just realized why my project has a bug. I am using ASP to loop through a set of records while it creates URLs with a querystring that has a single value pair. This URL needs to open in a floating window if clicked. (this is for an administrator and so opening a small floater gives them more efficient access to the data that will be displayed). I now understand that because the ASP executes first, that...
5
7681
by: Nirmal Singh | last post by:
I am a newbie trying to learn ASP.net 2.0. I want to retrieve the QueryString and process it to produce some parameters. I then want to redirect the user to another page, passing these parameters, but not as a querystring. Any help would be gratefully received.
0
2591
by: L'eau Prosper Research | last post by:
Press Release: L'eau Prosper Research (Website: http://www.leauprosper.com) releases new TradeStation 8 Add-on - L'eau Prosper Market Manipulation Profiling Tools Set. L'eau Prosper Market Manipulation Profiling Tools Set is a set of advanced tools that help Serious Traders analyze the market direction, market manipulative behavior and predicting the change of trend.
0
2354
by: L'eau Prosper Research | last post by:
NEW TradeStation 8 Add-on - L'eau Prosper Market Manipulation Profiling Tools Set By L'eau Prosper Research Press Release: L'eau Prosper Research (Website: http://www.leauprosper.com) releases new TradeStation 8 Add-on - L'eau Prosper Market Manipulation Profiling Tools Set. L'eau Prosper Market Manipulation Profiling Tools Set is a set of
3
1611
by: Nathan Sokalski | last post by:
I would like to be able to set the querystring for the URL used for a postback (the action attribute of the form). The Form property of the page does not give access to this property, and I do not want to set the entire value by using the Attributes property. Is there any simple property that can be used to add to the outgoing querystring? Thanks. Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/
11
3671
by: AnonG | last post by:
Hi all, I'm trying to create a simple navigation script. It allows you to select a region. Once region is selected another list opens up and you can select a city within that region. It all works fine, you can change cities freely and url updates correctly. BUT if you have a city selected and want to re-select the region, the city id stays in the url which messes up my page. Some examples: http://192.168.2.16/Seznam.php?IdR=1...
0
9689
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10269
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10248
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10032
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6811
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5597
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4148
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3764
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2942
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.