Asp Net Session Error
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About
Asp Net Session Timeout
Us Learn more about Stack Overflow the company Business Learn more about hiring sessions in asp net c# developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the
Asp.net Session Variables
Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up ASP / C# Session Variable how to store value in session in asp net c# - Object reference not set to an instance of an object up vote 2 down vote favorite I'm somewhat new to ASP / C# and I'm having an issue (probably simple) with sessions variables. My project has a Site.Master in which session variables are set under the Page_Load method like so: protected void Page_Load(object sender, EventArgs e) { if ((Session)["UserID"]==null || (Session)["UserID"].ToString() == "") { (Session)["UserID"] asp.net session tutorial = HttpContext.Current.User.Identity.Name.ToString(); SqlDataReader dr = Sprocs.GetPermissionGroups(); string groupList = ""; while (dr.Read()) { if (groupList != "") { groupList = groupList + "|" + dr["WG_Group"].ToString(); } else { groupList = dr["WG_Group"].ToString(); } } dr.Close(); if (groupList != "") { (Session)["UserGroups"] = groupList; } } This does work. If I dump out the session variable 'UserGroups' to a label or something within this method, it does display the variable contents correctly. So, my problem lies within another page (say default.aspx) when I try to access that same session variable. In the Page_Load method of the other page I attempt to do this: protected void Page_Load(object sender, EventArgs e) { string GroupList = HttpContext.Current.Session["UserGroups"].ToString(); //some code with the variables here } This always fails with an "Object reference not set to an instance of an object." error. Am I trying to get the Session variable wrong? I've tried string GroupList = Session["UserGroups"].ToString(); this also errors with the same error. string GroupList = (string)(Session["UserGroups"]); This always returns an empty string. What am I doing wrong? Thanks! c# asp.net session share|improve this question edited Dec 30 '11 at 14:16 Jon Egerton 22.7k85191 asked Dec 29 '11 at 15:54 Seril
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack
Session Variables C#
Overflow the company Business Learn more about hiring developers or posting ads with us how to use session in asp net for login and logout Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a
Session In Asp.net Mvc
community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up HttpContext.current.Session is cleared in error page up vote 1 down vote favorite I have made a http://stackoverflow.com/questions/8670020/asp-c-sharp-session-variable-object-reference-not-set-to-an-instance-of-an-o custom error page for my ASP.NET 4 application. I put the exception object in HttpContext.current.Session["CustomError"] but when the user is redirected to the error page HttpContext.current.Session["CustomError"] is null. I do it in CustomError class constructor like this: public CustomError(enExceptionType ExceptionType) : base(ExceptionMessage(ExceptionType)) { HttpContext.Current.Session["CustomError"] = this; } when I step over the code Session["Error"] contains the error object. any idea? UPDATE: I removed custom error page from web.config and added this http://stackoverflow.com/questions/5763195/httpcontext-current-session-is-cleared-in-error-page to glabal.asax: void Application_Error(object sender, EventArgs e) { if (Context.IsCustomErrorEnabled) { Response.Redirect("~/Error.aspx"); } } by stepping through this function I noticed that when an exception is thrown this function is called two time, the first time Session["CustiomError"] contains the error object but the second time its null. asp.net session-variables custom-error-pages share|improve this question edited Apr 23 '11 at 11:27 asked Apr 23 '11 at 8:36 nima 2,56422339 What do you want to achieve with the custom error session, If you want to log it, you can use the application_error event in global asax –Ivo Apr 23 '11 at 8:46 I want to display the error message of custom error object on error page. –nima Apr 23 '11 at 8:47 Can you put the code where you assign the exception? –Cristian Boariu Apr 23 '11 at 8:55 I updated the post and added the code –nima Apr 23 '11 at 9:04 @nima Are you sure that the redirect is done AFTER you fill in the exception value? –Cristian Boariu Apr 23 '11 at 9:20 | show 1 more comment 2 Answers 2 active oldest votes up vote 0 down vote Instead of using Response.redirect(URL) (which I assume you have in your code
resources Windows Server 2012 resources Programs MSDN subscriptions Overview Benefits Administrators Students Microsoft Imagine Microsoft Student Partners ISV Startups TechRewards Events Community Magazine Forums Blogs Channel 9 Documentation APIs and reference Dev centers Retired content Samples We’re https://msdn.microsoft.com/en-us/library/994a1482.aspx sorry. The content you requested has been removed. You’ll be auto redirected in 1 second. MSDN Library MSDN Library MSDN Library MSDN Library Design Tools Development Tools and Languages Mobile and Embedded Development .NET Development Office development Online Services Open Specifications patterns & practices Servers and Enterprise Development Speech Technologies Web Development Windows Desktop App Development TOC Collapse the table of content Expand the table of content This asp net documentation is archived and is not being maintained. This documentation is archived and is not being maintained. How to: Display Safe Error Messages Other Versions Visual Studio 2010 .NET Framework 4 Visual Studio 2008 .NET Framework 3.0 Visual Studio 2005 When your application displays error messages, it should not give away information that a malicious user might find helpful in attacking your system. For example, if your application in asp net unsuccessfully tries to log in to a database, it should not display an error message that includes the user name it is using. There are a number of ways to control error messages, including the following: Configure the application not to show verbose error messages to remote users. (Remote users are those who request pages while not working on the Web server computer.) You can optionally redirect errors to an application page. Include error handling whenever practical and construct your own error messages. In your error handler, you can test to see whether the user is local and react accordingly. Create a global error handler at the page or application level that catches all unhandled exceptions and routes them to a generic error page. That way, even if you did not anticipate a problem, at least users will not see an exception page. To configure the application to turn off errors for remote users In the Web.config file for your application, make the following changes to the customErrors element: Set the mode attribute to RemoteOnly (case-sensitive). This configures the application to show detailed errors only to local users (that is, to you, the developer). Optionally include a defaultRedirect attribute that points t