如何修复错误“索引超出范围。必须是非负数且小于集合的大小。参数名称:index [英] how do I fix the error "Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index

查看:131
本文介绍了如何修复错误“索引超出范围。必须是非负数且小于集合的大小。参数名称:index的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  Imports  CrystalDecisions.CrystalReports.Engine 
Imports MySql.Data.MySqlClient
公共 referralview

私有 Sub referralview_Load( ByVal sender As System。 Object ByVal e As System.EventArgs)句柄 MyBase .Load
Dim strsql As String
strsql = select * from report4其中cdate =& List.DataGridView1.SelectedRows( 0 )。单元格( 0 )。Value.ToString& 按cdate desc排序
Dim cmd 作为 MySqlCommand
Dim adpter 作为 MySqlDataAdapter
Dim dt As DataTable

使用 cmd
.CommandText = strsql
.Connection = List.connect
结束 使用

使用 adpter
.SelectCommand = cmd
。填充(dt)

结束 使用
Dim crystal As CrystalReport4
crystal .SetDataSource(dt)
CrystalReportViewer3.ReportSource = crystal
CrystalReportViewer3.Refresh()
结束 Sub
结束

解决方案

让我们看看: List.DataGridView1.SelectedRows 需要至少有一行才能获得任何行在下一步,同样,如果索引0处的行确实存在, List.DataGridView1.SelectedRows(0).Cells 需要至少有一个单元格允许您获取索引0处的任何单元格。在调试器下执行它并检查这些对象。然后修改你的代码,检查在索引之前访问集合元素之前索引的对象是否存在。



-SA

Imports CrystalDecisions.CrystalReports.Engine
Imports MySql.Data.MySqlClient
Public Class referralview

    Private Sub referralview_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim strsql As String
        strsql = "select *  from report4 where cdate = " & List.DataGridView1.SelectedRows(0).Cells(0).Value.ToString & " order by cdate desc"
        Dim cmd As New MySqlCommand
        Dim adpter As New MySqlDataAdapter
        Dim dt As New DataTable

        With cmd
            .CommandText = strsql
            .Connection = List.connect
        End With

        With adpter
            .SelectCommand = cmd
            .Fill(dt)

        End With
        Dim crystal As New CrystalReport4
        crystal.SetDataSource(dt)
        CrystalReportViewer3.ReportSource = crystal
        CrystalReportViewer3.Refresh()
    End Sub
End Class

解决方案

Let's see: List.DataGridView1.SelectedRows needs to have at least one row to allow you to get any row at index 0. On next step, likewise, if the row at index 0 does exist, List.DataGridView1.SelectedRows(0).Cells needs to have at least one cell to allow you to get any cell at index 0. Execute it under the debugger and inspect those objects. Then modify your code to check up that the objects indexed like that exist before accessing the collection elements by indices.

—SA


这篇关于如何修复错误“索引超出范围。必须是非负数且小于集合的大小。参数名称:index的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆