Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Development Tools
 ASP.NET
 Datagrid sorting with PageIndexChanged

Author  Topic 

bubberz
Constraint Violating Yak Guru

289 Posts

Posted - 2006-03-29 : 14:35:18
I'm trying to allow sorting via the column headers in my datagrid. I'm running into issues when the user clicks the page numbers. The error shows in my try/catch section, and says "Object reference not set to an instance of an object"

I tried to add another parameter in the PageIndexchanges even handler:

f As DataGridSortCommandEventArgs

...but an error message during compiling the project said the signature was different for Handles DGD.PageIndexChanged

Here's the code:

***************

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
If Not Page.IsPostBack Then
Call BuildDGDel("ActID")
End If
End Sub
Sub BuildDGDel(ByVal SortFieldName As String)
'Create the command item
scon1.Open()
Dim strSQLDG As String
strSQLDG = "SELECT [DelNbr], ActId, [DelDesc], [Date] FROM WP_Del ORDER BY " & SortFieldName
Dim objCmd As New SqlCommand(strSQLDG, scon1)

'Create the DataAdapter
Dim DA As New SqlDataAdapter
DA.SelectCommand = objCmd

'Populate the DataSet and the connection
Dim DS As New DataSet
DA.Fill(DS)
scon1.Close()

'Specifiy DataSource and call DataBind()
DGD.DataSource = DS
DGD.DataBind()
End Sub

Private Sub DGD_PageIndexChanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles DGD.PageIndexChanged
DGD.CurrentPageIndex = e.NewPageIndex
Call CheckSorting()
End Sub
Sub CheckSorting()
Dim e As DataGridSortCommandEventArgs
Try
Call BuildDGDel(e.SortExpression)
Catch ex As Exception
lblStatus.Text = "Error on line 80: " & ex.Message
End Try

End Sub
Sub SortDataGrid(ByVal sender As Object, ByVal e As DataGridSortCommandEventArgs)
BuildDGDel(e.SortExpression)
End Sub

End Class

twhelan1
Yak Posting Veteran

71 Posts

Posted - 2006-03-29 : 16:40:29
quote:
Originally posted by bubberz



Private Sub DGD_PageIndexChanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles DGD.PageIndexChanged
DGD.CurrentPageIndex = e.NewPageIndex
Call CheckSorting()
End Sub
Sub CheckSorting()
Dim e As DataGridSortCommandEventArgs
Try
Call BuildDGDel(e.SortExpression)
Catch ex As Exception
lblStatus.Text = "Error on line 80: " & ex.Message
End Try

End Sub
Sub SortDataGrid(ByVal sender As Object, ByVal e As DataGridSortCommandEventArgs)
BuildDGDel(e.SortExpression)
End Sub

End Class




The line marked in red.. you can't do that. A DataGridSortcommandeventArgs variable can't just be dim'd and then used like that.

Take a look at:
http://www.xefteri.com/articles/show.cfm?id=19
and see if that's any help.

~Travis
Go to Top of Page

bubberz
Constraint Violating Yak Guru

289 Posts

Posted - 2006-03-29 : 17:31:00
Thanks Travis!

I've got the following code, and it's acting weird.....I'm trying to write things to the page labels, but haven't figured it out yet.
If I click the P3ActivityID link, then the SQL Query has no "ASC" as a suffix. If I click it again, then it does. click it again, then it goes to DESC. I should just have to click it once, and the query then has DESC. This does work for the date column. These are the only two that are sortable in the datagrid.

When the page loads, there is in fact an "ASC" suffix for the query.

Here's the code behind:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
If Not Page.IsPostBack Then
ViewState("P3ActivityIDSortDirection") = "ASC"
ViewState("DateSortDirection") = "ASC"
ViewState("SortExpr") = "P3ActivityId ASC"
Call BuildDGDel(ViewState("SortExpr"))
SwapDirection("P3ActivityIDSortDirection")
End If

End Sub
Sub SwapDirection(ByVal viewStateKey As String)
If ViewState(viewStateKey) = "ASC" Then
ViewState(viewStateKey) = "DESC"
Else
ViewState(viewStateKey) = "ASC"
End If
End Sub
Sub BuildDGDel(ByVal SortExpr As String)
'Create the command item
scon1.Open()
Try
Dim strSQLDG As String
'Dim strRQW As String = Server.HtmlEncode(Request.QueryString("WPPID"))
strSQLDG = "SELECT [DelNumb], P3ActivityId, [DelDes], [Date] FROM "
strSQLDG &= "WP_Del ORDER BY " & SortExpr
Dim objCmd As New SqlCommand(strSQLDG, scon1)

'Create the DataAdapter
Dim DA As New SqlDataAdapter
DA.SelectCommand = objCmd

'Populate the DataSet and the connection
Dim DS As New DataSet
DA.Fill(DS)
'Specifiy DataSource and call DataBind()
DGD.DataSource = DS
DGD.DataBind()
lblStatus2.Text = strSQLDG.ToString
Catch ex As Exception
lblStatus.Text &= "Line 79: " & ex.Message
Finally
scon1.Close()
End Try
End Sub

Private Sub DGD_PageIndexChanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles DGD.PageIndexChanged
DGD.CurrentPageIndex = e.NewPageIndex
BuildDGDel(ViewState("SortExpr"))
End Sub
Sub SortDataGrid(ByVal sender As Object, ByVal e As DataGridSortCommandEventArgs) Handles DGD.SortCommand
BuildDGDel(e.SortExpression & " " & ViewState(e.SortExpression & "SortDirection"))
lblStatus.Text = "Line 95: " & e.SortExpression.ToString()
SwapDirection(e.SortExpression & "SortDirection")
End Sub

Private Sub DGD_SortCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles DGD.SortCommand
ViewState("SortExpr") = e.SortExpression
DGD.CurrentPageIndex = 0
BuildDGDel(ViewState("SortExpr"))
'BuildDGDel(e.SortExpression & " " & ViewState(e.SortExpression & "SortDirection"))
End Sub
Go to Top of Page

twhelan1
Yak Posting Veteran

71 Posts

Posted - 2006-03-30 : 09:27:12
First, you have two functions that both Handle DGD.SortCommand. One of them should probably go . Then, follow your logic with your calls to SwapDirection, consider how the program flows and what is happening to your SortDirection viewstates. What is it when the page first loads and why? What happens when you click the sort button?



~Travis
Go to Top of Page
   

- Advertisement -