Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Datgrid with paging

Posted on 2005-04-09
4
Medium Priority
?
336 Views
Last Modified: 2010-04-07
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
0
Comment
Question by:gfk76
  • 2
4 Comments
 
LVL 7

Accepted Solution

by:
ayha1999 earned 1000 total points
ID: 13743808
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
 

Author Comment

by:gfk76
ID: 13743852
Like magic!!!
Thanks alot!!
0
 
LVL 26

Expert Comment

by:Rejojohny
ID: 13746767
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
 

Author Comment

by:gfk76
ID: 13748026
Thanks you for the tip!
I will absolutely take this under concideration.
0

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
The Relationships Diagram is a good way to get an overall view of what a database is keeping track of. It is also where relationships are defined. A relationship specifies how two tables connect to each other. As you build tables in Microsoft Ac…
Suggested Courses
Course of the Month15 days, 13 hours left to enroll

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question