Click here to Skip to main content
15,867,308 members

Paging in Datalist using Mysql Database

Narendra Singh Rathore asked:

Open original thread
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>
Tags: ASP.NET4.0

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



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