Click here to Skip to main content
15,899,475 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i im trying to show button of hide through sessions. but nothing happening

What I have tried:

if (Session["acct"].Equals("Buyer")) // it shows an error. Object reference not set to an instance of an object.
           {
               Button5.Visible = false;
               foreach (DataListItem item in DataList1.Items)
               {
                   Button btn = item.FindControl("Button6") as Button;

                   btn.Visible = true;


                   LinkButton btn2 = item.FindControl("LinkButton1") as LinkButton;

                   btn2.Visible = false;
               }
           }


if (Session["acct"].ToString()==("Buyer"))  // it also shows an error.  Object reference not set to an instance of an object.
           {
               Button5.Visible = false;
               foreach (DataListItem item in DataList1.Items)
               {
                   Button btn = item.FindControl("Button6") as Button;

                   btn.Visible = true;


                   LinkButton btn2 = item.FindControl("LinkButton1") as LinkButton;

                   btn2.Visible = false;
               }
           }



if (Session["acct"]== "Buyer") // no error but not working
           {
               Button5.Visible = false;
               foreach (DataListItem item in DataList1.Items)
               {
                   Button btn = item.FindControl("Button6") as Button;

                   btn.Visible = true;


                   LinkButton btn2 = item.FindControl("LinkButton1") as LinkButton;

                   btn2.Visible = false;
               }
           }
Posted
Comments
Member 12982873 26-May-18 1:36am    
any one there ???
Member 12982873 26-May-18 8:09am    
Any there plz ???
an0ther1 27-May-18 19:01pm    
Review your code, specifically where you have set your session value.
Object reference not set in snippets 1 & 2 are saying that the session does not contain a value named acct

If the Session does not contain this value you will get object reference not set, typically you would test for a Null value & then reset the value prior to using it, typically something like;
if(this.Session["acct"] == null)
{
// populate the value
}
// get the value and use it

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