Link to home
Start Free TrialLog in
Avatar of gfk76
gfk76

asked on

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
ASKER CERTIFIED SOLUTION
Avatar of ayha1999
ayha1999

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gfk76
gfk76

ASKER

Like magic!!!
Thanks alot!!
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 ... ...
Avatar of gfk76

ASKER

Thanks you for the tip!
I will absolutely take this under concideration.