views:

1701

answers:

3

I have a gridview that is databound in the code-behind using a stored procedure. I am handling the Paging event in the code as well, but whenever I click on a page number, I keep getting the empty data template instead of more rows. Any suggestions?

EDIT: I am re-binding the data source of the gv after I change the page index.

Here is my code - I have a dropdown list that determines what the data source is:

Protected Sub ddlProjectForm_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlProjectForm.SelectedIndexChanged
    Dim strProjectFormID As String = Me.ddlProjectForm.SelectedValue
    Dim conn As New SqlConnection(WebConfigurationManager.ConnectionStrings("Conn").ConnectionString)
    Dim cmd As New SqlCommand()
    Dim da As New SqlDataAdapter
    Dim ds As New DataSet

    If strProjectFormID <> "Select" Then
        Try
            Using conn
                conn.Open()

                With cmd
                    .Connection = conn
                    .CommandType = CommandType.StoredProcedure
                    .CommandText = "sp_GetAllFormData"
                    .Parameters.AddWithValue("@projectFormID", strProjectFormID)
                End With

                da.SelectCommand = cmd
                da.Fill(ds)

                Me.gvAllSentData.DataSource = ds.Tables(0)
                Me.gvAllSentData.DataBind()
                Me.gvAllSentData.Visible = True
            End Using
        Catch sqlEx As SqlException
            Dim newError As New ErrorLogger(Me.Page.Title, sqlEx.Message, Session("UserName"))
            newError.LogError()

            Trace.Write(sqlEx.Message)
            Me.lblBadFeedback.Visible = True
            Me.lblBadFeedback.Text = "We're sorry - an error has occurred.  It has been logged and will be reviewed by the site admin."
        Catch ex As Exception
            Dim newError As New ErrorLogger(Me.Page.Title, ex.Message, Session("UserName"))
            newError.LogError()

            Trace.Write(ex.Message)
            Me.lblBadFeedback.Visible = True
            Me.lblBadFeedback.Text = "We're sorry - an error has occurred.  It has been logged and will be reviewed by the site admin."
        End Try
    Else
        Me.gvAllSentData.DataSource = Nothing
        Me.gvAllSentData.Visible = False
    End If

End Sub

Protected Sub gvAllSentData_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvAllSentData.PageIndexChanging
    Me.gvAllSentData.PageIndex = e.NewPageIndex
    Me.gvAllSentData.DataBind()
End Sub
A: 

As silly as it sounds, are you remembering to call your databinding code again? When the paging occurs, a post back is made, and the datasource for your GridView is lost, so you will need to rebind the data again so that the GridView can load the appropriate data based on the page.

I think it was my second or third ASP.NET app I had written before I remembed to re-databind the first time through when writing code 8^D

Dillie-O
Yes, I am binding after setting the new page index.
Brian Lewis
Hmm, can edit your initial question and post your code into it there then?
Dillie-O
+2  A: 

You are rebinding an empty datasource. Your code should read:

Protected Sub gvAllSentData_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvAllSentData.PageIndexChanging
    Me.gvAllSentData.PageIndex = e.NewPageIndex
    Me.gvAllSentData.DataSource = __The_Data_To_Bind__
    Me.gvAllSentData.DataBind()
End Sub
Gavin Miller
A: 

It does sound like a databinding issue. Maybe your datasource is empty after the postback?

Do other events like sorting work?

Can you step through the binding part of the code after the postback and confirm the data is all there?

Do you have any code you can post? Maybe you should create a test harness containing just the parts of the code that aren't working to try and track down the problem.

Robin Day