NullReferenceException when passing parameter value by method get from JavaScript function

0

I need to pass a value by method get . On the start page I have

asked by Efrain Mejias C 14.09.2016 в 16:28
source

2 answers

1

I think the problem of the null reference is given in this command: Session["idrestaurant"].ToString() because as I see in code you have not yet defined this session variable.

In any case, try to capture the GET parameters in this way:

    string miVariableGet = "";

    if (Request.QueryString["id"] == null){
       //La variable 'id' es nula
    }else{
        //Se logro capturar el parametro y lo puedes utilizar. Por ejemplo
        miVariableGet = Request.QueryString["id"];   
    }
    
answered by 14.09.2016 / 18:24
source
1

You should always expect it to be null, since they might not send you anything in the query string or there may be nothing in the session (lost session).

<%
        int idrestaurant = 0;
        var rawValue = "";

        if (Request.QueryString?.Keys.Count == 0)
        {//AQUI EL ERROR
            rawValue = Session["idrestaurant"];                  
        }
        else
        {            
            rawValue = Request.QueryString["id"];        
        }

        if(rawValue !=null){
                 int.TryParse(rawValue.ToString(), out idrestaurant);
        }                    

        Session["idrestaurant"] = idrestaurant;
         %>
    
answered by 14.09.2016 в 16:36