指数超出范围。必须是非负数且小于集合的大小。 (网格视图) [英] Index was out of range. Must be non-negative and less than the size of the collection. (GridView)

查看:100
本文介绍了指数超出范围。必须是非负数且小于集合的大小。 (网格视图)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我有一个 GridView ,它显示名为content的SQL-Server 2012表中的所有文件。每当我点击下载按钮时,都应该删除该文件。但是,我收到了这个错误:





Hi,

I have a GridView that displays all the files in an SQL-Server 2012 table called content. Whenever I click the ''Download'' button, the file should be deleted. However, I am getting this error:


Index was out of range. Must be non-negative and less than the size of the collection.





这是代码:





This is the code:


    Protected Sub lnkDownload_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim lnkbtn As LinkButton = TryCast(sender, LinkButton)
        Dim gvrow As GridViewRow = TryCast(lnkbtn.NamingContainer, GridViewRow)

        Dim fileid As Integer = Convert.ToInt32(GridView1.DataKeys(gvrow.RowIndex).Value.ToString())
        
        Dim con As New SqlConnection("Data Source=BRIAN-PC\SQLEXPRESS;Initial Catalog=master_db;Integrated Security=True;")



        Using cmd As New SqlCommand()
            cmd.CommandText = "Select content_name, content_type, content_file from content where content_id=@Id"
            cmd.Parameters.AddWithValue("@Id", SqlDbType.Int).Value = 1
            cmd.Connection = con
            con.Open()

            Dim dt As DataTable = GetData(cmd)

            If dt IsNot Nothing Then

                download(dt)

            End If
           
        End Using

    End Sub


    Public Function GetData(ByVal cmd As SqlCommand) As DataTable

        Dim dt As New DataTable

        Dim strConnString As String = System.Configuration.ConfigurationManager.ConnectionStrings("ConnStringDb1").ConnectionString()

        Dim con As New SqlConnection(strConnString)

        Dim sda As New SqlDataAdapter

        cmd.CommandType = CommandType.Text

        cmd.Connection = con

        Try

            con.Open()

            sda.SelectCommand = cmd

            sda.Fill(dt)

            Return dt

        Catch ex As Exception

            Response.Write(ex.Message)

            Return Nothing

        Finally

            con.Close()

            sda.Dispose()

            con.Dispose()

        End Try

    End Function


    Protected Sub download(ByVal dt As DataTable)

        Dim bytes() As Byte = CType(dt.Rows(0)("Data"), Byte())

        Response.Buffer = True

        Response.Charset = ""

        Response.Cache.SetCacheability(HttpCacheability.NoCache)

        Response.ContentType = dt.Rows(0)("ContentType").ToString()

        Response.AddHeader("content-disposition", "attachment;filename=" & dt.Rows(0)("Name").ToString())

        Response.BinaryWrite(bytes)

        Response.Flush()

        Response.End()

    End Sub




End Class





有人能告诉我如何解决这个问题吗?



Can anyone tell me how to fix this problem please?

推荐答案

这种异常是最容易调试的异常之一。始终使用调试器。要修复,请检查计数长度)的集合或数组:您的索引应该大于或等于0但不到数 - 1 (或长度 - 1 )。



在你的情况下,引起我注意的第一个问题是 dt.Rows(0);它 dt.Rows 有0行,索引0超出范围。







在OP澄清之后:



This kind of exception is one of the easiest to debug. Always use the debugger. To fix, check up the collection or array for Count (Length): your index should be more or equal to 0 but less then Count - 1 (or Length - 1).

In your case, the first problem which caught my eye is dt.Rows(0); it dt.Rows has 0 rows, index 0 is out of range.



After OP''s clarification:

The some problem goes with the line
Convert.ToInt32(GridView1.DataKeys(gvrow.RowIndex).Value.ToString())



如果出于任何原因, GridView1.DataKeys 的键数少于或等于 gvrow.RowIndex ,这将是同样的例外。



-SA


If, by any reason, GridView1.DataKeys has less or equal number of keys, than gvrow.RowIndex, it will be the same exception.

—SA


这篇关于指数超出范围。必须是非负数且小于集合的大小。 (网格视图)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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