Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
ASP.NET 3.5 Basics If you are new to ASP or ASP.NET programming with version 3.5, this is the forum to begin asking questions. Please also see the Visual Web Developer 2008 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 3.5 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old December 5th, 2009, 03:20 AM
Friend of Wrox
Points: 1,469, Level: 15
Points: 1,469, Level: 15 Points: 1,469, Level: 15 Points: 1,469, Level: 15
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2009
Posts: 341
Thanks: 14
Thanked 3 Times in 3 Posts
Smile Paging with grid view

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......
  #2 (permalink)  
Old December 5th, 2009, 10:54 AM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Didn't I provide an answer to that earlier? You need to look at e.CommandName and only execute your code when it makes sense. I doubt you want to select a single item every time you want to page....

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
  #3 (permalink)  
Old December 5th, 2009, 11:17 AM
Friend of Wrox
Points: 1,469, Level: 15
Points: 1,469, Level: 15 Points: 1,469, Level: 15 Points: 1,469, Level: 15
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2009
Posts: 341
Thanks: 14
Thanked 3 Times in 3 Posts
Smile

yes sir, I do not regret from what you provided earlier but still having problem in paging. I also followed the link you provided.
OK I try to put the code in the e.CommandName
  #4 (permalink)  
Old December 5th, 2009, 11:27 AM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

You should *check* the CommandName and only execute the code if the command name is something you expect (e.g. not Sort or Page, but one you've set yourself):

Code:
 
Protected Sub GridView1_RowCommand1(ByVal sender As Object, ByVal e As GridViewCommandEventArgs) Handles GridView1.RowCommand
 
  If e.CommandName = "play1" Or e.CommandName = "download1" Then
    ' Only execute your code here
  End If
End Sub
Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
The Following User Says Thank You to Imar For This Useful Post:
sophia (December 5th, 2009)
  #5 (permalink)  
Old December 5th, 2009, 11:38 AM
Friend of Wrox
Points: 1,469, Level: 15
Points: 1,469, Level: 15 Points: 1,469, Level: 15 Points: 1,469, Level: 15
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2009
Posts: 341
Thanks: 14
Thanked 3 Times in 3 Posts
Smile

love you sir, I got your message from your first post. And Now paging is working fine.
But Can you please explain why it was getting me problem when I put the code outside the e.CommandName
Please reply this one
And of course thank you.......
  #6 (permalink)  
Old December 5th, 2009, 12:53 PM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

I provided that answer in my first post to your original qustion (now confusingly burried somewhere else in a separate thread): Paging

The RowCommand also fires when sorting and paging. In those cases your own code doesn't make sense to fire....

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
The Following User Says Thank You to Imar For This Useful Post:
sophia (December 5th, 2009)
  #7 (permalink)  
Old December 5th, 2009, 01:01 PM
Friend of Wrox
Points: 1,469, Level: 15
Points: 1,469, Level: 15 Points: 1,469, Level: 15 Points: 1,469, Level: 15
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2009
Posts: 341
Thanks: 14
Thanked 3 Times in 3 Posts
Smile

may be that, that time I was not able to understand you properly. Because sometimes we are not able to understand somethings at first time but on subsequent learning things become clear.
Anyway again thank you for replying my query.
All credit goes to you. Thank you once again sir. You really solved my query at one shot once again. Thank you..........
  #8 (permalink)  
Old December 5th, 2009, 01:05 PM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Yes, I agree. That's why I am not simply supplying you with the answer, but encourage you to search the web, read articles, and experiment. It's the only way to really understand these kind of topics....

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
  #9 (permalink)  
Old December 5th, 2009, 01:10 PM
Friend of Wrox
Points: 1,469, Level: 15
Points: 1,469, Level: 15 Points: 1,469, Level: 15 Points: 1,469, Level: 15
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2009
Posts: 341
Thanks: 14
Thanked 3 Times in 3 Posts
Smile

Thank you sir for encouraging me and what ever I do. Hope one day I will also be able to think like you and be able to help others as you do.
One request
Always keep smiling
Please........

Thank you once again.


Similar Threads
Thread Thread Starter Forum Replies Last Post
Paging Data grid view Prakash001 Visual Basic 2005 Basics 0 February 6th, 2008 04:07 AM
Data Grid View Paging in VB.Net Prakash001 Visual Studio 2005 0 January 30th, 2008 07:44 AM
Help Grid View Drop Down List, view all jskinner123 ASP.NET 2.0 Basics 0 November 25th, 2007 06:25 PM
Paging numbers at top of Grid rrhandle VB.NET 2002/2003 Basics 0 November 16th, 2005 11:45 AM
problem facing in paging data grid Asteroid ASP.NET 1.0 and 1.1 Basics 1 December 29th, 2004 03:39 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.