Click here to Skip to main content
15,884,836 members
Please Sign up or sign in to vote.
1.23/5 (4 votes)
See more:
use session so that the username will be displayed in another webform asp c#


unable to display username

What I have tried:

if (Context.Session != null)
              {
                  BindGrid();

              }else
              {
                  Response.Redirect("Login.aspx");
              }


i tried this

private void BindGrid(string sortExpression = null)
       {
           conn.Open();

           SqlCommand cmd = new SqlCommand("SELECT * from [test].[dbo].[myform] ", conn);
           SqlDataAdapter sda = new SqlDataAdapter();

           cmd.Connection = conn;
           sda.SelectCommand = cmd;
           using (DataTable dt = new DataTable())
           {
               sda.Fill(dt);
               if (sortExpression != null)
               {
                   DataView dv = dt.AsDataView();
                   this.SortDirection = this.SortDirection == "ASC" ? "DESC" : "ASC";
                   dv.Sort = sortExpression + " " + this.SortDirection;
                   GridView2.DataSource = dv;
               }
               else
               {
                   GridView2.DataSource = dt;
               }
               GridView2.DataBind();
               conn.Close();

           }

       }
Posted
Comments
jimmson 26-Feb-20 7:51am    
The code you've posted has nothing to do with sessions..
ZurdoDev 26-Feb-20 8:44am    
Session["SomeName"] = "SomeValue";
String someValue = Session["SomeName"];

Pretty simple.
sudevsu 3-Mar-20 8:35am    
As soon as you log in, store the user into a session like ZurdoDev commented above. It holds the value until the session is expired. When does a session expires? You must be already knowing an answer to this so it is pretty simple to show session values in another web form of same application using sessions
dnxit 8-Mar-20 7:49am    
Session variable with a key for identification can be used like this

protected void Button1_Click(object sender, EventArgs e)
{
//textbox value is stored in Session
Session["UserName"] = tbUserName.Text;
Response.Redirect("WebForm2.aspx");
}

protected void WebForm2_Page_Load(object sender, EventArgs e)
{
//Session value is assign on the text box
if (Session["UserName"] != null)
{
tbUserName.Text = Session["UserName"].ToString();
}
}

Read this article for details
https://www.c-sharpcorner.com/UploadFile/225740/introduction-of-session-in-Asp-Net/

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900