繁体   English   中英

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

[英]Getting “Index was outside the bounds of the array” exception in server, local works fine

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

"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"))

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM