By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,353 Members | 1,277 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,353 IT Pros & Developers. It's quick & easy.

Paging in ASP.NET(separate code in .vb file)

P: 1
Hi , I Hope You fine.
I have some problem with this code for paging in asp.net
this bottom code work correctly without any error but when
I try to place separate code in .VB file then error is begin and occured .I want to separate this code and compiling .vb code using VBC.exe later .(bin/paging.dll)
when do it like me so you retrive only < Previous Page Next Page > in your web browser and you don't retrive list Of data in your web browser.
then i try this job and place <% dopagin() %> in paging.aspx file between <body> ... </body>
then i retrive list of data but when you click next you cannot navigate than more 1 page and when
you click previous page you get error "Index -5 is not non-negative and below total rows count"
how can i correct this code .somebody help me please
thank you very much


Expand|Select|Wrap|Line Numbers
  1.  
  2. -------------Correct pagingDS.aspx Code begin ---------------------
  3.  
  4. <%@ Page Language="VB" ContentType="text/html" ResponseEncoding="iso-8859-1" %>
  5. <%@ Import Namespace="System.Data" %>
  6. <%@ Import Namespace="System.Data.SQLClient" %>
  7.  
  8. <script language="VB" runat="server">
  9.     Dim pagedData As New pagedDataSource
  10.  
  11.     Sub Page_Load(byVal obj As Object, byVal e As EventArgs)
  12.         doPaging()
  13.     End Sub
  14.  
  15.     Function getTheData() As DataTable
  16.         Dim DS As New DataSet()        
  17.         Dim strConnect As New SQLConnection("server=localhost;uid=sa;pwd=relayer;Database=Northwind")
  18.         Dim objSQLAdapter As New SQLDataAdapter("SELECT companyName, contactName, contactTitle FROM customers", strConnect)
  19.         objSQLAdapter.Fill(DS, "customers")
  20.  
  21.         Return DS.Tables("customers").Copy
  22.     End Function
  23.  
  24.     Sub doPaging()        
  25.         pagedData.DataSource = getTheData().DefaultView
  26.         pagedData.AllowPaging = True
  27.         pagedData.PageSize = 5
  28.  
  29.         Try
  30.             pagedData.CurrentPageIndex = Int32.Parse(Request.QueryString("Page")).ToString()
  31.         Catch ex As Exception
  32.             pagedData.CurrentPageIndex = 0
  33.         End Try
  34.  
  35.         btnPrev.Visible = ( NOT pagedData.IsFirstPage )
  36.         btnNext.Visible = ( NOT pagedData.IsLastPage )
  37.  
  38.         pageNumber.Text = (pagedData.CurrentPageIndex + 1) & " of " & pagedData.PageCount
  39.  
  40.         theDataList.DataSource = pagedData
  41.         theDataList.DataBind()
  42.     End Sub
  43.  
  44.     Public Sub Prev_Click(ByVal obj As Object, ByVal e As EventArgs)
  45.         Response.Redirect(Request.CurrentExecutionFilePath & "?Page=" & (pagedData.CurrentPageIndex - 1))
  46.     End Sub
  47.  
  48.     Public Sub Next_Click(ByVal obj As Object, ByVal e As EventArgs)
  49.         Response.Redirect(Request.CurrentExecutionFilePath & "?Page=" & (pagedData.CurrentPageIndex + 1))
  50.     End Sub
  51. </script>
  52. <html>
  53.     <head>
  54.         <title>Paging with ASP.NET - DataSet Example - VB.NET</title>
  55.     </head>
  56.     <body>
  57.         <form runat="server">
  58.             <font size="-1" face="Verdana, Arial, Helvetica, sans-serif"><asp:label id="pageNumber" runat="server" /></font>
  59.             <asp:DataList id="theDataList" runat="server">
  60.                 <ItemTemplate>
  61.                     <table border="0" cellpadding="0" cellspacing="0" width="500">
  62.                         <tr>
  63.                             <td width="140"><font size="-1" face="Verdana, Arial, Helvetica, sans-serif"><strong>Company Name</strong>:</font></td>
  64.                             <td><font size="-1" face="Verdana, Arial, Helvetica, sans-serif"><%# DataBinder.Eval(Container.DataItem, "companyName") %></font></td>
  65.                         </tr>
  66.                         <tr>
  67.                             <td width="110"><font size="-1" face="Verdana, Arial, Helvetica, sans-serif"><strong>Contact Name</strong>:</font></td>
  68.                             <td><font size="-1" face="Verdana, Arial, Helvetica, sans-serif"><%# DataBinder.Eval(Container.DataItem, "contactName") %></td>
  69.                         </tr>
  70.                         <tr>
  71.                             <td width="110"><font size="-1" face="Verdana, Arial, Helvetica, sans-serif"><strong>Contact Title</strong>:</font></td>
  72.                             <td><font size="-1" face="Verdana, Arial, Helvetica, sans-serif"><%# DataBinder.Eval(Container.DataItem, "contactTitle") %></font></td>
  73.                         </tr>
  74.                     </table>
  75.                 </ItemTemplate>
  76.                 <separatortemplate>
  77.                     <hr color="#0099FF" />
  78.                 </separatortemplate>
  79.             </asp:DataList>
  80.  
  81.             <asp:LinkButton id="btnPrev" Text="&lt;" OnClick="Prev_Click" runat="server" />
  82.             <asp:LinkButton id="btnNext" Text="&gt;" OnClick="Next_Click" runat="server" />
  83.         </form>
  84.     </body>
  85. </html>
  86.  
  87. -------------------Paging.aspx End Code-----------------------------------------
  88.  
  89.  
  90. ***********************************************************************
  91.  
  92.  
  93. ---------------MY .ASPX file begin------------------------
  94.  
  95. <%@ Page src="paging.vb" language="vb" AutoEventWireup="false" Explicit="True" Inherits="farhad.myfunction2" ContentType="text/html" %>
  96.  
  97. <html>
  98. <head>
  99. <title>Paging with ASP.NET - DataSet Example - VB.NET</title>
  100. </head>
  101. <body>
  102. <form runat="server">
  103. <font size="-1" face="Verdana, Arial, Helvetica, sans-serif"><asp:label id="pageNumber" runat="server" /></font>
  104. <asp:DataList id="theDataList" runat="server">
  105. <ItemTemplate>
  106.  
  107. <table border="0" cellpadding="0" cellspacing="0" width="500">
  108.                         <tr>
  109.                             <td width="140"><font size="-1" face="Verdana, Arial, Helvetica, sans-serif"><strong>Company Name</strong>:</font></td>
  110.                             <td><font size="-1" face="Verdana, Arial, Helvetica, sans-serif"><%# DataBinder.Eval(Container.DataItem, "companyName") %></font></td>
  111.                         </tr>
  112.                         <tr>
  113.                             <td width="110"><font size="-1" face="Verdana, Arial, Helvetica, sans-serif"><strong>Contact Name</strong>:</font></td>
  114.                             <td><font size="-1" face="Verdana, Arial, Helvetica, sans-serif"><%# DataBinder.Eval(Container.DataItem, "contactName") %></td>
  115.                         </tr>
  116.                         <tr>
  117.                             <td width="110"><font size="-1" face="Verdana, Arial, Helvetica, sans-serif"><strong>Contact Title</strong>:</font></td>
  118.                             <td><font size="-1" face="Verdana, Arial, Helvetica, sans-serif"><%# DataBinder.Eval(Container.DataItem, "contactTitle") %></font></td>
  119.                         </tr>
  120.                     </table>
  121.  
  122.         </ItemTemplate>
  123.         <separatortemplate>
  124.             <hr color="#0099FF" />
  125.     </separatortemplate>
  126.     </asp:DataList>
  127.  
  128.         <asp:LinkButton id="btnPrev" Text="&lt; Previous Page" OnClick="Prev_Click" runat="server" />
  129.         <asp:LinkButton id="btnNext" Text="Next Page &gt; " OnClick="Next_Click" runat="server" />
  130.         </form>
  131.     </body>
  132. </html>
  133.  
  134. ------------End My ASPX file ------------------------------
  135.  
  136. *************************************************
  137. -------MY .VB Code AND CLASS begin--> Paging.vb --------------------
  138. Imports System
  139. Imports    System.Data
  140. Imports System.Data.SQLClient
  141. Imports System.Web
  142. Imports System.Web.UI
  143. Imports System.Web.UI.Webcontrols
  144. Imports System.Xml
  145.  
  146.  
  147. namespace farhad
  148.  
  149. public class myfunction2 : Inherits Page
  150.  
  151. public pagedData As New pagedDataSource
  152. public ds As New DataSet()        
  153. public strConnect As New SQLConnection("server=localhost;uid=sa;pwd=mysecretpass;Database=northwind")
  154. public objSQLAdapter As New SQLDataAdapter("SELECT companyName, contactName, contactTitle FROM customers", strConnect)
  155. public btnPrev AS LinkButton
  156. public btnNext AS LinkButton
  157. public pageNumber AS Label
  158. public theDataList AS DataList
  159.  
  160. public Sub Page_Load(ByVal Sender as Object, ByVal E as EventArgs)
  161. dopaging()
  162. end sub
  163.     public function getTheData() As DataTable
  164.  
  165.         objSQLAdapter.Fill(DS, "customers")
  166.  
  167.         Return DS.Tables("customers").Copy
  168.     End function
  169.  
  170.     public Sub doPaging()        
  171.         pagedData.DataSource = getTheData().DefaultView
  172.         pagedData.AllowPaging = True
  173.         pagedData.PageSize = 5
  174.  
  175.         Try
  176.             pagedData.CurrentPageIndex = Int32.Parse(Request.QueryString("Page")).ToString()
  177.         Catch ex As Exception
  178.             pagedData.CurrentPageIndex = 0
  179.         End Try
  180.  
  181.         btnPrev.Visible = ( NOT pagedData.IsFirstPage )
  182.         btnNext.Visible = ( NOT pagedData.IsLastPage )
  183.  
  184.         pageNumber.Text = (pagedData.CurrentPageIndex + 1) & " of " & pagedData.PageCount
  185.  
  186.         theDataList.DataSource = pagedData
  187.         theDataList.DataBind()
  188.     End Sub
  189.  
  190.     public Sub Prev_Click(ByVal obj As Object, ByVal e As EventArgs)
  191.         Response.Redirect(Request.CurrentExecutionFilePath & "?Page=" & (pagedData.CurrentPageIndex - 1))
  192.     End Sub
  193.  
  194.     public Sub Next_Click(ByVal obj As Object, ByVal e As EventArgs)
  195.         Response.Redirect(Request.CurrentExecutionFilePath & "?Page=" & (pagedData.CurrentPageIndex + 1))
  196.     End Sub
  197.  
  198. end class
  199. end namespace
  200. ----------------------End My CLASS IN paging.VB FILE----------
  201.  
Aug 2 '06 #1
Share this Question
Share on Google+
2 Replies


P: 2
play with the index

If pagedData.CurrentPageIndex < 0 Then
pagedData.CurrentPageIndex = pagedData.CurrentPageIndex + (-pagedData.CurrentPageIndex)
End If
theDataList.DataSource = pagedData
theDataList.DataBind()

i have added this part to the dopaging() and things worked.
Nov 7 '06 #2

P: 2
sorry for previous post. i have corrected your code better. i hope it helps. im sending you dopaging sub only. but there are some extra controls that i have used. filter them please.
Sub doPaging()
pagedData.DataSource = getTheData().DefaultView
pagedData.AllowPaging = True
pagedData.PageSize = 5
Dim deg As Integer
Try
deg = Int32.Parse(Request.QueryString("Page"))
Catch ex As Exception
deg = 0
End Try
If deg < 0 Then
deg = deg + (-deg)
End If
Try
pagedData.CurrentPageIndex = deg
Catch ex As Exception
pagedData.CurrentPageIndex = 0
End Try
btnPrev.Visible = (Not pagedData.IsFirstPage)
btnNext.Visible = (Not pagedData.IsLastPage)


pageNumber.Text = pagedData.PageCount & " de " & (pagedData.CurrentPageIndex + 1)
pageNumber1.Text = pagedData.PageCount & " de " & (pagedData.CurrentPageIndex + 1)
If pagedData.CurrentPageIndex < 0 Then
pagedData.CurrentPageIndex = pagedData.CurrentPageIndex + (-pagedData.CurrentPageIndex)
End If
theDataList.DataSource = pagedData
theDataList.DataBind()
End Sub

Hi , I Hope You fine.
I have some problem with this code for paging in asp.net
this bottom code work correctly without any error but when
I try to place separate code in .VB file then error is begin and occured .I want to separate this code and compiling .vb code using VBC.exe later .(bin/paging.dll)
when do it like me so you retrive only < Previous Page Next Page > in your web browser and you don't retrive list Of data in your web browser.
then i try this job and place <% dopagin() %> in paging.aspx file between <body> ... </body>
then i retrive list of data but when you click next you cannot navigate than more 1 page and when
you click previous page you get error "Index -5 is not non-negative and below total rows count"
how can i correct this code .somebody help me please
thank you very much


Expand|Select|Wrap|Line Numbers
  1.  
  2. -------------Correct pagingDS.aspx Code begin ---------------------
  3.  
  4. <%@ Page Language="VB" ContentType="text/html" ResponseEncoding="iso-8859-1" %>
  5. <%@ Import Namespace="System.Data" %>
  6. <%@ Import Namespace="System.Data.SQLClient" %>
  7.  
  8. <script language="VB" runat="server">
  9.     Dim pagedData As New pagedDataSource
  10.  
  11.     Sub Page_Load(byVal obj As Object, byVal e As EventArgs)
  12.         doPaging()
  13.     End Sub
  14.  
  15.     Function getTheData() As DataTable
  16.         Dim DS As New DataSet()        
  17.         Dim strConnect As New SQLConnection("server=localhost;uid=sa;pwd=relayer;Database=Northwind")
  18.         Dim objSQLAdapter As New SQLDataAdapter("SELECT companyName, contactName, contactTitle FROM customers", strConnect)
  19.         objSQLAdapter.Fill(DS, "customers")
  20.  
  21.         Return DS.Tables("customers").Copy
  22.     End Function
  23.  
  24.     Sub doPaging()        
  25.         pagedData.DataSource = getTheData().DefaultView
  26.         pagedData.AllowPaging = True
  27.         pagedData.PageSize = 5
  28.  
  29.         Try
  30.             pagedData.CurrentPageIndex = Int32.Parse(Request.QueryString("Page")).ToString()
  31.         Catch ex As Exception
  32.             pagedData.CurrentPageIndex = 0
  33.         End Try
  34.  
  35.         btnPrev.Visible = ( NOT pagedData.IsFirstPage )
  36.         btnNext.Visible = ( NOT pagedData.IsLastPage )
  37.  
  38.         pageNumber.Text = (pagedData.CurrentPageIndex + 1) & " of " & pagedData.PageCount
  39.  
  40.         theDataList.DataSource = pagedData
  41.         theDataList.DataBind()
  42.     End Sub
  43.  
  44.     Public Sub Prev_Click(ByVal obj As Object, ByVal e As EventArgs)
  45.         Response.Redirect(Request.CurrentExecutionFilePath & "?Page=" & (pagedData.CurrentPageIndex - 1))
  46.     End Sub
  47.  
  48.     Public Sub Next_Click(ByVal obj As Object, ByVal e As EventArgs)
  49.         Response.Redirect(Request.CurrentExecutionFilePath & "?Page=" & (pagedData.CurrentPageIndex + 1))
  50.     End Sub
  51. </script>
  52. <html>
  53.     <head>
  54.         <title>Paging with ASP.NET - DataSet Example - VB.NET</title>
  55.     </head>
  56.     <body>
  57.         <form runat="server">
  58.             <font size="-1" face="Verdana, Arial, Helvetica, sans-serif"><asp:label id="pageNumber" runat="server" /></font>
  59.             <asp:DataList id="theDataList" runat="server">
  60.                 <ItemTemplate>
  61.                     <table border="0" cellpadding="0" cellspacing="0" width="500">
  62.                         <tr>
  63.                             <td width="140"><font size="-1" face="Verdana, Arial, Helvetica, sans-serif"><strong>Company Name</strong>:</font></td>
  64.                             <td><font size="-1" face="Verdana, Arial, Helvetica, sans-serif"><%# DataBinder.Eval(Container.DataItem, "companyName") %></font></td>
  65.                         </tr>
  66.                         <tr>
  67.                             <td width="110"><font size="-1" face="Verdana, Arial, Helvetica, sans-serif"><strong>Contact Name</strong>:</font></td>
  68.                             <td><font size="-1" face="Verdana, Arial, Helvetica, sans-serif"><%# DataBinder.Eval(Container.DataItem, "contactName") %></td>
  69.                         </tr>
  70.                         <tr>
  71.                             <td width="110"><font size="-1" face="Verdana, Arial, Helvetica, sans-serif"><strong>Contact Title</strong>:</font></td>
  72.                             <td><font size="-1" face="Verdana, Arial, Helvetica, sans-serif"><%# DataBinder.Eval(Container.DataItem, "contactTitle") %></font></td>
  73.                         </tr>
  74.                     </table>
  75.                 </ItemTemplate>
  76.                 <separatortemplate>
  77.                     <hr color="#0099FF" />
  78.                 </separatortemplate>
  79.             </asp:DataList>
  80.  
  81.             <asp:LinkButton id="btnPrev" Text="&lt;" OnClick="Prev_Click" runat="server" />
  82.             <asp:LinkButton id="btnNext" Text="&gt;" OnClick="Next_Click" runat="server" />
  83.         </form>
  84.     </body>
  85. </html>
  86.  
  87. -------------------Paging.aspx End Code-----------------------------------------
  88.  
  89.  
  90. ***********************************************************************
  91.  
  92.  
  93. ---------------MY .ASPX file begin------------------------
  94.  
  95. <%@ Page src="paging.vb" language="vb" AutoEventWireup="false" Explicit="True" Inherits="farhad.myfunction2" ContentType="text/html" %>
  96.  
  97. <html>
  98. <head>
  99. <title>Paging with ASP.NET - DataSet Example - VB.NET</title>
  100. </head>
  101. <body>
  102. <form runat="server">
  103. <font size="-1" face="Verdana, Arial, Helvetica, sans-serif"><asp:label id="pageNumber" runat="server" /></font>
  104. <asp:DataList id="theDataList" runat="server">
  105. <ItemTemplate>
  106.  
  107. <table border="0" cellpadding="0" cellspacing="0" width="500">
  108.                         <tr>
  109.                             <td width="140"><font size="-1" face="Verdana, Arial, Helvetica, sans-serif"><strong>Company Name</strong>:</font></td>
  110.                             <td><font size="-1" face="Verdana, Arial, Helvetica, sans-serif"><%# DataBinder.Eval(Container.DataItem, "companyName") %></font></td>
  111.                         </tr>
  112.                         <tr>
  113.                             <td width="110"><font size="-1" face="Verdana, Arial, Helvetica, sans-serif"><strong>Contact Name</strong>:</font></td>
  114.                             <td><font size="-1" face="Verdana, Arial, Helvetica, sans-serif"><%# DataBinder.Eval(Container.DataItem, "contactName") %></td>
  115.                         </tr>
  116.                         <tr>
  117.                             <td width="110"><font size="-1" face="Verdana, Arial, Helvetica, sans-serif"><strong>Contact Title</strong>:</font></td>
  118.                             <td><font size="-1" face="Verdana, Arial, Helvetica, sans-serif"><%# DataBinder.Eval(Container.DataItem, "contactTitle") %></font></td>
  119.                         </tr>
  120.                     </table>
  121.  
  122.         </ItemTemplate>
  123.         <separatortemplate>
  124.             <hr color="#0099FF" />
  125.     </separatortemplate>
  126.     </asp:DataList>
  127.  
  128.         <asp:LinkButton id="btnPrev" Text="&lt; Previous Page" OnClick="Prev_Click" runat="server" />
  129.         <asp:LinkButton id="btnNext" Text="Next Page &gt; " OnClick="Next_Click" runat="server" />
  130.         </form>
  131.     </body>
  132. </html>
  133.  
  134. ------------End My ASPX file ------------------------------
  135.  
  136. *************************************************
  137. -------MY .VB Code AND CLASS begin--> Paging.vb --------------------
  138. Imports System
  139. Imports    System.Data
  140. Imports System.Data.SQLClient
  141. Imports System.Web
  142. Imports System.Web.UI
  143. Imports System.Web.UI.Webcontrols
  144. Imports System.Xml
  145.  
  146.  
  147. namespace farhad
  148.  
  149. public class myfunction2 : Inherits Page
  150.  
  151. public pagedData As New pagedDataSource
  152. public ds As New DataSet()        
  153. public strConnect As New SQLConnection("server=localhost;uid=sa;pwd=mysecretpass;Database=northwind")
  154. public objSQLAdapter As New SQLDataAdapter("SELECT companyName, contactName, contactTitle FROM customers", strConnect)
  155. public btnPrev AS LinkButton
  156. public btnNext AS LinkButton
  157. public pageNumber AS Label
  158. public theDataList AS DataList
  159.  
  160. public Sub Page_Load(ByVal Sender as Object, ByVal E as EventArgs)
  161. dopaging()
  162. end sub
  163.     public function getTheData() As DataTable
  164.  
  165.         objSQLAdapter.Fill(DS, "customers")
  166.  
  167.         Return DS.Tables("customers").Copy
  168.     End function
  169.  
  170.     public Sub doPaging()        
  171.         pagedData.DataSource = getTheData().DefaultView
  172.         pagedData.AllowPaging = True
  173.         pagedData.PageSize = 5
  174.  
  175.         Try
  176.             pagedData.CurrentPageIndex = Int32.Parse(Request.QueryString("Page")).ToString()
  177.         Catch ex As Exception
  178.             pagedData.CurrentPageIndex = 0
  179.         End Try
  180.  
  181.         btnPrev.Visible = ( NOT pagedData.IsFirstPage )
  182.         btnNext.Visible = ( NOT pagedData.IsLastPage )
  183.  
  184.         pageNumber.Text = (pagedData.CurrentPageIndex + 1) & " of " & pagedData.PageCount
  185.  
  186.         theDataList.DataSource = pagedData
  187.         theDataList.DataBind()
  188.     End Sub
  189.  
  190.     public Sub Prev_Click(ByVal obj As Object, ByVal e As EventArgs)
  191.         Response.Redirect(Request.CurrentExecutionFilePath & "?Page=" & (pagedData.CurrentPageIndex - 1))
  192.     End Sub
  193.  
  194.     public Sub Next_Click(ByVal obj As Object, ByVal e As EventArgs)
  195.         Response.Redirect(Request.CurrentExecutionFilePath & "?Page=" & (pagedData.CurrentPageIndex + 1))
  196.     End Sub
  197.  
  198. end class
  199. end namespace
  200. ----------------------End My CLASS IN paging.VB FILE----------
  201.  
Nov 7 '06 #3

Post your reply

Sign in to post your reply or Sign up for a free account.