8 Replies - 975 Views - Last Post: 16 April 2016 - 08:06 PM Rate Topic: -----

#1 csnetsurfer   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 04-March 16

Posting session values from one for to another using ASP.Net/C# ?

Posted 06 April 2016 - 02:10 AM

Not sure where I am going wrong, but I can't post a session variable value from one ASP.Net page to another.

Here is my startpage.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="index.aspx.cs" Inherits="SessionVarTest.index" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" method="post" action="SecondPage.aspx" runat="server" >
    <div>
        <!-- Display objects for input/output -->
        FirstName:<asp:TextBox ID="txb_FName" runat ="server" required= "true" ></asp:TextBox>
        <br />
        LastName:<asp:TextBox ID="txb_LName" runat ="server" required="true" ></asp:TextBox>
        <br />
        UserName:<asp:TextBox ID="txb_UserName" runat ="server" required="true" ></asp:TextBox>
        <br />
        Password:<asp:TextBox ID="txb_Password" runat ="server" required="true" ></asp:TextBox>
        <br />
        Address:<asp:TextBox ID="txb_Address" runat ="server" required="true" ></asp:TextBox>
        <br />
        Email:<asp:TextBox ID="txb_Email" runat ="server" required="true" ></asp:TextBox>
        <br />
        Phone Number:<asp:TextBox ID="txb_PhoneNumber" runat ="server" required="true" ></asp:TextBox>
        <br />
        <asp:Button ID="submitButton" runat="server" Text ="Submit" PostBackUrl="~/SecondPage.aspx" />
    </div>
    </form>
</body>
</html>




Here is my StartPage.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SessionVarTest
{
    public partial class index : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //Pass the stored information in the variables using objects.
            string fName = Request.Form["txb_FName"];
            string lName = Request.Form["txb_LName"];
            string uName = Request.Form["txb_UserName"];
            string pWord = Request.Form["txb_Password"];
            string address = Request.Form["txb_Address"];
            string email = Request.Form["txb_Email"];
            string phone = Request.Form["txb_PhoneNumber"];

            //Session["PhoneID"] = 123;
            Session["fNameID"] = "First Name: " + fName;
            Session["lNameID"] = "Last Name: " + lName ;
            Session["uNameID"] = "User Name: " + uName;
            Session["pWordID"] = "Password: " + pWord;
            Session["addressID"] = "Address: " + address;
            Session["emailID"] = "Email: " + email;
            Session["phoneID"] = "Phone Number " + phone;
        }
    }
}



Here is my SecondPage.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SecondPage.aspx.cs" Inherits="SessionVarTest.SecondPage" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <!--<asp:Label ID ="PhoneID" runat ="server" ></asp:Label>-->

        <!-- Use labels to display the information being passed -->
        <asp:Label ID="lbl_fName" runat="server" ></asp:Label>
        <br />
        <asp:Label ID="lbl_LName" runat="server" ></asp:Label>
        <br />
        <asp:Label ID="lbl_UserName" runat="server" ></asp:Label>
        <br />
        <asp:Label ID="lbl_Password" runat="server" ></asp:Label>
        <br />
        <asp:Label ID="lbl_Address" runat="server" ></asp:Label>
        <br />
        <asp:Label ID="lbl_Email" runat="server" ></asp:Label>
        <br />
        <asp:Label ID="lbl_PhoneNumber" runat="server" ></asp:Label>
    
    </div>
    </form>
</body>
</html>



Here is my SecondPage.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SessionVarTest
{
    public partial class SecondPage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //Make the labels show the ID's values and write to the page.
            string fNameID = (string)Session["lbl_fName"];
            lbl_fName.Text = "First Name: " + fNameID;

            string lNameID = (string)Session["lbl_LName"];
            lbl_LName.Text = "Last Name: " + lNameID;

            string uNameID = (string)Session["lbl_UserName"];
            lbl_UserName.Text = "User Name: " + uNameID;
      
            string passwordID = (string)Session["lbl_Password"];
            lbl_Password.Text = "Password: " + passwordID;

            string phoneID = (string)Session["lbl_PhoneNumber"];
            lbl_PhoneNumber.Text = "Phone Number: " + phoneID;

        }
    }
}



I can set the values and pass them, but I cannot retrieve any values from session variables from one page to the other.
Not sure where I am going wrong.
Any help would be greatly appreciated!


Is This A Good Question/Topic? 0
  • +

Replies To: Posting session values from one for to another using ASP.Net/C# ?

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14168
  • View blog
  • Posts: 56,759
  • Joined: 12-June 08

Re: Posting session values from one for to another using ASP.Net/C# ?

Posted 06 April 2016 - 06:59 AM

Page 2 references a session value here:
15	            string fNameID = (string)Session["lbl_fName"];


That is _not_ the same name given in page 1.
24	            Session["fNameID"] = "First Name: " + fName;


Fix your names in page 2.
Was This Post Helpful? 0
  • +
  • -

#3 csnetsurfer   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 04-March 16

Re: Posting session values from one for to another using ASP.Net/C# ?

Posted 06 April 2016 - 10:07 AM

Thank you so much for your help on this!

I did what you said and the string is now showing.

I really appreciate your help on this!

I so far got the text to show up, but I also need to carry over what the user puts in the text box using a session variable.

Any ideas would be greatly appreciated!

Was This Post Helpful? 0
  • +
  • -

#4 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14168
  • View blog
  • Posts: 56,759
  • Joined: 12-June 08

Re: Posting session values from one for to another using ASP.Net/C# ?

Posted 06 April 2016 - 10:10 AM

I am not sure what you are asking.

Again you need to make sure _ALL_ variables used in SESSION are trying to find the right names. If you name it one thing but call it something else in another page that won't work, will it?. No, it won't.
Was This Post Helpful? 0
  • +
  • -

#5 csnetsurfer   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 04-March 16

Re: Posting session values from one for to another using ASP.Net/C# ?

Posted 06 April 2016 - 12:16 PM

View Postmodi123_1, on 06 April 2016 - 10:10 AM, said:

I am not sure what you are asking.

Again you need to make sure _ALL_ variables used in SESSION are trying to find the right names. If you name it one thing but call it something else in another page that won't work, will it?. No, it won't.


I'm just asking how to use a session variable from the user's input using an input box from one page to the other.

In other words, when the user puts in their first name, how do I store it into a session variable and pass it to display on the next page in the label?

Note: On the first page I have input boxes and on the second page I have labels and I would like to use a session variable to display what I put in the input boxes in the second page labels.

Was This Post Helpful? 0
  • +
  • -

#6 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14168
  • View blog
  • Posts: 56,759
  • Joined: 12-June 08

Re: Posting session values from one for to another using ASP.Net/C# ?

Posted 06 April 2016 - 12:18 PM

Ah, I thought I covered that in my original post. You were using the wrong names for session variables stored in page1.
Was This Post Helpful? 0
  • +
  • -

#7 csnetsurfer   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 04-March 16

Re: Posting session values from one for to another using ASP.Net/C# ?

Posted 08 April 2016 - 01:05 AM

I'm trying to capture what the user puts in the text box as a string into a session variable and post it on the next page in a label. Here is what I have so far:


Page1.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="index.aspx.cs" Inherits="SessionVarTest.index" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" method="post" action="SecondPage.aspx" runat="server" >
    <div> 
        <!-- Display objects for input/output -->
        First Name: <asp:TextBox ID="txb_FName" type="textbox" runat ="server" required ="true" ></asp:TextBox>
        <asp:Button ID="submitButton" runat="server" Text ="Submit" PostBackUrl="~/SecondPage.aspx" />
    </div>
    </form>
</body>
</html>



Page1.cs

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

namespace SessionVarTest
{
    public partial class index : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Session["userID"] = 123; //This was for testing and I got 123 on the next page, but cannot get what is in the textbox.
            Session["fName"] = txb_FName.Text; //Passing to the session variable to the label on the next page.
        }
    }
}




Page2.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SecondPage.aspx.cs" Inherits="SessionVarTest.SecondPage" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID ="lbl_id" runat ="server" ></asp:Label>

        <!-- Use label ID to display the information being passed, Display the label -->
        First Name: <asp:Label ID="lbl_fName" runat="server" ></asp:Label>
    </div>
    </form>
</body>
</html>



Page2.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SessionVarTest
{
    public partial class SecondPage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {       
            int uID = (int)Session["userID"];
            lbl_id.Text = uID.ToString();

            string firstName = (string)Session["fName"];
            lbl_fName.Text = firstName;
        }
    }
}




I'm trying to capture what the user puts in the text box as a string into a session variable and post it on the next page in a label. Here is what I have so far:


Page1.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="index.aspx.cs" Inherits="SessionVarTest.index" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" method="post" action="SecondPage.aspx" runat="server" >
    <div> 
        <!-- Display objects for input/output -->
        First Name: <asp:TextBox ID="txb_FName" type="textbox" runat ="server" required ="true" ></asp:TextBox>
        <asp:Button ID="submitButton" runat="server" Text ="Submit" PostBackUrl="~/SecondPage.aspx" />
    </div>
    </form>
</body>
</html>



Page1.cs

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

namespace SessionVarTest
{
    public partial class index : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Session["userID"] = 123; //This was for testing and I got 123 on the next page, but cannot get what is in the textbox.
            Session["fName"] = txb_FName.Text; //Passing to the session variable to the label on the next page.
        }
    }
}




Page2.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SecondPage.aspx.cs" Inherits="SessionVarTest.SecondPage" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID ="lbl_id" runat ="server" ></asp:Label>

        <!-- Use label ID to display the information being passed, Display the label -->
        First Name: <asp:Label ID="lbl_fName" runat="server" ></asp:Label>
    </div>
    </form>
</body>
</html>



Page2.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SessionVarTest
{
    public partial class SecondPage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {       
            int uID = (int)Session["userID"];
            lbl_id.Text = uID.ToString();

            string firstName = (string)Session["fName"];
            lbl_fName.Text = firstName;
        }
    }
}




I'm trying to capture what the user puts in the text box as a string into a session variable and post it on the next page in a label. Here is what I have so far:


Page1.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="index.aspx.cs" Inherits="SessionVarTest.index" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" method="post" action="SecondPage.aspx" runat="server" >
    <div> 
        <!-- Display objects for input/output -->
        First Name: <asp:TextBox ID="txb_FName" type="textbox" runat ="server" required ="true" ></asp:TextBox>
        <asp:Button ID="submitButton" runat="server" Text ="Submit" PostBackUrl="~/SecondPage.aspx" />
    </div>
    </form>
</body>
</html>



Page1.cs

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

namespace SessionVarTest
{
    public partial class index : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Session["userID"] = 123; //This was for testing and I got 123 on the next page, but cannot get what is in the textbox.
            Session["fName"] = txb_FName.Text; //Passing to the session variable to the label on the next page.
        }
    }
}




Page2.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SecondPage.aspx.cs" Inherits="SessionVarTest.SecondPage" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID ="lbl_id" runat ="server" ></asp:Label>

        <!-- Use label ID to display the information being passed, Display the label -->
        First Name: <asp:Label ID="lbl_fName" runat="server" ></asp:Label>
    </div>
    </form>
</body>
</html>



Page2.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SessionVarTest
{
    public partial class SecondPage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {       
            int uID = (int)Session["userID"];
            lbl_id.Text = uID.ToString();

            string firstName = (string)Session["fName"];
            lbl_fName.Text = firstName;
        }
    }
}




I'm trying to capture what the user puts in the text box as a string into a session variable and post it on the next page in a label. Here is what I have so far:


Page1.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="index.aspx.cs" Inherits="SessionVarTest.index" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" method="post" action="SecondPage.aspx" runat="server" >
    <div> 
        <!-- Display objects for input/output -->
        First Name: <asp:TextBox ID="txb_FName" type="textbox" runat ="server" required ="true" ></asp:TextBox>
        <asp:Button ID="submitButton" runat="server" Text ="Submit" PostBackUrl="~/SecondPage.aspx" />
    </div>
    </form>
</body>
</html>



Page1.cs

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

namespace SessionVarTest
{
    public partial class index : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Session["userID"] = 123; //This was for testing and I got 123 on the next page, but cannot get what is in the textbox.
            Session["fName"] = txb_FName.Text; //Passing to the session variable to the label on the next page.
        }
    }
}




Page2.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SecondPage.aspx.cs" Inherits="SessionVarTest.SecondPage" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID ="lbl_id" runat ="server" ></asp:Label>

        <!-- Use label ID to display the information being passed, Display the label -->
        First Name: <asp:Label ID="lbl_fName" runat="server" ></asp:Label>
    </div>
    </form>
</body>
</html>



Page2.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SessionVarTest
{
    public partial class SecondPage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {       
            int uID = (int)Session["userID"];
            lbl_id.Text = uID.ToString();

            string firstName = (string)Session["fName"];
            lbl_fName.Text = firstName;
        }
    }
}



Was This Post Helpful? 0
  • +
  • -

#8 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14168
  • View blog
  • Posts: 56,759
  • Joined: 12-June 08

Re: Posting session values from one for to another using ASP.Net/C# ?

Posted 08 April 2016 - 09:17 AM

Think about your order of operations.
15	            Session["fName"] = txb_FName.Text;


Happens when the form _FIRST_ loads.
12	        protected void Page_Load(


Clearly the user hasn't had time to input data yet, right? I mean this is the form LOADING.

I would figure you would have a button click event for 'submitButton' and then assign the text box to a session variable then.
Was This Post Helpful? 0
  • +
  • -

#9 csnetsurfer   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 04-March 16

Re: Posting session values from one for to another using ASP.Net/C# ?

Posted 16 April 2016 - 08:06 PM

Thank you for your help on this, this was a simple mistake, but I am learning.

I figured out what was stopping the Session variable from passing. It was that I had in redirecting the page inside the form.

Here is the code that works correctly now:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="index.aspx.cs" Inherits="SessionVarTest.index" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" method="post" runat="server" >
    <div>
        <!-- Display input textboxes and a submit button. 
             Require that the user inputs into the text boxes. -->
        First Name: <asp:TextBox ID="txb_FName" runat ="server" required = "required" ></asp:TextBox><br />
        Last Name: <asp:TextBox ID="txb_LName" runat ="server" required = "required" ></asp:TextBox><br />
        User Name: <asp:TextBox ID="txb_UserName" runat ="server" required = "required" ></asp:TextBox><br />
        Password: <asp:TextBox ID="txb_Password" runat ="server" required = "required" ></asp:TextBox><br />
        Address: <asp:TextBox ID="txb_Address" runat ="server" required = "required" ></asp:TextBox><br />       
        Email: <asp:TextBox ID="txb_Email" runat ="server" required = "required" ></asp:TextBox><br />
        Phone: <asp:TextBox ID="txb_PhoneNumber" runat ="server" required = "required" ></asp:TextBox><br />
        <asp:Button ID="submitButton" runat="server" Text ="Submit" onclick="submitButton_Click" />
    </div>
    </form>

</body>
</html>



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

namespace SessionVarTest
{
    public partial class index : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void submitButton_Click(object sender, System.EventArgs e)
        {
            /*Place the text box value in a session variable 
              and then go to the second page.*/
            Session["fName"] = txb_FName.Text;
            Session["lName"] = txb_LName.Text;
            Session["uName"] = txb_UserName.Text;
            Session["pWord"] = txb_Password.Text;
            Session["address"] = txb_Address.Text;
            Session["email"] = txb_Email.Text;
            Session["phone"] = txb_PhoneNumber.Text;
            Response.Redirect("~/SecondPage.aspx");
        }
    }
}



<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SecondPage.aspx.cs" Inherits="SessionVarTest.SecondPage" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <!-- Use the labels ID to display the information being passed, 
             Display the labels and a button on their own line. -->
        First Name: <asp:Label ID="lbl_fName" runat="server" ></asp:Label><br />
        Last Name: <asp:Label ID="lbl_LName" runat="server" ></asp:Label><br />
        User Name: <asp:Label ID="lbl_UserName" runat="server" ></asp:Label><br />
        Password: <asp:Label ID="lbl_Password" runat="server" ></asp:Label><br />
        Address: <asp:Label ID="lbl_Address" runat="server" ></asp:Label><br />
        Email: <asp:Label ID="lbl_Email" runat="server" ></asp:Label><br />
        Phone: <asp:Label ID="lbl_PhoneNumber" runat="server" ></asp:Label>
    </div>
    </form>
</body>
</html>



<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SecondPage.aspx.cs" Inherits="SessionVarTest.SecondPage" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <!-- Use the labels ID to display the information being passed, 
             Display the labels and a button on their own line. -->
        First Name: <asp:Label ID="lbl_fName" runat="server" ></asp:Label><br />
        Last Name: <asp:Label ID="lbl_LName" runat="server" ></asp:Label><br />
        User Name: <asp:Label ID="lbl_UserName" runat="server" ></asp:Label><br />
        Password: <asp:Label ID="lbl_Password" runat="server" ></asp:Label><br />
        Address: <asp:Label ID="lbl_Address" runat="server" ></asp:Label><br />
        Email: <asp:Label ID="lbl_Email" runat="server" ></asp:Label><br />
        Phone: <asp:Label ID="lbl_PhoneNumber" runat="server" ></asp:Label>
    </div>
    </form>
</body>
</html>


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1