Wrox Programmer Forums
|
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 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
 
Old February 11th, 2008, 10:41 AM
Friend of Wrox
 
Join Date: Mar 2007
Posts: 205
Thanks: 4
Thanked 0 Times in 0 Posts
Default [2005] GridView sort problem

I am trying to sort a GridView and get error:

An unhandled exception of type 'System.StackOverflowException' occurred in System.Web.dll
Here is the code:

Code:
Protected Sub GridView1_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles GridView1.Sorting

   If Page.IsPostBack = True Then
     Dim mySortExpression As String = ""
     mySortExpression = e.SortExpression.ToString
     Dim mySortDirection As SortDirection = SortDirection.Ascending
     GridView1.Sort(mySortExpression, mySortDirection)
     GridView1.DataBind()
   End If
End Sub
 
Old February 11th, 2008, 02:45 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

You got yourself in a loop here. When the GridView is about to sort its own data, it fires the Sorting event. Then inside this event, you get fresh data, rebind the GridView and tell it to sort. This in turn causes the GridView to trigger its Sorting event which you handle by getting fresh data, rebinding the GridView and telling it to sort. This in turn causes the GridView to trigger its Sorting event which you handle by getting..... oh, wait, I'm sure you get the idea.... ;)

So, generally, you don't need to manually write code for this; the GridView "just sorts" in many circumstances. So, most likely, you can just remove all the code, including the Sorting handler.

If you *must* manually sort the data, then you need to set e.Cancel to true to signal that you have taken care of sorting yourself.

Hope this helps,

Imar
---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.
 
Old February 11th, 2008, 04:27 PM
Friend of Wrox
 
Join Date: Mar 2007
Posts: 205
Thanks: 4
Thanked 0 Times in 0 Posts
Default

This is the error if I remove the code:

The GridView 'GridView1' fired event Sorting which wasn't handled.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Web.HttpException: The GridView 'GridView1' fired event Sorting which wasn't handled.



 
Old February 11th, 2008, 04:40 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Where does your data come from and how does the code that retrieves it look?

Imar


---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
 
Old February 11th, 2008, 05:27 PM
Friend of Wrox
 
Join Date: Mar 2007
Posts: 205
Thanks: 4
Thanked 0 Times in 0 Posts
Default

Here is the code that retrives the data. Data comes from an access table and GridView is displayed perfectly. I also do paging and that works (see code for paging at the end)

Code:
Public Function GetInventory(ByVal myDouble As Double) As Object

        Dim mySelectString As String = "Select Diameter, Pitch, Rotation, Bore, Blades, Material, Manufacturer," + _
                                              "Model, Quantity, Condition from PropInventory where Sold = False and Diameter >= " & + _
                                              myDouble & " Order by Diameter"

        Dim ConnectionString As String = ConfigurationManager.AppSettings("ConnectionString")
        Dim myOleDbConnection = New OleDbConnection(ConnectionString)

        myOleDbConnection.Open()

        Dim myOleDbCommand As New OleDbCommand

        myOleDbCommand.CommandText = mySelectString

        myOleDbCommand.Connection = myOleDbConnection

        Dim myOleDbDataAdapter As New OleDbDataAdapter(myOleDbCommand)
        Dim myDataSet As New Data.DataSet
        Dim myDataTable As New Data.DataTable

        myOleDbDataAdapter.Fill(myDataSet, ("myDataTable"))

        'Fill manual rows Uses Number of Rows from table and grid page size
        Dim ShortFullPage As Double = myDataSet.Tables("myDataTable").Rows.Count Mod GridView1.PageSize
        Dim ManualRowsNeeded As Integer = GridView1.PageSize - ShortFullPage

        Dim i As Integer
        For i = 0 To ManualRowsNeeded - 1
            myDataSet.Tables("myDataTable").Rows.Add(myDataSet.Tables("myDataTable").NewRow())
        Next i

        myObject = myDataSet

        Return myObject

    End Function


Code for paging:

Code:
Protected Sub GridView1_PageIndexChanging1(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridView1.PageIndexChanging

        If Page.IsPostBack = True Then

            Dim myDouble As Double = 0

            GetInventory(myDouble)
            Dim myDataSet As New Data.DataSet

            myDataSet = myObject
            'DisplayCurrentPage()
            GridView1.PageIndex = e.NewPageIndex
            GridView1.DataSource = myDataSet
            GridView1.DataBind()

        End If

    End Sub
 
Old February 11th, 2008, 06:05 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Why are you returning an object from your Get* method? I think if you'd return the DataSet directly (and type the method as such) sorting is supported.

In fact, you may be better off creating a SqlDataSource or an AccessDataSource in markup and let .NET handle this for you. It looks like you're writing way too much code manually for things ASP.NET can handle for you automatically....

Imar


---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
 
Old February 12th, 2008, 09:42 AM
Friend of Wrox
 
Join Date: Mar 2007
Posts: 205
Thanks: 4
Thanked 0 Times in 0 Posts
Default

I agree, it would be easier to create a DataSource control and let asp handle it. However, writing the code behind is a good learning curve even if I constantly run into obstacles like these. Again thank you.

 
Old February 12th, 2008, 10:26 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

You're absolutely right. It's the best way to learn.

If you go down that route, you might as well look into a "proper" way to do things. Instead of writing code that works with DataSets, you're better off using object oriented techniques. This article series gives you a nice introduction: http://imar.spaanjaars.com/QuickDocId.aspx?quickdoc=416

Hope this helps,

Imar


---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004





Similar Threads
Thread Thread Starter Forum Replies Last Post
GridView - initial load and sort direction snufse ASP.NET 2.0 Basics 0 October 30th, 2008 08:20 PM
[2005] GridView - empty cell shows no border snufse ASP.NET 2.0 Basics 2 July 29th, 2008 02:49 PM
Sort with gridview smithsf22 ASP.NET 2.0 Basics 1 December 9th, 2006 09:17 AM
GridView Sort (Top Urgent ) msbsam ASP.NET 2.0 Professional 0 December 6th, 2006 02:34 AM
GridView sort without DataSet? drasko ASP.NET 2.0 Professional 1 October 3rd, 2006 06:57 AM





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