473,840 Members | 1,538 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Querystring validation

My apologies in advance if there is a better forum for this question.

Given the following snippet:

<script type="text/c#" runat="server">
protected void Page_Load(Objec t Sender, EventArgs e)
{
if ( Request.QuerySt ring.Count 0 )
{
if ( Request.QuerySt ring[ "pid" ] != "" )
{

My problem is as follows,

a) if url is passed with no querystring at all, works as intended
b) if url is passed with querystring "?pid=" (blank), works as intended
c) if url is passed with querystring "?pid=12345 ", works as intended
d) if url is passed with querystring with misspelled or missing "pid"
element, the script crashes because the [ if ( Request.QuerySt ring[ "pid" ]
!= "" ) ] returns true.

How do I test to ensure that an element ("pid") is present in the
querystring before continuing?

Jan 18 '08 #1
4 5967
"EW Newbie" <EW******@discu ssions.microsof t.comwrote in message
news:63******** *************** ***********@mic rosoft.com...
How do I test to ensure that an element ("pid") is present in the
querystring before continuing?
protected void Page_Load(Objec t Sender, EventArgs e)
{
if (Request.QueryS tring["pid"] != null)
{
// do something
}
}
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jan 18 '08 #2
On Jan 18, 3:54*pm, EW Newbie <EWNew...@discu ssions.microsof t.com>
wrote:
My apologies in advance if there is a better forum for this question.

Given the following snippet:

* *<script type="text/c#" runat="server">
* *protected void Page_Load(Objec t Sender, EventArgs e)
* *{
* * * if ( Request.QuerySt ring.Count 0 )
* * * * {
* * * * * *if ( Request.QuerySt ring[ "pid" ] != "" )
* * * * * * *{

My problem is as follows,

a) if url is passed with no querystring at all, works as intended
b) if url is passed with querystring "?pid=" (blank), works as intended
c) if url is passed with querystring "?pid=12345 ", works as intended
d) if url is passed with querystring with misspelled or missing "pid"
element, the script crashes because the [ if ( Request.QuerySt ring[ "pid" ]
!= "" ) ] returns true.

How do I test to ensure that an element ("pid") is present in the
querystring before continuing?
if ( Request.QuerySt ring != null && Request.QuerySt ring["pid"] != "" )
{
...
}
Jan 18 '08 #3
if (!String.IsNull OrEmpty(Request .QueryString["pid"])
{
}

Regards
--
Milosz
"EW Newbie" wrote:
My apologies in advance if there is a better forum for this question.

Given the following snippet:

<script type="text/c#" runat="server">
protected void Page_Load(Objec t Sender, EventArgs e)
{
if ( Request.QuerySt ring.Count 0 )
{
if ( Request.QuerySt ring[ "pid" ] != "" )
{

My problem is as follows,

a) if url is passed with no querystring at all, works as intended
b) if url is passed with querystring "?pid=" (blank), works as intended
c) if url is passed with querystring "?pid=12345 ", works as intended
d) if url is passed with querystring with misspelled or missing "pid"
element, the script crashes because the [ if ( Request.QuerySt ring[ "pid" ]
!= "" ) ] returns true.

How do I test to ensure that an element ("pid") is present in the
querystring before continuing?


Jan 18 '08 #4
There's even better way of handling optional query string parameters which i
prefer ( i assume pid's type is integer):

protected int? Pid
{
get
{
int value;
return int.TryParse(Re quest.QueryStri ng["pid"], out value) ?
(int?)value : null;
}
}

and then you can easly use it everywhere in the code:

if (Pid.HasValue)
{
// do something here
}

or if the value is optional but should be initialized to a default value:

private int? pid;
protected int Pid
{
get
{
if (!this.pid.HasV alue)
{
int value;
this.pid = int.TryParse(Re quest.QueryStri ng["pid"], out value) ?
value : 0 // replace it with default value ;
}
return this.pid.Value;
}
}

I know it mihgt look horrible but if you parse the same parameter
many times on the same page it should help you clean up the code a little bit.

Regards
--
Milosz
"Milosz Skalecki [MCAD]" wrote:
if (!String.IsNull OrEmpty(Request .QueryString["pid"])
{
}

Regards
--
Milosz
"EW Newbie" wrote:
My apologies in advance if there is a better forum for this question.

Given the following snippet:

<script type="text/c#" runat="server">
protected void Page_Load(Objec t Sender, EventArgs e)
{
if ( Request.QuerySt ring.Count 0 )
{
if ( Request.QuerySt ring[ "pid" ] != "" )
{

My problem is as follows,

a) if url is passed with no querystring at all, works as intended
b) if url is passed with querystring "?pid=" (blank), works as intended
c) if url is passed with querystring "?pid=12345 ", works as intended
d) if url is passed with querystring with misspelled or missing "pid"
element, the script crashes because the [ if ( Request.QuerySt ring[ "pid" ]
!= "" ) ] returns true.

How do I test to ensure that an element ("pid") is present in the
querystring before continuing?

Jan 18 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
2998
by: Robb Meade | last post by:
Hi all, A recent project that I had finished and went live with no apparant problems. My client received an email from a user who mentioned that by accident they had been typing (over the querystring I guess), and the url had become default.asp?pageid='asd
4
3036
by: Luklrc | last post by:
Hi, I'm having to create a querysting with javascript. My problem is that javscript turns the "&" characher into "&amp;" when it gets used as a querystring in the url EG: /mypage.asp?value1=1&amp;value2=4&amp; ... which of course means nothing to asp.
12
5297
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....
0
1060
by: Jerry O | last post by:
Hi, I have a forms authenticated application that appears to have a problem when redirecting users whose session has timed out. The web form is posted via a ‘get' When they click a submit button after their session has timed out they get the error at the end of the post. It appears to be a problem with the url that has been sent which is as follows:
5
11096
by: =?Utf-8?B?QWxoYW1icmEgRWlkb3MgS2lxdWVuZXQ=?= | last post by:
Hello to all, I have a page ASPX that calls to another one. It does window.open to another URL, and I want to hide the URL because the user not must see the querystring by security subjects. I discard properties of window.open (javascript) because the url can be displayed by contextual menu of IE - Option Properties.
4
2974
by: Javilen | last post by:
Hello, I am developing a forums for asp.net, in doing this I am using code behind to do all validation on users and roles and then using embedded ASP to do the displaying for categories, sections and threads! The problem I am having is, when you first navigate to the site you pass the forum name in a querystring. So foe example: forumid=testforum I do this because the site will host multiple forums and this will allow me to use one DB...
5
3590
by: magix8 | last post by:
Hi, I have form GET method, example: index.asp?Type=1&Type=3&Type=4&.... So, I have something like this at the receiver side to retrieve multiple Type value and insert into tables.
3
2163
by: pingsheng | last post by:
Dear all, I have a form with dynamically created input fields. These fields go to next page for submitting into SQL database. The thing is all fields are the same but 4 fields. So each record must be submitted with a unique key, while everything is the same, these 4 fields must be different.... Idealy, these are the codes. please teach me how to do it. objConn.Open(sConnection) sRowNum = request.querystring("row_num")
4
1865
by: .nLL | last post by:
Hi, im am a classic asp developer and started to learn asp.net but got stuck with a simple problem even before i step in to further. to learn i have started from a simple project (a login system with forms) due to projects platform (a mobile web site) i cant use cookies (cookies arent supported on all phones), anyway because of that i do un/pw check on very page and i have to put Dim MyUn As String =...
0
9861
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
9699
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10924
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10665
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
10301
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...
1
7839
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7023
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();...
1
4498
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
4078
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.