Click here to Skip to main content
15,905,508 members
Please Sign up or sign in to vote.
1.36/5 (3 votes)
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:

VB

    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?
Posted
Updated 10-Apr-13 9:41am
v3
Comments
Richard C Bishop 10-Apr-13 15:45pm    
What line is it throwing this exception?
Member 9978591 10-Apr-13 15:50pm    
Dim fileid As Integer = Convert.ToInt32(GridView1.DataKeys(gvrow.RowIndex).Value.ToString())
Sergey Alexandrovich Kryukov 10-Apr-13 15:46pm    
Use the debugger.
—SA
Member 9978591 10-Apr-13 15:50pm    
I used it. index is '0' at Dim fileid As Integer = Convert.ToInt32(GridView1.DataKeys(gvrow.RowIndex).Value.ToString())
Sergey Alexandrovich Kryukov 10-Apr-13 15:47pm    
Check up dt.Rows(0), id dt.Rows has 0 rows, index 0 is out of range.
—SA

1 solution

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.

[EDIT]

After OP's clarification:

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

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

—SA
 
Share this answer
 
v3
Comments
Maciej Los 10-Apr-13 16:06pm    
+5!
Sergey Alexandrovich Kryukov 10-Apr-13 16:41pm    
Thank you, Maciej.
—SA
CPallini 10-Apr-13 16:28pm    
5.
Sergey Alexandrovich Kryukov 10-Apr-13 16:41pm    
Thank you, Carlo.
—SA
[no name] 10-Apr-13 17:22pm    
+5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900