Hello friends, I am having problem in paging with the grid view. The problem is that suppose there are N no. of pages then moving in between from page no. 1 to N-1 works fine but as soon as I move to page no. N and try to move some previous page then it generates error telling that
Code:
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
I am using
VB.NET and grid view with SqlDataSource. Also using RowCommand Event to perform some task on commands.
My code is something is like this
Code:
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" GridLines ="Horizontal" EmptyDataText="Sorry,There Are No Data Records To Display." AutoGenerateColumns="False" DataKeyNames="Id" DataSourceID="SqlDataSource1">
<Columns>
<asp:ButtonField ButtonType="Button" CommandName="play1" HeaderText="Play" Text="Play" />
<asp:BoundField DataField="SongName" HeaderText="Song Name" SortExpression="SongName" />
<asp:ButtonField ButtonType="Button" CommandName="download1" HeaderText="Download" Text="Download" />
</Columns>
</asp:GridView>
And the code behind file is like
Code:
Protected Sub GridView1_RowCommand1(ByVal sender As Object, ByVal e As GridViewCommandEventArgs) Handles GridView1.RowCommand
con.Open()
Dim play_index_db As Integer = GridView1.DataKeys(e.CommandArgument).Values("Id").ToString()
Dim s As String = "select SongPath from Music where Id = " & play_index_db.ToString()
Dim cmd As New SqlCommand(s, con)
Dim p As String = Convert.ToString(cmd.ExecuteScalar())
If e.CommandName = "play1" Then
code goes here.................
End If
End If
If e.CommandName = "download1" Then
code goes here...........
End If
con.Close()
End Sub
Error is generated on the line
Code:
Dim s As String = "select SongPath from Music where Id = " & play_index_db.ToString()
whenever I move to page No. N to some previous page.
any suggestion is respected and hope soon I'll get reply
Thank you......