473,668 Members | 2,452 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Issue in deleting session ID from Dictionary object

62 New Member
Please help as this issue is driving us crazy...
Any idea would be of great help..

Application is running over IIS and I am getting error "
Index was outside the bounds of the array." on the Session_Start on line
Expand|Select|Wrap|Line Numbers
  1. AllSessions[Session.SessionID] = GetSession.GetNewSession(Context);

When I tried to catch the error I got random values for AllSessions.Cou nt. It touched 77 and after a day it came back on 66 and in an hour it touched 99.

Now, my question is why I am getting error even after removing session from Dictionary object under Session_End event.

Here is my global.asax file

Expand|Select|Wrap|Line Numbers
  1. <%@ Application Language="C#" %>
  2. <%@ Import Namespace="System.Collections.Generic" %>
  3.  
  4. <script runat="server">
  5.  
  6.     private static Dictionary<String, GetSession> AllSessions = new Dictionary<String, GetSession>();
  7.  
  8.     void Application_Start(object sender, EventArgs e) 
  9.     {
  10.         Context.Cache.Insert("Web.AllSessions", AllSessions);
  11.         // Code that runs on application startup
  12.  
  13.     }
  14.  
  15.     void Application_End(object sender, EventArgs e) 
  16.     {
  17.         //  Code that runs on application shutdown
  18.  
  19.     }
  20.  
  21.     void Application_Error(object sender, EventArgs e) 
  22.     { 
  23.         // Code that runs when an unhandled error occurs
  24.  
  25.     }
  26.  
  27.     void Session_Start(object sender, EventArgs e) 
  28.     {
  29.         try
  30.         {
  31. AllSessions[Session.SessionID] = GetSession.GetNewSession(Context);
  32.         // Code that runs when a new session is started
  33. }
  34. catch (Exception ex)
  35.         {
  36.         Web.Globals.WriteError("SessionInCache.GetNewSessionForCache(Context) -> " + ex.Message +  "REMOTE_HOST: " + Context.Request.ServerVariables["REMOTE_HOST"] + " SessionID " + Context.Session.SessionID + " Number of sessions " + AllSessions.Count);
  37.         }
  38.  
  39.     }
  40.  
  41.     void Session_End(object sender, EventArgs e) 
  42.     {
  43.         // Code that runs when a session ends. 
  44.         // Note: The Session_End event is raised only when the sessionstate mode
  45.         // is set to InProc in the Web.config file. If session mode is set to StateServer 
  46.         // or SQLServer, the event is not raised.
  47.  
  48.         AllSessions.Remove(Session.SessionID);
  49.  
  50.     }
  51.  
  52. </script>
  53.  
here is my GetSession class

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Web;
  5. using System.Web.Security;
  6. using System.Web.UI;
  7. using System.Web.UI.WebControls;
  8. using System.Web.UI.WebControls.WebParts;
  9. using System.Web.UI.HtmlControls;
  10.  
  11. /// <summary>
  12. /// Summary description for GetSession
  13. /// </summary>
  14. public class GetSession
  15. {
  16.     private String m_SessionId = null;
  17.     public String m_UserAgent = null;
  18.     public String m_RemoteHost = null;
  19.     public String m_RemoteAddr = null;
  20.     public String m_AcceptCharsets = null;
  21.     public String m_AcceptEncodings = null;
  22.     public String m_AcceptLanguage = null;
  23.     public String m_EmplId = null;
  24.     public String m_LoginName = null;
  25.     public DateTime m_SessionStart = DateTime.UtcNow;
  26.  
  27.     public GetSession()
  28.     {
  29.         //
  30.         // TODO: Add constructor logic here
  31.         //
  32.     }
  33.     public static GetSession GetNewSession(HttpContext Context)
  34.     {
  35.         GetSession mySession = null;
  36.         try
  37.         {
  38.  
  39.  
  40.             mySession = new GetSession();
  41.             mySession = new GetSession();
  42.             mySession.m_SessionId = Context.Session.SessionID;
  43.             mySession.m_UserAgent = Context.Request.ServerVariables["HTTP_USER_AGENT"];
  44.             mySession.m_RemoteHost = Context.Request.ServerVariables["REMOTE_HOST"];
  45.  
  46.             // Handle HTTP proxy forwarding
  47.             if (Context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null) mySession.m_RemoteAddr = Context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
  48.             else if (Context.Request.ServerVariables["HTTP_FORWARDED"] != null) mySession.m_RemoteAddr = Context.Request.ServerVariables["HTTP_FORWARDED"];
  49.             else mySession.m_RemoteAddr = Context.Request.ServerVariables["REMOTE_ADDR"];
  50.  
  51.             mySession.m_AcceptCharsets = Context.Request.ServerVariables["HTTP_ACCEPT_CHARSET"];
  52.             mySession.m_AcceptEncodings = Context.Request.ServerVariables["HTTP_ACCEPT_ENCODING"];
  53.             mySession.m_AcceptLanguage = Context.Request.ServerVariables["HTTP_ACCEPT_LANGUAGE"];
  54.  
  55.  
  56.  
  57.         }
  58.         catch (Exception ex)
  59.         {
  60.             Console.WriteLine(ex.Message.ToString());
  61.         }
  62.         return mySession;
  63.     }
  64. }
  65.  
Jun 1 '10 #1
1 2217
tlhintoq
3,525 Recognized Expert Specialist
The error means... If you have an array of 10 items, and you try to access item number 15... The item number (the index) was outside of the bounds (the limits) of the array.

You need to not just assume your values are good. For example if (value < maximum) then do your thing.

I can make some general suggestions that apply to good coding practice.
  • Assume that everything is broken, or at least not ideal.
  • Presume that the user is going to provide data in a format or manner that you just didn't expect. If you use a textbox for a number, the user will type "One".
  • Assume that hardware breaks in the middle of what you are doing, so you have to recover.
  • Take a few extra lines of code to get standards like the boot drive, the number thousands seperator etc. Don't assume that you have a C: drive or that a comma is the separator because not everyone is in America.
  • Check that files/folders exist, even if you just did a call to make it: You may not have permissions.
  • Don't assume the harddrive has room for what you are doing: They do fill up. Usually right in the middle of you writing to your log file.
  • Get used to placing breakpoints and walking through the code line by line. Double check EVERYTHING on every line. Keep the "Locals" and "Autos" windows open so you can see your values.
    • Put a breakpoint on the first line of the method causing trouble.
    • When the code stops there, walk through line by line with F-10.
    • Check the values of your assumptions (looking at the Locals and Automatic variable windows as well as hovering the mouse over the variables in the code (hothelp will popup).
  • Stop. Breath. Relax. Then reason out the problem. Cut it down by sections or halves. "The value was good here, then at this method it wasn't. Where did it go between 'A' and 'B'?"
  • Range check and validate values. Confirm that you didn't get a zero when you are only set to accept 1-10. Confirm your objects and values aren't null. Initialize them to a known default if possible. If a selection can be from 0-10, then initialize to -1: Now you have something to check for.

Example:
Expand|Select|Wrap|Line Numbers
  1. Graphics g = Graphics.FromImage(m_Undo);
Presumes that m_Undo must be good (not null)(actually exists)(not in use)(you have permissions)(do esn't time out when accessed). If that assumption fails so does the program, because you can't make anything from a file if the file is null. Get used to validating data and assumptions in your code if you want it to be robust. For example:
Expand|Select|Wrap|Line Numbers
  1. if (m_Undo != null)
  2. {
  3.    bool bSuccess = false;
  4.    // Do your thing here, for example:
  5.    if (myObject != null) bSuccess = true;
  6.    // or
  7.    if (denominator > 0) bSuccess = true;
  8.    // or
  9.    if (MyFunctionReturn != Failed) bSuccess = true;
  10.    // Hurray, your thing worked!
  11.  
  12.    if (bSuccess)
  13.    {
  14.       // Then do this other thing if it worked
  15.    }
  16.    else
  17.    {
  18.       // Then do the failure recovery part / user failure message
  19.    }
  20.  
  21.    return bSuccess; // If you want the calling method to know if it worked.
  22. }
Jun 1 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

6
6325
by: Johnny Klunk | last post by:
Wondering if someone can give me a hand with something that I'm sure is really easy - but damned if I know what I'm doing wrong. I'm trying to read the contents of a database into an ASP dictionary object. However I'm getting the error Microsoft VBScript runtime error '800a01c9' This key is already associated with an element of this collection /test.asp, line 21 There's definately no repeated data in either column, it's currently...
2
5302
by: Ryan Malone | last post by:
Passing Dictionary object byref Ive created an ASP class that uses a dictionary object which is filled from a recordset. It passes the object to the propterty of another ASP class byref: Public Property Let dicReplaceVars(byref vdicReplaceVars) set p_ReplaceVars = vdicReplaceVars End Property
8
5576
by: Rodd Snook | last post by:
I have an application which makes extensive use of the Scripting.Dictionary object. I'm not doing anything silly like putting them outside the page scope -- just creating quite a few of them and stuffing quite a lot of data (from and MS SQL database) into them. On Windows 2000 server, everything is fine. If the data structures get really big it slows down, but for normal operation it's no problem. Recently our hosting provider moved to...
5
2088
by: TWiSTeD ViBE | last post by:
Hi, While pouring over some code I've discovered a previous developer heavily uses the "dictionary" object. Whilst I see some of the advantages of using this system It's something I've not used myself so am not sure of the limitations. We are about to widen the scope of the website it's being used on to a WorldWide system - greatly increasing the number of users that will be using the website.
26
4050
by: Alan Silver | last post by:
Hello, I have a server running Windows Server 2003, on which two of the web sites use the MegaBBS ASP forum software. Both sites suddenly developed the same error, which seems to be connected to the dictionary object. After some tinkering, I whittled it down to the following (complete) ASP... <%@ CodePage=65001 Language="VBScript"%>
1
9249
by: john wright | last post by:
I have a dictionary oject I created and I want to bind a listbox to it. I am including the code for the dictionary object. Here is the error I am getting: "System.Exception: Complex DataBinding accepts as a data source either an IList or an IListSource at System.Windows.Forms.ListControl.set_DataSource(Object value)
4
2319
by: Betina Andersen | last post by:
I have a dictionary object, then I create a new dictionary object and sets it equal to my original, then I pass the new dictionary object to a function that changes some of my values - but then my original dictionary also gets changed and that was not the intention, can someone explain to me why it behaves that way and how do I avoid it, så I van have different dictionary objects? Thanks Betina
2
2967
by: maynard | last post by:
I have defined a template class (tree data structure) that uses dynamic memory and has properly implemented ctor's, dtor and assignment operator. I can observe the address of my tree object prior to the destructor being called, and then the address once inside the destructor...they're different! The following calls are on the stack between the call to my destructor and the actual destructor itself: `eh vector destructor iterator'(void...
2
6252
by: J055 | last post by:
HI Can someone tell me what is wrong with this? The first 'if' condition works but the 'else' condition seems to get the right Count but the contents of the key and items are lost. Set dic_qs = CreateObject("Scripting.Dictionary") If Request.QueryString("new") = "1" Then
4
1886
AnuSumesh
by: AnuSumesh | last post by:
Hi I want that if user is logged in on one machine and trying to log in another machine also then he should not be allowed to log in again. For that-- i am creating Dictionary object on application start event and saving username and cookie value in that after user log in. and whenever user login , i m checking for existing enteries in dictionary, if dictionary contis entry for user then he is not allowed to login again else allowed to...
0
8462
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8893
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8586
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7401
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6209
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5681
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4205
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2792
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2026
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.