2 Replies - 3854 Views - Last Post: 11 December 2013 - 09:52 PM Rate Topic: -----

#1 jesders88  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 25-October 12

Getting Session value from another page and outputting to screen

Posted 11 December 2013 - 08:24 PM

So the title says it all. I have a session in the login page of my site. Problem is when I try to retrieve it from another page and put "Welcome(whatever the persons userName is)" It doesnt work. I need a fresh set of eyes. Codes Below

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class login : System.Web.UI.Page
{
    
    protected void Page_Load(object sender, EventArgs e)
    {
        txtUser.Focus();
        badUserPassCombo.Visible = false;
        if (Page.IsPostBack)
        {
            SqlConnection dbConnection = new SqlConnection("Data Source=.\\SQLEXPRESS;Integrated Security=True");
            try
            {

                dbConnection.Open();
                dbConnection.ChangeDatabase("BedNBreakfast");
                SqlCommand sqlCommand = new SqlCommand("SELECT * FROM Customer WHERE userName ='"
                    + txtUser.Text + "' AND userPassword = '" + txtPassword.Text + "'", dbConnection);


                SqlDataReader curUser = sqlCommand.ExecuteReader();
                if (curUser.Read())
                {
                    Session["userName"] = curUser["userName"].ToString();
                    Session["userPassword"] = curUser["userPassword"].ToString();

                    Response.Redirect("showUserInfo.aspx");
                }
                else
                    
                txtUser.Focus();
                
                badUserPassCombo.Visible = true;
                badUserPassCombo.Text = "Bad UserName and/or Password";

            }
            catch (SqlException exception)
            {
                Response.Write("<p>Error code " + exception.Number
                    + ": " + exception.Message + "</p>");
            }
            dbConnection.Close();
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class showUserInfo : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        lblWelcome.Visible = true;
        
         if (Page.IsPostBack)
        {
            SqlConnection dbConnection = new SqlConnection("Data Source=.\\SQLEXPRESS;Integrated Security=True");
            try
            {

                dbConnection.Open();
                dbConnection.ChangeDatabase("BedNBreakfast");
                SqlCommand sqlCommand = new SqlCommand("SELECT * FROM Customer WHERE userName ='" + Session["userName"] + "'", dbConnection);


                SqlDataReader curUser = sqlCommand.ExecuteReader();
                if (curUser.Read())
                {
                    Session["userName"] = curUser["userName"];
                    //lblWelcome.Text = Session["userName"].ToString();
                    lblWelcome.Text = "<p>" + curUser["userName"].ToString() + "</p>";

                    
                    
                    
                }
                else
                {
                   
                lblWelcome.Text = Session["userName"].ToString();

            }}
            catch (SqlException exception)
            {
                Response.Write("<p>Error code " + exception.Number
                    + ": " + exception.Message + "</p>");
            }
            dbConnection.Close();
        }
    }

    }



Is This A Good Question/Topic? 0
  • +

Replies To: Getting Session value from another page and outputting to screen

#2 trevster344  Icon User is offline

  • The Peasant
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,505
  • Joined: 16-March 11

Re: Getting Session value from another page and outputting to screen

Posted 11 December 2013 - 09:25 PM

I'm not sure if this is the issue but it could possibly be the redirect. If that's not the problem, then try session.add(variable name, value) instead of session(variable) = value. Let me know how that works.
Was This Post Helpful? 0
  • +
  • -

#3 jesders88  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 25-October 12

Re: Getting Session value from another page and outputting to screen

Posted 11 December 2013 - 09:52 PM

View Posttrevster344, on 11 December 2013 - 09:25 PM, said:

I'm not sure if this is the issue but it could possibly be the redirect. If that's not the problem, then try session.add(variable name, value) instead of session(variable) = value. Let me know how that works.

Thanks for the help...I got it to work now I am having trouble just getting values from the database.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1