Retrieving and integer querystring value

I am using ASP.Net 1.1 C#.  I have the following line at the top of my code before my functions:

public int recID = Request.QueryString["recid"];

I am getting this error on the above line:

CS0120: An object reference is required for the nonstatic field, method, or property 'System.Web.UI.UserControl.Request.get'

What's going on here?  I'm trying all kinds of different things but can't seem to populate an integer variable with the querystring value.

I've also tried:
public int recID = (int) Request.QueryString["recid"];
static int recID = Request.QueryString["recid"];
public static int recID = Request.QueryString["recid"];

help.


russomrAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

apb2Commented:
How about:

int recID = (int) Request.QueryString["recid"];

why do you want to give it a public declaration?

If you want to be able to access it publicly i would wite a public property to do so, then you also be following basic oop principles

apb2
0
apb2Commented:
forgive my c# (if it's wrong)

int recID = (int) Request.QueryString["recid"];

public int RECID
{
  get
   {
      return recID;
   }
}

hth apb2
0
naveenkohliCommented:
public int recID = (Int32)HttpContext.Current.Request.QueryString["recid"];

But this kind of implementation can get you in trouble. You are assuming that recid QS parametter will always be present in URL. If for some reason you don't have it, this line of code will throw exception.

Move initialization of this variable in Page_Load on OnInit event handler.
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

russomrAuthor Commented:
Nothing seems to be working.  I agree, I should (and did) move it to within the Page_Load event, however I'm getting:

Cannot implicitly convert type 'string' to 'int'


Right now, the path I'm taking is:

int recID = (Int32)HttpContext.Current.Request.QueryString["recid"];

And I am passing a number into the page via querystring.
0
naveenkohliCommented:
My fault.. forgot QS come as string

int recID = Int32.Parse(HttpContext.Current.Request.QueryString["recid"]);
0
russomrAuthor Commented:
That seems to work, however you mentioned earlier that it will throw and exception if no parameter is passed, which it does.

In my code, I want to execute a function if the variable recID contains a value, otherwise I want to skip execution of that function.  What is the most efficient way to do this?

I want to do something like this:

// IF RECID VALUE IS PASSED TO PAGE, DISPLAY DEFAULT DATA
if(recID.ToString() != "") {
      if(!IsPostBack) {
            PopulateForm(recID);
      }
}

But getting that exception obviously causes problems.

I've upped the points considering this is a somewhat different question.
0
naveenkohliCommented:
if (HttpContext.Current.Request.QueryString["recid"] != null)
{
   int recID = Int32.Parse(HttpContext.Current.Request.QueryString["recid"]);
  // Do the things that you want to for valid QS
}
else
{
  // Skip or whatever you want to do if QS is not there.
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.