Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 907
  • Last Modified:

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.


0
russomr
Asked:
russomr
  • 3
  • 2
  • 2
1 Solution
 
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now