Session variables and Local host

Last post 07-01-2009 1:40 PM by forexbob. 8 replies.

Sort Posts:

  • Session variables and Local host

    05-26-2009, 4:11 PM
    • Member
      point Member
    • rienmann
    • Member since 05-26-2009, 8:07 PM
    • Posts 7

    I can't make session variables stick even in the simplest applications.  I have spent a week on this problem, I am so frustrated.

     

    I try to add a session variable on a web page, such as someones name.  Then when I try to use it.

     

    ie. (string)Session["name"], it is always blank.  However, if I add it to the global file, I can use it.

     

    I want something modifable. 

     

    I AM GOING CRAZY. What am I doing wrong?  I heard it has something to do with the : in the localhost:32154\project etc....

    Any help will be greatly appreciated..

  • Re: Session variables and Local host

    05-26-2009, 5:10 PM
    • Contributor
      5,624 point Contributor
    • RatheeshC
    • Member since 04-25-2008, 6:05 PM
    • Posts 1,198

    Hi,

    Please send the code on how you are adding & retrieving the value to session...?

     

    Thanks

    Ratheesh

    Thanks
    Ratheesh

    Please mark it as answer if it resolves your issue.
  • Re: Session variables and Local host

    05-26-2009, 7:24 PM
    • Member
      point Member
    • rienmann
    • Member since 05-26-2009, 8:07 PM
    • Posts 7
    Session.Add("StreetNo", txtStreetNo.Text);

    Session.Add("Street", txtStreet.Text);

    Session.Add("City", txtCity.Text);

    Session.Add("Province", drpdwnProvince.SelectedValue);

    Session.Add("PostalCode", txtPostalCode.Text);

     

     

    And I retrieve it as textbox1.Text =

    (string)Session["StreetNo"] + " " +

    (string)Session["Street"] + ", " +

    (string)Session["City"] + ", " +

    (string)Session["Province"] + ", " +

    (string)Session["PostalCode"];

     


    These are each on there own pages...

     

    However, if I use the Global.asax file

     

    void Session_Start(object sender, EventArgs e)

    {

    //Code that runs when a new session is started

    //Session.Add("StreetNo", "283");

    //Session.Add("Street", "bell");

    //Session.Add("City", "ottawa");

    //Session.Add("Province", "ON");

    //Session.Add("PostalCode", "k1s4j7");

     

    }

     

    This code when it is not commented out will work when I extract the variables with the exact same code...

     

    Any ideas

  • Re: Session variables and Local host

    06-01-2009, 3:42 AM
    Answer

    Hi,

    Firstly, please make sure the Session.Add method is executed and there are values in TextBoxs. You can insert Breakpoint to validate them during debugging session.

    For testing, you can try to run the following code and see whether you can get value from Session.

    <%@ Page Language="C#" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <script runat="server">

        protected void Page_Load(object sender, EventArgs e)
        {
            Session.Add("Name", "Leo");
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Response.Write("Name is "+(string)Session["Name"]);
        }
    </script>

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>Untitled Page</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:button ID="Button1" runat="server" Text="Show Session value" OnClick="Button1_Click" /></div>
        </form>
    </body>
    </html>

    Please also make sure the Session is not cleared before you try to access it.

     

    I look forward to receiving your test results.

    Thomas Sun
    Microsoft Online Community Support

    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question.
  • Re: Session variables and Local host

    06-02-2009, 11:28 AM
    • Contributor
      5,624 point Contributor
    • RatheeshC
    • Member since 04-25-2008, 6:05 PM
    • Posts 1,198

    If you are still facing issue, please let me know

    Thanks
    Ratheesh

    Please mark it as answer if it resolves your issue.
  • Re: Session variables and Local host

    06-18-2009, 1:51 PM
    • Member
      9 point Member
    • forexbob
    • Member since 10-10-2008, 12:39 PM
    • Posts 12

    I'm having a similar problem with session varibales. I used the code above to test to see if was working and it doesn't return the name. This is my code:

    Code Behind:

    Partial Class zzTest
        Inherits System.Web.UI.Page
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
            Session.Add("Name", "Leo")
        End Sub

        Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
            Response.Write("Name is " + CType(Session("Name"), String))
        End Sub
    End Class

    zzTest.aspx:

    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="zzTest.aspx.vb" Inherits="zzTest" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
              <asp:button ID="Button1" runat="server" Text="Show Session value" OnClick="Button1_Click" /></div>
        </div>
        </form>
    </body>
    </html>

    So if it's not working, what do I do?

    I'm a rookie at this and would normally post in the "Getting Started" group. So very verbose explanations are of great help to me.

    Thanks

    Bob

  • Re: Session variables and Local host

    06-18-2009, 8:23 PM
    • Member
      point Member
    • rienmann
    • Member since 05-26-2009, 8:07 PM
    • Posts 7

    I decided to use PreviousPage instead.  I never really understood why I couldn't create Session variables, unless they were in the global file.  And then access them on a page.  So i gave up..

     

    Thanks for your help,

     

    I will still have this problem in the future, but for now I am making due...

  • Re: Session variables and Local host

    07-01-2009, 9:34 AM
    • Contributor
      4,762 point Contributor
    • alaa9jo
    • Member since 07-01-2009, 1:17 PM
    • Jordan
    • Posts 886

    for forexbob problem:

    if you add a breakpoint in page_load,you will notice that the debugger wont break at it,which means page_load is not fired at all,that because AutoEventWireUp is set to false (you will find it in the source of your page),turning it to true should do it!

    but for you rienmann,I guess you have the same problem as forexbob.

    If any of you still facing the issue, please let me know

    Best Regards,
    Ala'a Alnajjar
    ----------------------------------------------------
    Please remember to click “Mark as Answer” on the post(s) that help(s) you even if they were for the same person.
    Also remember to close the thread by selecting “Resolved”,you will find it in your first post.

    Convert C# to VB.net and visa versus
  • Re: Session variables and Local host

    07-01-2009, 1:40 PM
    • Member
      9 point Member
    • forexbob
    • Member since 10-10-2008, 12:39 PM
    • Posts 12

    Thanks alaa9jo

    That was a simple fix. Now I can get on with my other project to see if I can figure it out.

    Bob

Page 1 of 1 (9 items)