Thursday, March 29, 2012

Paging problem

I've created a Datagrid and the AllowPaging is set to True, PageSize is set to 3. My database have a total of 7 records. When I run my page, only 3 records are display due to the PageSize, as I have 7 records, it shows there are total 3 pages. When I click the 2 or 3, nothing happen and it is still on the first page as it should display the next page with another 3 records. Any idea on fixing this problem.

I'm using Webmatrix 1.1 and MS SQL server 2000.

Thanks in advance.If you're using the default code generated by the Web Matrix it should just work. A few things to check:
1. Are you using a dataset which is bound to the datagrid containing the data which should be displayed? The default paging mechanism only works fine with a dataset using the Web Matrix code.
2. Is there a DataGrid_Page event handler in your code? It should contain:


DataGrid1.CurrentPageIndex = e.NewPageIndex
BindGrid()

Check if the event is registered correctly to the PageIndexChanged event of the datagrid. It should point to the DataGrid_Page event method.
3. The Page_Load event should check for a postback of the page and only bind the data to the grid when there's NO postback:

Sub Page_Load(Sender As Object, E As EventArgs)
If Not Page.IsPostBack Then
' Databind the data grid on the first request only
' (on postback, rebind only in paging command)
BindGrid()
End If
End Sub

Hope this can help you?

Tip: turn page tracing on (Trace = "True") to monitor which events are fired to determine where the problem is located in the code.
Thanks bdesmet, I've got it running now.

Paging Problem

Private Sub LoadData()
Dim keyword As String = Request.QueryString("kw")
Dim ds As New DataSet
ds = BusinessLayer.Usefull.FindBook(keyword.ToLower)

gv1.DataSource = ds.Tables(0)
gv1.DataBind()
End Sub
Protected Sub gv1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gv1.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
gv2 = e.Row.FindControl("gv2")
gv2.DataSource = dtOutlines
gv2.DataBind()
End If
End Sub

Protected Sub gv2_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs)
CType(gv1.FindControl("gv2"), GridView).PageIndex = e.NewPageIndex
'Error Happens here, Object reference not set to an instance of an object
LoadData()
End Sub

HTML:
GV2:OnPageIndexChanging="gv2_PageIndexChanging"

Any Ideas??

Any Ideas??


Protected Sub gv2_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs)
ctype(sender,GridView).PageIndex=e.NewPageIndex
'Error Happens here, Object reference not set to an instance of an object
LoadData()
End Sub


Paging Problems

I have set up paging on a datagrid on a webform.
Everything loads up nicely but when I click on a page link nothing happens.
Any Ideas anyone??
Thanks in AdvanceYou have to implement your own procedure to implement the paging functionality. Have a look at 4guys:-

http://www.4guysfromrolla.com/webtech/072101-1.2.shtml

Paging question

Hello all:

Suppose I have a datagrid and when I retrieve a datatable, and flake a
dataview off of that table to bind to my grid, my datatable has, say,
10,000+ records in it.
Suppose further that my datagrid has paging turned on, and I display 10
rows of data at a time.
Let us further suppose that I am caching the table in the page cache,
and retrieving it at datagrid binding time:

myDataView = New DataView(CType(Cache.Item("myDataTable"),DataTable)
myDataGrid.DataSource = myDataView
myDataGrid.DataBind()

Given that my datagrid will display only 10 rows at a time -
1) How many rows are in my dataview? All of them, or just the 10 being
displayed in the datagrid?
2) Where in memory is my datatable - on the server, or on the client?

I'm trying to figure out if I have a bandwidth issue whether I allow
the grid to display all of the rows, or if I page it...

Thanks much,
zdrakec> Given that my datagrid will display only 10 rows at a time -
> 1) How many rows are in my dataview? All of them, or just the 10 being
> displayed in the datagrid?

DataViews don't "hold" any data at all, they are just filters of the
original data in your DataTable.

> 2) Where in memory is my datatable - on the server, or on the client?

All of your object instances are always held in memory on the server that
created them.

> I'm trying to figure out if I have a bandwidth issue whether I allow
> the grid to display all of the rows, or if I page it...

You may want to consider pulling down just 10 records from your database at
a time and make more trips to the data store than pulling down all of them
and not going back to the data store again.

> Thanks much,
> zdrakec
Hello Scott:

Ya know, I knew that about dataviews, so I must be getting senile :)

I'm wondering, not so much about holding that big a recordset on the
server memory, but how much of that data is actually being sent via my
network back to the client...all of it, or just the 10 records I have
my grid display at a time?

Thank you,
zdrakec
All of it is being sent to the client, regardless of how many records you
opt to show at any one time. This is why I suggest getting the records in
smaller batches and more often.

"zdrakec" <zdrakec@.yahoo.com> wrote in message
news:1121808311.966023.57210@.z14g2000cwz.googlegro ups.com...
> Hello Scott:
> Ya know, I knew that about dataviews, so I must be getting senile :)
> I'm wondering, not so much about holding that big a recordset on the
> server memory, but how much of that data is actually being sent via my
> network back to the client...all of it, or just the 10 records I have
> my grid display at a time?
> Thank you,
> zdrakec
That's what I figured, but I wasn't sure. Yes, I may well have to fetch
smaller batches...

Thanks for the info!

Cheers,
zdrakec

Paging query results...or however you say this

I got a table with thousands of news articles.
Need a query that return a certain number of articles, for a specific day, and then display only a certain number at a time.
On the page I guess I will have "Next, Previous, First, Last" links...

So I guess the query will go like "Give me all the articles for this day, from number X up to number "X + 20"

not sure how to start with this...any ideas welcome to push me offHMMM...seems it might not be so hard. by setting the rowcount, i can limit the number of records returned by the query...but how to I say "start at row number x" ?

SET rowcount 10
SELECT ArticleID, Title
FROM tblArticles
WHERE PublishDate = '2004-06-02 00:00:00.000' and lang = 1
SET ROWCOUNT 0
this seems to work fine...any critic?
-- the "starting values". In our case, we need two:
DECLARE @.startingID INT;
-- returns resutls from row @.a to row @.b:
DECLARE @.a INT;
DECLARE @.b INT;
SET @.a = 1 -- start at row 2
SET @.b = 100 -- end at row 5
-- get the starting date and starting ID to return results:
SET rowcount @.a
SELECT @.startingID = ArticleID
FROM tblArticles
WHERE PublishDate = '2004-06-04 00:00:00.000' and lang = 1
ORDER BY ArticleID ASC
-- find out how many rows to return, and set the rowcount:
SET @.b = 1 + @.b - @.a
SET rowcount @.b
-- now return the results:
SELECT * FROM tblArticles
WHERE
PublishDate = '2004-06-04 00:00:00.000' AND lang = 1 AND ArticleID >= @.StartingID
ORDER BY PublishDate ASC,ArticleID ASC
-- clean up:
SET rowcount 0 let me convert this to a sproc
now for the page itself.
hmmm...must know before hand how many articles...guess another query needed
how to keep track of what page we are at, and so on, without running this query again and again (database design bad..not done by me..not very optimized)

Paging Query

I have created some code with some help from this forum and it nearlly works the way I want it to !!

My original code would display the results of a search without any paging...

so i got the help on customizing the code to include paging of the results.

Before I added the paging code, when you loaded my search web page no results would show until you entered some search criteria.
when i got the paging code working the web page when initially loaded would display all the records and would page OK and when you entered some search criteria, it would filter the relevant results.
I then stumbled on how to modify this code to do what I want and it works but only for a while then it errors out on both my local and server copy. To get it working I have to run the aspx page that shows all the results, then when I run my modified page it works again ?
There must be some sort of caching going on, but im not sure??

I will include the code below from my two files, the only difference in the code is the lines I post...the 1st bit of code is the file that displays all the rows of data when loaded and pages fine


<%@dotnet.itags.org. Page Explicit="True" Language="VB" Debug="True" ContentType="text/html" ResponseEncoding="iso-8859-1" %>
<%@dotnet.itags.org. Register TagPrefix="ASPFD" TagName="Header" src="http://pics.10026.com/?src=header.ascx" %>
<%@dotnet.itags.org. import Namespace="System.Data" %>
<%@dotnet.itags.org. import Namespace="System.Data.OleDb" %>
<script runat="server"
' Sub Page_Load(Sender As Object, E As EventArgs)
' Header.AddPageName("Search")
' End Sub

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

If Not Page.IsPostBack Then

Session("SelectStatement") = "Select * From GolfersQ Order By Lastname"
BindGrid()

End If

End Sub

Sub Search_Click(ByVal Sender As Object, ByVal E As EventArgs)

Dim SelectStatement As String
Dim WhereClause As String

Message.Text = ""

' If they only entered a Lastname, go to the Lastname page


The next snippet of code is from my search page, that works fine, even paging for a while but then suddenly starts erroring out, until I run the original page again, you will see the lines of code i have commented out..

<script runat="server"
Sub Page_Load(Sender As Object, E As EventArgs)
Header.AddPageName("Search")
End Sub

' Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

' If Not Page.IsPostBack Then

' Session("SelectStatement") = "Select * From GolfersQ Order By Lastname"
' BindGrid()

' End If

' End Sub

Sub Search_Click(ByVal Sender As Object, ByVal E As EventArgs)

Dim SelectStatement As String
Dim WhereClause As String

Message.Text = ""

' If they only entered a Lastname, go to the Lastname page


I know this sounds confusing, its difficult to explain whats going on, but if anybody can point me in the right direction on whats happening, I would be very grateful.
Thanks in advance
SteveIt would probably help if you showed us the error message.
Hi,
Here is the error message im getting

Server Error in '/' Application.
------------------------

Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

Line 110:
Line 111: Connect.ConnectionString = ConnectString
Line 112: Adapter.SelectCommand = New OleDbCommand(Session("SelectStatement").ToString(), Connect)
Line 113: 'Adapter.SelectCommand.Connection.Open()
Line 114: Adapter.Fill(Top100DS, "GolfersQ")

Source File: D:\Webspace\golfinmanchester.co.uk\wwwroot\search.aspx Line: 112

Stack Trace:

[NullReferenceException: Object reference not set to an instance of an object.]
ASP.Search_aspx.BindGrid() in D:\Webspace\golfinmanchester.co.uk\wwwroot\search.aspx:112
ASP.Search_aspx.AdsGrid_Page(Object sender, DataGridPageChangedEventArgs e) in D:\Webspace\golfinmanchester.co.uk\wwwroot\search.aspx:126
System.Web.UI.WebControls.DataGrid.OnPageIndexChanged(DataGridPageChangedEventArgs e) +110
System.Web.UI.WebControls.DataGrid.OnBubbleEvent(Object source, EventArgs e) +349
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +26
System.Web.UI.WebControls.DataGridItem.OnBubbleEvent(Object source, EventArgs e) +100
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +26
System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e) +120
System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +115
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +18
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +138
System.Web.UI.Page.ProcessRequestMain() +1277

------------------------
Version Information: Microsoft .NET Framework Version:1.1.4322.573; ASP.NET Version:1.1.4322.573

hope this helps you help me !!
Session("SelectStatement") is probably null (Nothing).

Paging Records on at a time.

Hello,
I have an asp.net application that will use a SQL Server DBMS for the
backend.
On my asp.net form. I have a next, previous, last and first buttons on my
form.
Basically, this application will allow the user to page through some data
one record at a time.
Let's say my SQL statement returns the following rows with ID's of (1, 3,
5, 9, 10).
What would be the best way that I can always retrieve the previousID and
nextID according to the rows my Select SQL statement returned?
Thanks,
MarkIf your using a DataGrid, you can use the Inbuilt paging.
This will keep track of what the previous and next items are.
private void dg_PageIndexChanged(object source,
System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
dg.CurrentPageIndex = e.NewPageIndex;
PopulateGrid(); //this would rebind the grid but the the CurrentPageIndex
set as above
}
Also ensure to set the AllowPaging attribute on your DataGrid to true,
and in your case, set the PageSize to 1
If your using a DataList, i don't believe it has InBuilt paging, and you'll
have to build a custom solution
"SouthSpawn" <southspawn@.aol.com> wrote in message
news:45aab3325cec912c9cb54249ee98a80d@.lo
calhost.talkaboutsoftware.com...
> Hello,
> I have an asp.net application that will use a SQL Server DBMS for the
> backend.
> On my asp.net form. I have a next, previous, last and first buttons on my
> form.
> Basically, this application will allow the user to page through some data
> one record at a time.
> Let's say my SQL statement returns the following rows with ID's of (1, 3,
> 5, 9, 10).
> What would be the best way that I can always retrieve the previousID and
> nextID according to the rows my Select SQL statement returned?
> Thanks,
> Mark
>