在服务器中获取“索引超出数组范围”异常,本地工作正常

本文关键字:异常 工作 范围 数组 获取 服务器 索引 | 更新日期: 2023-09-27 18:36:13

我在服务器中收到此异常:

"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

但是在本地运行代码时,我没有收到任何异常或错误。知道是什么原因造成的吗?

以下是Page_Load方法的代码副本:

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

在服务器中获取“索引超出数组范围”异常,本地工作正常

好吧,您的堆栈跟踪指向Page_Load事件,并且因为我只能看到对字典/数组的一个引用,因此问题很可能在那里。

此行

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

很可能是问题所在。请检查您的应用程序设置,并可能创建一个仅用于一个目的的测试文件 - 访问应用程序设置并显示分配给密钥"smsDBconnection"的值以确保它正常工作。如果它有效,那么您目前没有向我们展示一些不同的东西。

当您尝试通过不存在的索引访问数组的元素时,您收到的错误是典型的错误消息。例如,当您的集合有 5 个元素并且您尝试引用第 6 个元素时。