473,508 Members | 2,227 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.Count. 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 2213
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)(doesn'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
6319
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...
2
5283
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: ...
8
5560
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...
5
2078
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...
26
4021
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...
1
9237
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...
4
2309
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...
2
2953
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...
2
6249
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...
4
1876
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...
0
7132
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7336
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,...
0
7504
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5640
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,...
1
5059
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...
0
4720
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...
0
3211
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...
0
3196
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1568
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 ...

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.