Empty QueryString parameter - asp.net

The Problem
What is the proper way to check for the foo parameter in the following url's querystring using asp.net? Is this even possible?
http://example.com?bar=3&foo
I have tried checking Request["foo"] as well as Request.QueryString["foo"] and I get null for both. I have also tried populating a List with the values from the QueryString collection but as I mention below, it does not include the value.
The Question
I understand that there is no value, but shouldn't Request["foo"] return an empty string rather than null? Is there a way to find out if a querystring key exists even if it has no value?
Notes
I found here that Request.QueryString.AllKeys includes null for blank querystring parameters.
[edit]
As stated below by James and Dreas a Regex to parse the raw url might be the best (and possibly only) approach.
Regex.IsMatch(Request.RawUrl, "[?&]thumb([&=]|$)")

You can use null as the key for the NameValueCollection and it will give you a comma-delimited list of parameter names that don't have values.
For http://example.com?bar=3&foo you would use Request.QueryString[null] and it would retrieve foo.
If you have more than one parameter name without a value, it will give you a value that is comma-delimited.
For http://example.com?bar=3&foo&test you would get foo,test as a value back.
Update:
You can actually use Request.QueryString.GetValues(null) to get the parameter names that don't have values.

Request.ServerVariables["QUERY_STRING"] will return the query string, complete, as a string. Then search it using a Regex or IndexOf

You get null because the foo parameter doesn't have a value with it.
...What's the problem exactly?
If you still want to check for its existence (although it lacks a value), try something like this:
bool doesFooExist = Request.Url.AbsoluteUri.IndexOf("foo=") >= 0 ? true : false;

Dreas is correct. Variable "bar" has a value but foo does not.

QueryString["Bar"] will return 3 because it has the value 3 associated with the variable Bar. However, Foo will return null because their is no value and when you are calling QueryString on a variable or key, you are querying for the value, not the key, so that is why you are returning null.

query string is probably a waste one. If you use Request.Params[""] or iterate it, then you will find your desired one. Its really handy than other things.
Let me know if you need any help in this.

Related

PeopleCode reflection for variables

Does PeopleCode have something similar to reflection in C# whereby I can access a variable with the variable name stored in a string? I am aware of the # operator but I think it only applies to fields and records (correct me if I am wrong. I tried and couldn't get it to work.)
Basically, I need to access a Component variable by name with the name itself being variable.
You can do it by # :
Local string &sValue1 = "var1";
getting the value :
winmessage(#(&sValue1));

Difference between Session("foo") is "test" and Cstr(Session("foo") = "test"

I want to know the difference between the following.
Difference between Session("foo") is "test" and Cstr(Session("foo")) = "test"
I understand the second one is casting to string. The thing I don't get is, one one page, when I tried,
if Session("foo") is "test" Then
Do something
This worked on one page. On some pages the do something does not get executed even if Session("foo") is "test". On the other hand Cstr(Session("foo")) = "test" always works. Why is the difference. Out of the two, which suits better for convention?
The Is operator determines if two object references refer to the same object. However, it does not perform value comparisons. If object1 and object2 both refer to the exact same object instance, result is True; if they do not, result is False. Ref
So in your case you need to convert the object of the Session to string before you want to compare it with another string value type.
To Answer to your next question
You can set a property in your page instead of repeating that code several times on your page like below:
Public Property MyValue() As String
Get
Return Session("MyValue").ToString()
End Get
Set
Session("MyValue") = value
End Set
End Property
Now you can Set and Get this value anywhere inside your page, like this:
MyValue = "This is my value" 'set a value
Dim message As String = MyValue 'get a value
The return type of Session is Object.
The Is operator compares the object references of two objects.
The = operator compares the values stored in two objects.
Since you want to check to see if the session has a particular name, you want to compare the values of two strings, you need to use the = operator.
When you use the Is operator to compare object references, it checks to see of the two references are pointing to the same string instance. But, Is will evaluate to false if you compare two separate object instances, even if both instances contain the same string value.

Is passing a query string parameter (whose value is null) without the equals sign valid?

Variation 1:
file.jsp?parameter1=&parameter2=abc
Variation 2:
file.jsp?parameter1&parameter2=abc
I know Variation 1 is considered valid, but is variation 2 considered valid? Specifically, there is no value for the parameter, is the equals sign required in this case?
I think it is valid to have query parameter with just the parameter name.
If you are familiar with Java, Look at the #see UriComponentsBuilder QueryParam function, by default it does this for any query parameter with null.
If no values are given, the resulting URI will contain the query
parameter name only (i.e. ?foo instead of ?foo=bar.
See this answer for more info.
Is a url query parameter valid if it has no value?

problem in DataTextField with non simple return value

I know that the syntax for using DataTextField is:
VA.DataTextField = "nameofcolum"
but if my request is not simple that means my request returns the result in a column which not exist in this case. What I should to affect to DataTextField?
It's hard to tell what you're looking for here without more information.
If your result set does not contain the column you need, change your result set - alter your query. If you need your datatextfield as a combination of columns, you can iterate through the result set and build the text string based on the column values and assign it as the display value.

How can I update a database string value from a TextBox?

I cant update my database through textbox.in the case of numeric value i can update but cant with a string value.
my coding is...
SqlCommand cmdup= new SqlCommand("UPDATE [port1] SET [prt1]=#prt1 WHERE [no]= 1",cn);
cmdup.Parameters.Add("#prt1", TextBox1.Text);
cmdup.ExecuteNonQuery();
if any one know the ans: reply me
I'd recommend explicitly defining the parameter type to match the type in the DB, and passing the value to Parameters.Add as the appropriate type.
e.g. at the moment, you are passing a string typed value to Parameters.Add, and not defining the type explicitly. Therefore, it will assume the data type from the type of value supplied...so the #prt1 type will be passed in to the DB as NVARCHAR I believe.
If the prt1 field is an INTEGER for example, much safer IMHO to do something like:
cmd.Parameters.Add("#prt", SqlDbType.Int,4).Value = Int32.Parse(TextBox1.Text);
or
cmd.Parameters.AddWithValue("#prt", Int32.Parse(TextBox1.Text));
I always like to fully define the type of the parameters I pass in to SQL to rule out any potential, unexpected issues.
Check the datatype for the #prt1 to see if it is a Int value because you are setting to the same column two different values.
Parse the textBox to Integer as you want to insert a numeric value i.e. Int64.Parse("Textbox1.Text"); well with string it does have problems.
Just a wild guess: Does prt maybe only accept numeric values?

Resources