I am retrieving values from database, i checked the values are coming fine but when i try to run the code and it transfer me from LOGIN page to the MOVIE PLAYER form the session value is not working?
THIS IS MY LOGIN PAGE
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace PlayMovie
{
public partial class LoginForm : System.Web.UI.Page
{
private LoginBLL bll;
public LoginForm()
{
bll = new LoginBLL();
}
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnLogin_Click(object sender, EventArgs e)
{
string uname = txtBoxUsername.Text.Trim();
string pass = txtBoxPass.Text.Trim();
LoginBLL customer = bll.GetName(uname);
bool entry = bll.CheckLoginCreds(pass, uname);
if (entry)
{
Response.Redirect("MoviePlayer.aspx");
/*Session["FirstName"] =
Session["LastName"] = customer.LName;*/
Session["fName"] = customer.FName;
}
else {
lblInvalidLogin.Text = "Login Failed";
lblInvalidLogin.ForeColor = System.Drawing.Color.Red;
}
}
protected void linkBtnReg_Click(object sender, EventArgs e)
{
Server.Transfer("Register.aspx", true);
}
}
}
Now this is my MOVIEPLAYER
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace PlayMovie
{
public partial class PlayMovie : System.Web.UI.Page
{
private MovieBLL bll;
public PlayMovie() {
bll = new MovieBLL();
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
LoadListBoxData();
string sessionValue = (string)Session["fName"];
labelFName.Text = sessionValue;
//Response.Write(firstName);
}
}
public void LoadListBoxData() {
List<MovieBLL> listMovies = bll.LoadMovieNames();
listBoxMovies.DataSource = listMovies;
listBoxMovies.DataTextField = "MovieName";
listBoxMovies.DataValueField = "MovieID";
listBoxMovies.DataBind();
}
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
}
public void FillMovieData()
{
foreach (ListItem movie in listBoxMovies.Items)
{
if (movie.Selected)
{
int movieID = Convert.ToInt32(movie.Value);
string movieName = movie.Text;
MovieBLL movieData = bll.GetMovieData(movieID);
if (movieData != null)
{
txtBoxMName.Text =movie.Text;
txtBoxMGenre.Text = movieData.MovieGenre;
txtBoxMRating.Text = movieData.MovieRating+"/5.0";
txtBoxMDesc.Text = movieData.MovieDesc;
}
else
{
txtBoxMName.Text = "";
txtBoxMGenre.Text = "";
txtBoxMRating.Text = "";
txtBoxMDesc.Text = "";
}
}
}
}
protected void listBoxMovies_SelectedIndexChanged(object sender, EventArgs e)
{
//FillMovieData();
}
protected void Button1_Click(object sender, EventArgs e)
{
FillMovieData();
moviePlayer.Enabled = true;
btnPlay.Enabled = true;
}
protected void btnPlay_Click(object sender, EventArgs e)
{
foreach (ListItem movie in listBoxMovies.Items)
{
if (movie.Selected)
{
moviePlayer.FullScreen = true;
moviePlayer.MovieURL = @"\Movies\" + movie.Text + ".mp4";
}
}
}
}
}
We can clearly see that i retreived value from database i.e. customer.FName and then put it into a session variable, then i used response.redirect to go to other page and then i retreived my session variable and put it into a label but it is showing nothing.
Hey @Buttman on your login page you need to set the session variable before you redirect the user (otherwise the session value is never set, and will not be accessible - as you are currently seeing).
To fix this try re-ordering your login page code to:
Session["fName"] = customer.FName; // Set Session first
Response.Redirect("MoviePlayer.aspx"); // Then redirect user.
That should then allow you to access the session variable in subsequent pages. Hope that helps :)