Click here to Skip to main content
15,881,380 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have done paging in Datalist using MySql database , it is working well but there is one issue i.e
if i am searching products i.e( selecting categories from dropdownlist) then it fetch all items that are related to product , then i go to second page by clicking on next button , but if i select other category to search it fetch the data in the previous page where i was searching mean on second page where i was previously.
i.e if i am on second page , then the data fetch will be on second page ,it doesn't start from first page to fetch data it remain on that page , so if some category doesn't have data , it still showing 2 page of 1 pages, and if click on next button it will show showing 3 page of 1 pages and so on .
What is this issue can anybody solve it .............
Code below to understand it .........


VB
<pre lang="vb">

Imports MySql.Data.MySqlClient
Imports System.Data


  Dim cn As New MySqlConnection
    Dim cd As MySqlCommand
    Dim s, f, g, b, city As String
    Dim r, d, d1 As MySqlDataReader
    Dim da As New MySqlDataAdapter
    Dim ds As New DataSet
    Dim pg As New PagedDataSource
    Dim p As Integer


 Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        ds.Clear()
        bind()
    End Sub

Sub bind()
cn.ConnectionString = &quot;server=localhost ; user id=root; password=; database=**&quot;

            cn.Open()
            s = &quot;select name,emp_add,opno,pno,eds,wl,type from main where  product=&#39;&quot; &amp; DropDownList1.SelectedItem.Text &amp; &quot;&#39; ORDER BY name &quot;
            cd = New MySqlCommand(s, cn)
            da.SelectCommand = cd
            da.Fill(ds)
            pg.DataSource = ds.Tables(0).DefaultView
            pg.AllowPaging = True
            pg.PageSize = 10
            pg.CurrentPageIndex = CurrentPage
            Label2.Text = &quot;Showing Page: &quot; &amp; (CurrentPage + 1).ToString() &amp; &quot; of &quot; &amp; pg.PageCount.ToString()
            Button2.Enabled = Not pg.IsFirstPage
            Button3.Enabled = Not pg.IsLastPage
            DataList1.DataSource = pg
            DataList1.DataBind()
            cn.Close()
            
End Sub


 Public Property CurrentPage() As Integer
        Get
            Dim s1 As Object = Me.ViewState("CurrentPage")
            If s1 Is Nothing Then
                Return 0
            Else
                Return CInt(s1)
            End If
        End Get
        Set(ByVal value As Integer)
            Me.ViewState("CurrentPage") = value
        End Set
    End Property


 Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
        CurrentPage -= 1
        bind()
    End Sub

    Protected Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button3.Click

        CurrentPage += 1
        bind()
    End Sub
</pre>
Posted
Updated 7-Jan-13 16:39pm
v2

1 solution

When someone changes the data in any way, you need to set 'CurrentPage' to 0, or 1, whatever the default is.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900