Datgrid with paging

I am very new to ASP.net programming and have the last couple of days been lost in a datagrid+paging problem.
I am abel to list the first 5 paged records from the database, but when I press the Next link on the datagrid, everything disappears.
No error message or no new list of records.
Could someone be kind enough to take a look at my code, and point me in the right direction??

The code:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If Not IsPostBack Then
            dbGrid.DataSource = bindData()
            dbGrid.DataBind()
        End If

End Sub

Private Sub ImageButton1_Click(ByVal sender As System.Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ImageButton1.Click
        bindData()
End Sub

Function bindData() As ICollection
   Dim myConnection As SqlConnection
   Dim myCommand As SqlDataAdapter
   myConnection = New SqlConnection("server=xxxxxxx;uid=xxxx;pwd=xxxxx;database=xxxxx;")
   myCommand = New SqlDataAdapter("SELECT var1,var2,3,var4,var5 FROM Table1", myConnection)

   Dim ds As DataSet = New DataSet()
   myCommand.Fill(ds)
   dbGrid.DataSource = ds
   dbGrid.DataBind()
End Function

Sub dbGrid_PageIndexChanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles dbGrid.PageIndexChanged
   dbGrid.CurrentPageIndex = e.NewPageIndex
   dbGrid.DataSource = bindData()
   dbGrid.DataBind()
End Sub
gfk76Asked:
Who is Participating?
 
ayha1999Commented:
Hi,

try changing the follwoing

 If Not IsPostBack Then
            bindData()
 End If

...

 Dim ds As DataSet = New DataSet()
   myCommand.Fill(ds)

Session("datasource")=Ds <----- add this line

   dbGrid.DataSource = ds
   dbGrid.DataBind()
End Function

Then in the pageindexhcanged evnet, chang to

Sub dbGrid_PageIndexChanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles dbGrid.PageIndexChanged
   dbGrid.CurrentPageIndex = e.NewPageIndex
   dbGrid.DataSource =Sesssion("datasource")
   dbGrid.DataBind()
End Sub

hope this helps.

ayha


0
 
gfk76Author Commented:
Like magic!!!
Thanks alot!!
0
 
RejojohnyCommented:
gfk76, looks like u got the solution .. but an advice .. using session when not rquired is not a good idea for ur application's perfomance .. if u have 10 such pages and u plan to use 10 ssession variables and if u have 100 such concurrent users, u will have 10*100 session variables which will be eating into ur servers memory ... as u will never know when to release ur session variables ...it will only get released when the user closes his browser and in-turn the session ...

what should have been done is that ... ur dataset should be fetched everytime the page is loaded and u should use this new dataset in the dbGrid_PageIndexChanged event .. for that just remove the is postback in the load event .. and the rest of ur original code will be correct .. note that the load event is always excuted first ....

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            dbGrid.DataSource = bindData()
            dbGrid.DataBind()
End Sub

.. ur original code in the pageIndexChanged will open the new page ...

If ur page is been used very frequently and the data does not change much then think about using "application cache" to store the dataset ... ...
0
 
gfk76Author Commented:
Thanks you for the tip!
I will absolutely take this under concideration.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.