JackofAll
JackofAll

Reputation: 537

Display page when no querystring is present

I have a page which requires me to have a gameweek id on page load to run specific methods.

This is parsed via the query string, and retrieved with the following:

gameweekID = Request.QueryString["gameweekID"];

For example:

page1.aspx?gameweekID=1

My issue is however on occasions where the url is page1.aspx, where no querystring is provided.

How can I default the page to gameweekID = 1, instead of getting the error

Procedure or function 'GetPredictions' expects parameter '@gameweekID', which was not supplied.

Upvotes: 0

Views: 446

Answers (2)

iefpw
iefpw

Reputation: 7062

if(Request.Querystring["gameweekID"] === null) Response.Redirect("page.aspx?!gameweekID=1")

Upvotes: 0

p.s.w.g
p.s.w.g

Reputation: 149058

You can do this:

gameweekID = Request.QueryString["gameweekID"] ?? "1";

Which means that if Request.QueryString["gameweekID"] is null use "1" instead, but this will still produce errors if you request page1.aspx?gameweekID= or page1.aspx?gameweekID=foo.

Perhaps a more elegant way would be to validate that gameweekID is actually an integer. Like this:

string gameweekIDString = Request.QueryString["gameweekID"];
int gameweekID;
if (!int.TryParse(gameweekIDString, out gameweekID))
{
    gameweekID = 1;
}

Upvotes: 3

Related Questions