I have a detils view on a page, that if a user isn't a "PowerUser", I need to hide the edit button of the detailsview. But, I keep getting this error.
Object reference not set to an instance of an object.
Here's my code from my code behind, where I'm checking the access level, and if the user isn't the proper level, I'm trying to hide the button. What am I forgeting to do?
I think the null value is your session variable as you aren't putting it out as a string. I would put the session variable into a string variable and compare that way.
string CompareVal = Session["PowerUser"];
if (CompareVal == "Y")
{
Forgot to mention, that the line it's failing on is below, so it's fine on the session portion, just not on finding the button from the details view..??
1) No exception is thrown if you attempt to get a value out of session state that does not exist. To be sure that the value you want is in session state, check first for the existence of the object with a test such as the following:
if (Session["PowerUser"] == null)
// No such value in session state; take appropriate action.
2) Get Session value like the following:
string powerUser= (string)(Session["PowerUser"]);
3) Use the ChangeMode method to programmatically switch the DetailsView control between edit, insert, and read-only mode. This method also updates the CurrentMode property with the specified mode. The following table lists the different mode values.
funluckykitt...
Participant
1470 Points
1550 Posts
System.NullReferenceException: Object reference not set to an instance of an object. - Trying to ...
Nov 09, 2012 05:55 PM|LINK
I have a detils view on a page, that if a user isn't a "PowerUser", I need to hide the edit button of the detailsview. But, I keep getting this error. Object reference not set to an instance of an object.
Here's my code from my code behind, where I'm checking the access level, and if the user isn't the proper level, I'm trying to hide the button. What am I forgeting to do?
From my page & Code behind
<asp:TemplateField HeaderText=""> <EditItemTemplate> <asp:Button ID="btnUpdate" runat="server" CausesValidation="True" CommandName="Update" Text="Update" /> <asp:Button ID="btnCancel" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel" /> </EditItemTemplate> <ItemTemplate> <asp:Button ID="btnEdit" runat="server" CausesValidation="False" CommandName="Edit"Text="Edit"/> </ItemTemplate> </asp:TemplateField> From my code behind: protected void dvDetails_DataBound(object sender, EventArgs e) { Button btnEdit = (Button)dvDetails.FindControl("btnEdit") as Button; if (Session["PowerUser"] == "Y") { btnEdit.Visible = false; } else { btnEdit.Visible = true; } }bbcompent1
All-Star
33718 Points
8737 Posts
Moderator
Re: System.NullReferenceException: Object reference not set to an instance of an object. - Trying...
Nov 09, 2012 06:07 PM|LINK
I think the null value is your session variable as you aren't putting it out as a string. I would put the session variable into a string variable and compare that way.
string CompareVal = Session["PowerUser"]; if (CompareVal == "Y") {funluckykitt...
Participant
1470 Points
1550 Posts
Re: System.NullReferenceException: Object reference not set to an instance of an object. - Trying...
Nov 09, 2012 07:54 PM|LINK
Tried that, but get this error CS0266: Cannot implicitly convert type 'object' to 'string'. An explicit conversion exists (are you missing a cast?)
funluckykitt...
Participant
1470 Points
1550 Posts
Re: System.NullReferenceException: Object reference not set to an instance of an object. - Trying...
Nov 09, 2012 07:56 PM|LINK
Forgot to mention, that the line it's failing on is below, so it's fine on the session portion, just not on finding the button from the details view..??
btnEdit.Visible = true;
clevesteve
Participant
1405 Points
406 Posts
Re: System.NullReferenceException: Object reference not set to an instance of an object. - Trying...
Nov 09, 2012 08:39 PM|LINK
Couple things... from the looks of your code, you are hiding the edit button if the user is a power user... is that what you want to do?
Also, is the DetailsView definitely in read-only mode? I'm pretty sure this will fail when the DetailsView is in Edit mode.
protected void dvDetails_DataBound(object sender, EventArgs e) { Button btnEdit = (Button)dvDetails.FindControl("btnEdit") as Button; If(dvDetails.CurrentMode == DetailsViewMode.ReadOnly) { if (Session["PowerUser"] == "Y") { btnEdit.Visible = false; } else { btnEdit.Visible = true; } } }monjurul hab...
Member
62 Points
11 Posts
Re: System.NullReferenceException: Object reference not set to an instance of an object. - Trying...
Nov 09, 2012 09:43 PM|LINK
Steps should follow:
1) No exception is thrown if you attempt to get a value out of session state that does not exist. To be sure that the value you want is in session state, check first for the existence of the object with a test such as the following:
if (Session["PowerUser"] == null) // No such value in session state; take appropriate action.2) Get Session value like the following:
3) Use the ChangeMode method to programmatically switch the DetailsView control between edit, insert, and read-only mode. This method also updates the CurrentMode property with the specified mode. The following table lists the different mode values.
DetailsView.ChangeMode Method
Let me know if your feedback.
Habib
oned_gk
All-Star
35714 Points
7294 Posts
Re: System.NullReferenceException: Object reference not set to an instance of an object. - Trying...
Nov 10, 2012 12:26 AM|LINK
if (Session["PowerUser"] != null) { if (Session["PowerUser"] == "Y") { btnEdit.Visible = false; } else { btnEdit.Visible = true; } }Suwandi - Non Graduate Programmer