-4

I am getting this exception in Server:

"Index was outside the bounds of the array."

[IndexOutOfRangeException: Index was outside the bounds of the array.]
   pcdirectory.facultyPage.Page_Load(Object sender, EventArgs e) +2340
   System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14
   System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35
   System.Web.UI.Control.OnLoad(EventArgs e) +91
   System.Web.UI.Control.LoadRecursive() +74
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2207

But while running code in local I don't get any exception or error. Any idea what would be causing this?

Here is the copy of code for Page_Load method:

Sub Page_Load(sender As Object, e As EventArgs)
    if not isPostBack then
        dim smsDBConnection as new SqlConnection(AppSettings("smsDBconnection"))
        dim SQL as new stringBuilder()
        dim command as new sqlCommand()
        dim reader as sqldataReader
        dim directoryGlobal as new directoryGloablFunctions()

        try
            command.connection = smsDBConnection
            command.commandType = commandType.storedProcedure
            smsDBConnection.open()

            SQL.append("get_all_Numbers")
            command.commandText = SQL.toString()

            reader = command.executeReader()

            drpClass.datasource = reader
            drpClass.dataTextField = "Number"
            drpClass.dataValueField = "Number"
            drpClass.dataBind()
        catch ex as exception
            directoryGlobal.sendErrorMessageEmail(ex.message,SQL.toString(),ex.stackTrace,"DIRECTORY ERROR")
            directoryGlobal.messageBox("An error has occurred on this page and been reported to the administrator." & vbcrlf & "Please try again later or contact technical support",me)
            smsDBConnection.close()
        end try

        smsDBconnection.Close()
    end if
end sub
WeSt
  • 2,628
  • 5
  • 22
  • 37
TechPro
  • 331
  • 1
  • 10
  • 29
  • 1
    Care to show the code in your `Page_Load` method? – siride Jan 05 '15 at 18:31
  • Given that you have told us nothing about your code, no. No idea what could be causing this aside from an out-of-bounds index. – JLRishe Jan 05 '15 at 18:32
  • possible duplicate of [ArgumentOutOfRangeException : Index was out of range](http://stackoverflow.com/questions/600308/argumentoutofrangeexception-index-was-out-of-range) – Dgan Jan 05 '15 at 18:32
  • Wrap your code in the Page Load method in a try catch and figure out what isn't populating. Perhaps you are getting some data that you can't access from the server but you can locally, for example. – TheGeekYouNeed Jan 05 '15 at 18:34
  • @siride: I have pasted the copy of the Page_Load method above. – TechPro Jan 05 '15 at 19:24
  • You are creating a `smsDBConnection` on every page load and it's never used. Besides, that line is not inside a try-catch, so you could be having an exception there as well. If an exception occurs, `DBconnection.Close()` is called twice. Place that line in a `finally` block. – Andrew Jan 05 '15 at 20:44
  • What is `drpClass`? Where does `DBConnection` come from? – siride Jan 05 '15 at 21:28
  • @siride: drpClass is defined like this in code: Protected drpClass As System.Web.UI.WebControls.DropDownList Sorry, DBConnection was typo, I corrected it, It's smsDBConnection. – TechPro Jan 05 '15 at 21:41
  • What's going on in `DirectoryGlobalFunctions`? That looks like custom code which could contain an array reference. – siride Jan 06 '15 at 02:07
  • @siride: Directory Global doesn't contain any array reference. The only other place besides than above piece of code where globalfunction is used is: Public Class directoryGloablFunctions Public Sub New() End Sub – TechPro Jan 06 '15 at 15:22
  • @TechPro: are you sure? What's going on in `directoryGlobal.sendErrorMessageEmail`? – siride Jan 06 '15 at 16:49
  • Without the line number of the error, this will be pretty hard to debug. Can you deploy with a debug build so that we can get line numbers? – siride Jan 06 '15 at 16:49
  • @siride: In local build, I don't get any error. Page comes just fine. In Server, I checked in IIS log file, I just see error status code 500 0 0 15. But how to enable to see error line number in Server side any idea? – TechPro Jan 06 '15 at 17:01
  • I figured out, when basic authentication is enabled and anonymous authentication is disabled, application works just fine, but we need to disable basic authentication to make Single Sign On to work. Any Idea what I can try to make basic authentication disabled without application to crash? – TechPro Jan 13 '15 at 02:36

1 Answers1

0

Well, your stacktrace points to Page_Load event and because I can see only one reference to a dictionary/array, the problem is most likely there.

This line

dim smsDBConnection as new SqlConnection(AppSettings("smsDBconnection"))

is most probably the problem. Please check your AppSettings and maybe create a test file that will serve only one purpose - access the AppSettings and display a value assigned to key "smsDBconnection" to be sure it works. If it works, there's something different you're not showing us at the moment.

The error you're getting is a typical error message when you try to access an element of an array by an index that doesn't exist. For instance when your collection has 5 elements and you try to reference the 6th.

walther
  • 13,466
  • 5
  • 41
  • 67
  • It wouldn't be caused by a dictionary lookup issue, as that generates a different exception (`KeyNotFoundException`) or a list (`ArgumentOutOfRangeException`). – siride Jan 06 '15 at 02:06
  • I figured out, when basic authentication is enabled and anonymous authentication is disabled, application works just fine, but we need to disable basic authentication to make Single Sign On to work. Any Idea what I can try to make basic authentication disabled without application to crash? – TechPro Jan 13 '15 at 02:34