Link to home
Start Free TrialLog in
Avatar of JeffDrummond
JeffDrummond

asked on

Validation of a query string parameter.....

In my ASP.Net C# application, in the Page_Load event, I am trying to validate a query string parameter with this code:

if (Int32.TryParse(basicToolKey.ToString(), out result) = true && Convert.ToInt32(basicToolKey) > 0)

I get the error message with the squiggly lines under the first Int32:

The left-hand side of an assignment must be a variable, property or indexer.

I also tried this and it worked:

if (Request.Params.Get("Td_bt_key") != null)

Please advise on the best practice method to confirm that the parameter is not null and is an integer greater than 0.

Thanks!
Avatar of JeffDrummond
JeffDrummond

ASKER

Sorry, the original code s not the right example for y question.  This is the correct example:


if (Int32.TryParse(Request.Params["Td_bt_key"],out result) = true && Convert.ToInt32(Request.Params["Td_bt_key"]) > 0)

I get the error message with the squiggly lines under the first Int32:

The left-hand side of an assignment must be a variable, property or indexer.

I also tried this and it worked:

if (Request.Params.Get("Td_bt_key") != null)

Please advise on the best practice method to confirm that the parameter is not null and is an integer greater than 0.

Thanks!
ASKER CERTIFIED SOLUTION
Avatar of LordOfPorts
LordOfPorts
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Actually, there's no need for the Convert. TryParse does that for you. The most efficient way is:
int result = 0;
if (Int32.TryParse(Request.Params["Td_bt_key"],out result))
{
   // Then the value is a valid int, and the value is in the result variable.
}
else
{
   // Request.Params["Td_bt_key"] does not contain a valid integer.
}

Jim
SOLUTION
Avatar of Daniel Reynolds
Daniel Reynolds
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
TSmooth - I left the question open too long and duplicated your answer. You got my point across.

JeffDrummond - Please disregard my response.

Thanks,

Jim
This works for me.  Thanks to all involved.

        if (Request.Params.Get("Td_bt_key") != null)
        {
            int result;
            if (Int32.TryParse(Request.Params["Td_bt_key"], out result) && result > 0)
            {

// Do something.
               


            }

        }
        else
        {
            // TODO:  Failure code

        }