I am refactoring some legacy code. The app was not using querystrings. The previous developer was hard coding some variables that the app uses in other places.
Like this using VB.NET
so.Cpage = "ContractChange.aspx"
My question is can I programatically set this value and include the current querystring?
I want so.Cpage to be something like ContractChange.aspx?d=1&b=2
Can I do this with the request object or something? Note, I don't need the domain.
To get the current query string you would simply do something like the following:
Dim query as String = Request.QueryString("d")
This will assign the value of the "d" querystring to the string variable "query". Note that all query string values are strings, so if you're passing numbers around, you'll need to "cast" or convert those string values to numerics (be careful of exceptions when casting, though). For example:
Dim query as String = Request.QueryString("d")
Dim iquery as Integer = CType(query, Integer)
The QueryString property of the Request object is a collection of name/value key pairs. Specifically, it's of type System.Collections.Specialized.NameValueCollection, and you can iterate through each of the name/value pairs as so:
Dim coll As System.Collections.Specialized.NameValueCollection = Request.QueryString
Dim value As String
For Each key As String In coll.AllKeys
value = coll(key)
Next
Using either of these mechanisms (or something very similar) should enable you to construct a string variable which contains the full url (page and querystrings) that you wish to navigate to.
Try this:
so.Cpage = "ContractChange.aspx?" & Request.RawUrl.Split("?")(1)
In VB.Net you can do it with the following.
Dim id As String = Request.Params("RequestId")
If you want to process this in as an integer, you can do the following:
Dim id As Integer
If Integer.TryParse(Request.Params("RequestId"), id) Then
DoProcessingStuff()
End If
try this
Dim name As String = System.IO.Path.GetFileName(Request.ServerVariables("SCRIPT_NAME"))
Dim qrystring As String = Request.ServerVariables("QUERY_STRING")
Dim fullname As String = name & "/" & qrystring
Not sure about the syntax in VB.NET but in C# you would just need to do
StringId = Request.QueryString.Get("d");
Hope this helps.
Related
I have a string in VB:
url = "http://example.com/aa/bb/cc.html"
I want to trim this url to the last sub-folder so it becomes:
url = "http://example.com/aa/bb"
I need everything after the last "/" to be removed.
I am thinking of using the string.lastindexof("/") method but don't know how to continue from there.
use a combination of Substring and Lastindex of. Like this:
url.substring(0,url.lastindexof("/"))
might be that you need to substract 1 from the lastindexof("/") value, i always forget it^^
When working with an URL, consider using the Uri class. Then handling such cases become easy.
Create a Uri instance:
Dim url = new Uri("http://example.com/aa/bb/cc.html")
Then you can either do
Dim result = url.AbsoluteUri.Remove(url.AbsoluteUri.Length - url.Segments.Last().Length)
or something like
Dim result = new Uri(url, ".").AbsoluteUri
You could use String.Remove() to remove the unwanted part of the string:
Dim temp As String = "http://example.com/aa/bb/cc.html"
Dim index As String = temp.LastIndexOf("/"c)
Dim ret As String = temp.Remove(index, temp.Length - index)
how could I insert a variable into the middle of a URL using vb.net?
e.g.
Dim ver As String = "variable"
http://testurl.co.uk/folder/next_folder/" & ver & "/test.exe
(not sure if this is correct above)
Note: I know how to set a variable but not sure how to insert into the middle of a URL
You could do that (as #SysDragon has pointed out, you need to store the result in a variable). Another alternative would be:
Dim ver As String = "variable"
Dim url As String = String.format("http://testurl.co.uk/folder/next_folder/{1}/test.exe", ver)
It probably doesn't matter for something as trivial as this, but strings are immutable, so doing:
Dim myString as String = "a" & "b" & "c"
effectively destroys and recreates the string twice. StringBuilder (which I believe string.format uses internally) prevents this.
Almost, I think you want this:
Dim ver As String = "variable"
Dim url As String = "http://testurl.co.uk/folder/next_folder/" & ver & "/test.exe"
To browse the url in the form do something like this:
Dim wb1 As New Net.WebBrowser()
Form1.Controls.Add(wb1)
wb1.Navigate(url)
This is probably a dumb question but I have to call one of 30 or so global variables by constructing the variable name to be called from another information. However when i do that it treats it as a string. Any ideas on how to make this work?
eg something like this:
Public gsNewYork As String
public sub Getinfo
dim lslocation as string
dim a as string = "New"
dim b as string = "York"
lslocation = "gs" + a + b
lblLabel.text = lslocation
Any ideas on how to construct the variable name and have it identified as the variable name?
The most common solution to this problem is to store your variables in a Dictionary, where the strings you build are the key.
Here's the deal, it's a log in form and I need to put case-sensitive validation, in other words if your Username = Admin then Admin != admin
Consider this code block (VB is really unfamiliar for me so break it to me gently ^^)
This is after it has matched a record in the database to the parameter passed to the function LogIn()
If dataTable.Rows.Count > 0 Then
'case-sensitive Validation follows
'data in Column ID and Password are placed in variables
'which are then compared to the arguments sent using the Compare() function
Dim strID = dataTable.Columns("U_ID").
Dim strPass = dataTable.Columns("Password")
Dim idResult As Integer 'both results will hold the value of String.Compare()
Dim passwordResult As Integer
*idResult = String.Compare(strID, ID)
the asterisk-ed line returns an error (obviously) as strId is not of data type String.
That's my dilemma.
I also tried using LIKE but again since strId and strPass are not Strings, all I get is an error.
Change this line:
Dim strID = dataTable.Columns("U_ID")
to this:
Dim strID as String = dataTable.Columns("U_ID").ToString
I am using VB.NET code.
I have got the below string.
http://localhost:3282/ISS/Training/SearchTrainerData.aspx
Now I want to split the above string with "/" as well. I want to store the value SearchTrainerData.aspx in a variable.
In my case
Dim str as String
str = "SearchTrainerData.aspx"
What would be the code which will split the above string and further store it in a variable?
Try to use the function String.Split.
Your "string" is obviously a URL which means you should use the System.Uri class.
Dim url As Uri = New Uri("http://localhost:3282/ISS/Training/SearchTrainerData.aspx")
Dim segments As String() = url.Segments
Dim str As String = segments(segments.Length - 1)
This will also allow you to get all kinds of other interesting information about your "string" without resorting to manual (and error-prone) parsing.
The Split function takes characters, which VB.NET represents by appending a 'c' to the end of a one-character string:
Dim sentence = "http://localhost:3282/ISS/Training/SearchTrainerData.aspx"
Dim words = sentence.Split("/"c)
Dim lastWord = words(words.length - 1)
I guess what you are actually looking for is the System.Uri Class. Which makes all string splitting that you are looking for obsolete.
MSDN Documentation for System.Uri
Uri url = new Uri ("http://...");
String[] parts = url.Segments;
Use split(). You call it on the string instance, passing in a char array of the delimiters, and it returns to you an array of strings. Grab the last element to get your "SearchTrainerData.aspx."