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
    Likes Received:
    0
    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. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.