Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
|
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 12th, 2008, 10:58 AM
Friend of Wrox
 
Join Date: Mar 2007
Posts: 205
Thanks: 4
Thanked 0 Times in 0 Posts
Default [Resolved] IListSource problem in GridView

Being new to asp I am a little confused as to handle data grid paging. When I do paging it give me an error in the data bind like:

The IListSource does not contain any data sources. (In the PageIndexChanging event)

I have defined my data set as a global variable and it looks like it does not find it. Here is my complete code:

[u]Definitions:</u>
Code:
Partial Class PropSale
    Inherits System.Web.UI.Page
    Public myDataSet As New Data.DataSet
    Public myDataTable As New Data.DataTable

[u]Page Load:</u>
Code:
Public Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Page.IsPostBack = False Then

            Dim myDouble As Double = 0
            GetInventory(myDouble)
            GridView1.DataSource = myDataSet
            GridView1.DataBind()

        End If

    End Sub


[u]Function - Load data set:</u>
Code:
Public Function GetInventory(ByVal myDouble As Double) As Data.DataSet

        Dim mySelectString As String = "Select Diameter, Pitch, Rotation, Bore, Blades, Material, Manufacturer," + _
                                              "Model, Quantity, Condition from PropInventory where Sold = False and Diameter >= " & + _
                                              myDouble & " Order by Diameter"

        Dim ConnectionString As String = ConfigurationManager.AppSettings("ConnectionString")
        Dim myOleDbConnection = New OleDbConnection(ConnectionString)
myOleDbConnection.Open()
        Dim myOleDbCommand As New OleDbCommand
        myOleDbCommand.CommandText = mySelectString
        myOleDbCommand.Connection = myOleDbConnection
        Dim myOleDbDataAdapter As New OleDbDataAdapter(myOleDbCommand)
        myOleDbDataAdapter.Fill(myDataSet, ("myDataTable"))

        'Fill manual rows Uses Number of Rows from table and grid page size
        Dim ShortFullPage As Double = myDataSet.Tables("myDataTable").Rows.Count Mod GridView1.PageSize
        Dim ManualRowsNeeded As Integer = GridView1.PageSize - ShortFullPage

        Dim i As Integer
        For i = 0 To ManualRowsNeeded - 1
            myDataSet.Tables("myDataTable").Rows.Add(myDataSet.Tables("myDataTable").NewRow())
        Next i

        Return myDataSet

    End Function


[u]Page Changing:</u>
Code:
Public Sub GridView1_PageIndexChanging1(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridView1.PageIndexChanging

        If Page.IsPostBack = True Then

            DisplayCurrentPage()
            GridView1.PageIndex = e.NewPageIndex
            GridView1.DataSource = myDataSet
            GridView1.DataBind()

        End If

    End Sub
 
Old February 12th, 2008, 11:24 AM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

I see a few problems.

First you need to bind your GridView before you set the page index in the PageIndexChanging event. Also the evaluation of IsPostBack is redundant because, unless you are explictly calling this method from somewhere in your code on initial page load, IsPostBack will always evaluate to true. Here is what the code should look like:

            DisplayCurrentPage()
            GridView1.DataSource = myDataSet
            GridView1.DataBind()
            GridView1.PageIndex = e.NewPageIndex

Second, in your page load you make this call:
GetInventory(myDouble)

You have defined GetInventory as a function but are using it as a method. My suggestion would be to do something liket his:

            Dim myDouble As Double = 0
            GridView1.DataSource = GetInventory(myDouble)
            GridView1.DataBind()
By doing things this way you dont have to worry about class level members (e.g. myDataSet and myDataTable) since the function will return a DataSet object. =]

So then you could change the call in your PageIndexChanging event to look like this:

            DisplayCurrentPage()
            GridView1.DataSource = GetInventory(<value>)
            GridView1.DataBind()
            GridView1.PageIndex = e.NewPageIndex


Without any changes to the code, myDataSet will remain empty because you only fill it with data on the initial page_load when you call GetInventory().

hth.




================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
.: Wrox Technical Editor / Author :.
Wrox Books 24 x 7
================================================== =========
 
Old February 12th, 2008, 12:18 PM
Friend of Wrox
 
Join Date: Mar 2007
Posts: 205
Thanks: 4
Thanked 0 Times in 0 Posts
Default

Thank you, that was a very helpful tutorial getting me moving forward. I think I am getting the picture...

Things now work except for one thing: One the paging I have to click 2x before it shows next page and the same moving back to previous page.

Page Index Changing:
Code:
Public Sub GridView1_PageIndexChanging1(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridView1.PageIndexChanging

        Dim myDouble As Double
        If TextBox1.Text = " " Or TextBox1.Text = "" Then
            myDouble = 0.0
        Else
            myDouble = Convert.ToDecimal(TextBox1.Text)
        End If

        DisplayCurrentPage()
        GridView1.DataSource = GetInventory(myDouble)
        GridView1.DataBind()
        GridView1.PageIndex = e.NewPageIndex

    End Sub


Page Load:
Code:
Public Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Page.IsPostBack = False Then

            Dim myDouble As Double = 0
            GridView1.DataSource = GetInventory(myDouble)
            GridView1.DataBind()

        End If

    End Sub


 
Old February 12th, 2008, 12:35 PM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Hmm. Looking back on my post I misspoke. The error you recieved was not because you were setting the pageindex prior to the databind it was because the dataset was empty. >< My bad. Your code should read as follows:

GridView1.PageIndex = e.NewPageIndex
GridView1.DataSource = GetInventory(myDouble)
GridView1.DataBind()

Appologies.



================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
.: Wrox Technical Editor / Author :.
Wrox Books 24 x 7
================================================== =========
 
Old February 12th, 2008, 01:33 PM
Friend of Wrox
 
Join Date: Mar 2007
Posts: 205
Thanks: 4
Thanked 0 Times in 0 Posts
Default

Works like a charm....again I very much appreciate your time helping me out, you been a very good mentor !!!

 
Old February 12th, 2008, 01:42 PM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Glad it worked out for you. =]

-Doug

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
.: Wrox Technical Editor / Author :.
Wrox Books 24 x 7
================================================== =========





Similar Threads
Thread Thread Starter Forum Replies Last Post
[Resolved] GridView - manually set SortExpression snufse ASP.NET 2.0 Basics 1 November 3rd, 2008 10:00 AM
[Resolved] GridView to Excel snufse ASP.NET 2.0 Basics 2 October 28th, 2008 02:37 PM
[Resolved] GridView Sorting and Paging snufse ASP.NET 2.0 Basics 1 October 9th, 2008 07:45 AM
[Resolved] GridView TemplateField - get value snufse ASP.NET 2.0 Basics 25 July 29th, 2008 07:25 AM
Enable Editing on Gridview - RESOLVED Andrew.Berry ASP.NET 2.0 Basics 0 October 3rd, 2007 05:29 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.