Problem with Session in .NET c# help please

Discussion in 'General Computer Support' started by maffarazo, Mar 19, 2012.

  1. maffarazo

    maffarazo

    Joined:
    Mar 19, 2012
    Messages:
    1
    Hi, i'm working with a website and im just trying to do something simple for now.
    I can't get my Sessions working in the master page that i set up in the content page.
    Here's a piece of my code in the master page:

    if (Page.IsPostBack == true)
    {
    string name = Session["namn"].ToString();
    namn.Text = Session["namn"].ToString();
    Response.Write(Session["namn"].ToString());
    }

    and here's some code from my content page:

    protected void button_click(object sender, EventArgs e)
    {
    OleDbConnection DB = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; data source=" + Server.MapPath("../App_Data/Solfilmdata.mdb"));
    DB.Open();



    OleDbCommand info = new OleDbCommand("SELECT * FROM users WHERE anv='"+username.Text+"' AND pw='"+pw.Text+"'", DB);

    OleDbDataReader read = info.ExecuteReader();
    read.Read();



    if (read.HasRows == true)
    {
    Session["namn"] = read["anv"].ToString();
    Session["epost"] = read["e-post"].ToString();
    Session["id"] = read["behorighet"].ToString();


    }
    else
    {
    fel.Visible = true;

    }
    read.Close();
    DB.Close();

    }

    In the master page i have a textbox that should state the username, so at first it's just visitor and then when i'm logging in i'm setting up a session for the username but when i try to use that session in the masterpage it says it doesnt exist. and if i do a empty session in the masterpage then it doesnt change. so basicly i have two sessions, one for master page and one for the content pages.
    please help and thanks in advance :)


    Found the solution, just changed the code in the master page to:

    if (Session["namn"] == null)
    {
    namn.Text = "Besökare";
    }
    else
    {
    namn.Text ="Välkommen " + Session["namn"].ToString();
    }

    since it just kept changing the session back to the default setting in master page i removed it entirely from the masterpage.
     
    Last edited: Mar 19, 2012
    maffarazo, Mar 19, 2012
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. bqmassey

    Storing class with session (ASP.Net)

    bqmassey, Sep 22, 2006, in forum: Software
    Replies:
    0
    Views:
    1,166
    bqmassey
    Sep 22, 2006
  2. rammca

    Session handling in ASP.Net 2.0

    rammca, Oct 25, 2006, in forum: Software
    Replies:
    0
    Views:
    3,805
    rammca
    Oct 25, 2006
  3. kalim
    Replies:
    0
    Views:
    1,099
    kalim
    Jul 12, 2007
  4. imman

    session from asp to asp.net

    imman, Feb 20, 2008, in forum: General Computer Support
    Replies:
    0
    Views:
    944
    imman
    Feb 20, 2008
  5. susan_1516

    VB.net (using session variables)

    susan_1516, Sep 26, 2008, in forum: Software
    Replies:
    0
    Views:
    1,676
    susan_1516
    Sep 26, 2008
Loading...

Share This Page