locked
Session is not showing in other page RRS feed

  • Question

  • User-1642741938 posted

    I am stored one session variable, following is the coding:

    objConnection.Open();
            String str = "select UserName,Password from Login where UserName='" + UserNametxt.Text + "' and Password='" + Passwordtext.Text + "'";
            objCommand = new SqlCommand(str, objConnection);
            SqlDataAdapter objDataAdapter = new SqlDataAdapter(objCommand);
            DataSet objDataSet = new DataSet();
            objDataAdapter.Fill(objDataSet);
            if (objDataSet.Tables[0].Rows.Count > 0)
            {
                Response.Write(@"<script>alert('Valid ID')</script>");
                Response.Redirect("Blog.aspx");
            }
            else
            {
                Response.Write(@"<script>alert('Please enter the Valid ID')</script>");
            }
            //UserInfo objUserInfo = new UserInfo(UserNametxt.Text,Passwordtext.Text);
            Session["UserName"] = UserNametxt.Text;
            Session["UserName"] = DateTime.Now;

    I want to show the session in the other pages, i place the label and link button(to logout) in the other pages, the following is the code i used:
    objConnection.Open();
            String str = "select UserName,Password from Login where UserName='" + UserNametxt.Text + "' and Password='" + Passwordtext.Text + "'";
            objCommand = new SqlCommand(str, objConnection);
            SqlDataAdapter objDataAdapter = new SqlDataAdapter(objCommand);
            DataSet objDataSet = new DataSet();
            objDataAdapter.Fill(objDataSet);
            if (objDataSet.Tables[0].Rows.Count > 0)
            {
                Response.Write(@"<script>alert('Valid ID')</script>");
                Response.Redirect("Blog.aspx");
            }
            else
            {
                Response.Write(@"<script>alert('Please enter the Valid ID')</script>");
            }
            //UserInfo objUserInfo = new UserInfo(UserNametxt.Text,Passwordtext.Text);
            Session["UserName"] = UserNametxt.Text;
            Session["UserName"] = DateTime.Now;

    I want to show the session username in the other pages,  so i place a label and link button(for logout) in other pages, following is the coding i used:

    protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserName"] != null)
            {
                Label1.Text = "Welcome: " + Session["UserName"];
            }
            else
            {
                Label1.Visible = false;
                LinkButton1.Visible = false;
            }
        }

    But when i am running the application, in the other pages, the label and linkbutton are not visible. Please give me the solution for this.
    Saturday, October 22, 2011 7:42 AM

Answers

  • User1413134711 posted

    HI abdur because you maintion the Session after Response.Redirect();

    please write

    Session["UserName"] = UserNametxt.Text;
           
    Session["UserName"] = DateTime.Now;

    above code above the Response .Redirect(); like below

    String str = "select UserName,Password from Login where UserName='" + UserNametxt.Text + "' and Password='" + Passwordtext.Text + "'";
            objCommand = new SqlCommand(str, objConnection);
            SqlDataAdapter objDataAdapter = new SqlDataAdapter(objCommand);
            DataSet objDataSet = new DataSet();
            objDataAdapter.Fill(objDataSet);
      Session["UserName"] = UserNametxt.Text;
            Session["UserName"] = DateTime.Now;
     if (objDataSet.Tables[0].Rows.Count > 0)
            {
                Response.Write(@"<script>alert('Valid ID')</script>");
                Response.Redirect("Blog.aspx");
            }
            else
            {
                Response.Write(@"<script>alert('Please enter the Valid ID')</script>");
            }
    
    hope this will help you


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, October 22, 2011 7:46 AM

All replies

  • User1413134711 posted

    HI abdur because you maintion the Session after Response.Redirect();

    please write

    Session["UserName"] = UserNametxt.Text;
           
    Session["UserName"] = DateTime.Now;

    above code above the Response .Redirect(); like below

    String str = "select UserName,Password from Login where UserName='" + UserNametxt.Text + "' and Password='" + Passwordtext.Text + "'";
            objCommand = new SqlCommand(str, objConnection);
            SqlDataAdapter objDataAdapter = new SqlDataAdapter(objCommand);
            DataSet objDataSet = new DataSet();
            objDataAdapter.Fill(objDataSet);
      Session["UserName"] = UserNametxt.Text;
            Session["UserName"] = DateTime.Now;
     if (objDataSet.Tables[0].Rows.Count > 0)
            {
                Response.Write(@"<script>alert('Valid ID')</script>");
                Response.Redirect("Blog.aspx");
            }
            else
            {
                Response.Write(@"<script>alert('Please enter the Valid ID')</script>");
            }
    
    hope this will help you


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, October 22, 2011 7:46 AM
  • User-1500873099 posted

    Hi try this-

    //UserInfo objUserInfo = new UserInfo(UserNametxt.Text,Passwordtext.Text); 
           
    Session["UserName"] = UserNametxt.Text; 
           
    Session["UserName"] = DateTime.Now; 

    In place of above line write


    Saturday, October 22, 2011 7:52 AM
  • User-1642741938 posted

    HI abdur because you maintion the Session after Response.Redirect();

    please write

    Session["UserName"] = UserNametxt.Text;
           
    Session["UserName"] = DateTime.Now;

    Hi thanks for your reply, i got the solution.

    Saturday, October 22, 2011 7:54 AM
  • User-183374066 posted

    put your session code before redirection

    objConnection.Open();
            String str = "select UserName,Password from Login where UserName='" + UserNametxt.Text + "' and Password='" + Passwordtext.Text + "'";
            objCommand = new SqlCommand(str, objConnection);
            SqlDataAdapter objDataAdapter = new SqlDataAdapter(objCommand);
            DataSet objDataSet = new DataSet();
            objDataAdapter.Fill(objDataSet);
            if (objDataSet.Tables[0].Rows.Count > 0)
            {
                Response.Write(@"<script>alert('Valid ID')</script>");
                Session["UserName"] = UserNametxt.Text;
                Session["UserName"] = DateTime.Now;
                Response.Redirect("Blog.aspx");
            }
            else
            {
                Response.Write(@"<script>alert('Please enter the Valid ID')</script>");
            }
            //UserInfo objUserInfo = new UserInfo(UserNametxt.Text,Passwordtext.Text);
            
    Saturday, October 22, 2011 8:00 AM