6 Replies - 9931 Views - Last Post: 31 August 2011 - 02:29 PM Rate Topic: -----

#1 drilli  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 105
  • Joined: 26-May 09

I am losing Session variables after Response.Redirect(url)

Posted 29 August 2011 - 04:16 PM

I am having a problem with redirecitng while using session variables.

In my Produktet.aspx page is this code:

protected void Page_Load(object sender, EventArgs e)
        {
            
            if (!IsPostBack)
            {
                
                IDKat = int.Parse(Request.QueryString["ID_Kategoria"]);
                Session["ID_Kateg"] = IDKat;
           }
        }



From this page i go to DetajetEProduktit.aspx page and after doing something there and clicking a linkButton (in button_click event i try to redirect to Produktet.aspx page but than my sesion variable is lost).

here is the button click event:

 protected void lnkbKtheu_Click(object sender, EventArgs e)
        {
            string url = "~/Produktet.aspx?ID_Kategoria = " + Session["ID_Kateg"] ;
            Response.Redirect(url);
        }




I tried this : Respnse.Redirect(url,false) but it doesn`t work.

Does anyone know the solution?

Is This A Good Question/Topic? 0
  • +

Replies To: I am losing Session variables after Response.Redirect(url)

#2 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 444
  • View blog
  • Posts: 1,492
  • Joined: 28-April 09

Re: I am losing Session variables after Response.Redirect(url)

Posted 30 August 2011 - 05:31 AM

Session returns an object, you may need to convert it to a string or back to its original type which was an integer.
Was This Post Helpful? 0
  • +
  • -

#3 drilli  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 105
  • Joined: 26-May 09

Re: I am losing Session variables after Response.Redirect(url)

Posted 30 August 2011 - 06:58 AM

I tried this also but it`s the same problem again

protected void lnkbKtheu_Click(object sender, EventArgs e)
        {
            string url = "~/Produktet.aspx?ID_Kategoria = " + int.Parse( HttpContext.Current.Session["ID_Kateg"].ToString()) ;
            Response.Redirect(url,false);
        }


Was This Post Helpful? 0
  • +
  • -

#4 keakTheGEEK  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 107
  • View blog
  • Posts: 344
  • Joined: 23-February 10

Re: I am losing Session variables after Response.Redirect(url)

Posted 30 August 2011 - 04:32 PM

That's not going to work either because you are concatenating your url string to an int (int.parse returns an int).

Try using the Convert.ToString() function:
protected void lnkbKtheu_Click(object sender, EventArgs e)
{
    string url = "~/Produktet.aspx?ID_Kategoria = " + Convert.ToString(Session["ID_Kateg"]);
    Response.Redirect(url,false);
}



You could also use a Nullable int in this scenario as well:
protected void lnkbKtheu_Click(object sender, EventArgs e)
{
    int? ID_Kateg = Session["ID_Kateg"] as int?;
    if (ID_Kateg.HasValue) 
    {
        string url = "~/Produktet.aspx?ID_Kategoria = " + ID_Kateg.ToString();   
        Response.Redirect(url,false);
    }
    else 
    {
        //Do something else
    }
}


Was This Post Helpful? 0
  • +
  • -

#5 drilli  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 105
  • Joined: 26-May 09

Re: I am losing Session variables after Response.Redirect(url)

Posted 31 August 2011 - 03:48 AM

I tried both of them but still the same problem...there is a null value in session after redirecting.I don`t know what to do.
Was This Post Helpful? 0
  • +
  • -

#6 drilli  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 105
  • Joined: 26-May 09

Re: I am losing Session variables after Response.Redirect(url)

Posted 31 August 2011 - 04:18 AM

and here is the Error :


Value cannot be null.
Parameter name: String

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentNullException: Value cannot be null.
Parameter name: String

Source Error:


Line 32:
Line 33: PopulateMenu();
Line 34: IDKat = int.Parse(Request.QueryString["ID_Kategoria"]);
Line 35: Session["ID_Kateg"] = IDKat;
Line 36: //int.Parse(Request.QueryString["ID_Kategoria"]);

Source File: C:\Documents and Settings\Refresh\My Documents\Visual Studio 2008\Projects\PorositOnline\PorositOnline\Produktet.aspx.cs Line: 34

Stack Trace:


[ArgumentNullException: Value cannot be null.
Parameter name: String]
System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal) +7467290
System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info) +119
System.Int32.Parse(String s) +23
PorositOnline.Produktet.Page_Load(Object sender, EventArgs e) in C:\Documents and Settings\Refresh\My Documents\Visual Studio 2008\Projects\PorositOnline\PorositOnline\Produktet.aspx.cs:34
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35
System.Web.UI.Control.onload(EventArgs e) +99
System.Web.UI.Control.LoadRecursive() +50
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +627

Version Information: Microsoft .NET Framework Version:2.0.50727.3053; ASP.NET Version:2.0.50727.3053
Was This Post Helpful? 0
  • +
  • -

#7 drilli  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 105
  • Joined: 26-May 09

Re: I am losing Session variables after Response.Redirect(url)

Posted 31 August 2011 - 02:29 PM

Thanks All for your help.Now it is everything OK.It is working
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1