Logo 
Search:

Asp.net Forum

Ask Question   UnAnswered
Home » Forum » Asp.net       RSS Feeds

Trouble paging a datagrid

  Asked By: Waldemar    Date: Jul 07    Category: Asp.net    Views: 950
  

I am having a problem paging a datagrid.

When I have more than one page and try to get to page 2, my datagrid
disappears. However, if I click a button thus creating a Postback,
the grid reappears at the correct page.

It looks like the DataGrid1_PageIndexChanged event NEVER fires. I
can verify this because when I try to refill the datagrid with data
that only requires one page, I blow up with:

Server Error in '/FinalProject/Version1' Application.


Invalid CurrentPageIndex value. It must be >= 0 and < the PageCount.
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.Web.HttpException: Invalid
CurrentPageIndex value. It must be >= 0 and < the PageCount.

Source Error:

An unhandled exception was generated during the execution of the
current web request. Information regarding the origin and location
of the exception can be identified using the exception stack trace
below.

Stack Trace:


[HttpException (0x80004005): Invalid CurrentPageIndex value. It must
be >= 0 and < the PageCount.]
System.Web.UI.WebControls.DataGrid.CreateControlHierarchy(Boolean
useDataSource) +2117
System.Web.UI.WebControls.BaseDataList.OnDataBinding(EventArgs e)
+49
System.Web.UI.WebControls.BaseDataList.DataBind() +23
Version1.GetTableStructure.Button1_Click(Object sender, EventArgs
e) in C:\Documents and
Settings\ArtS\VSWebCache\www.forstudents.ws\finalproject\version1
\GetTableStructure.aspx.vb:79
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +108

System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.
RaisePostBackEvent(String eventArgument) +58
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler
sourceControl, String eventArgument) +18
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection
postData) +33
System.Web.UI.Page.ProcessRequestMain() +1263

Imports System.Data
Imports System.Data.SqlClient

Public Class GetTableStructure
Inherits System.Web.UI.Page
Protected WithEvents Label1 As System.Web.UI.WebControls.Label
Protected WithEvents DropDownList1 As
System.Web.UI.WebControls.DropDownList
Protected WithEvents Button1 As System.Web.UI.WebControls.Button
Protected WithEvents DataGrid1 As
System.Web.UI.WebControls.DataGrid
Protected WithEvents Label3 As System.Web.UI.WebControls.Label
Protected WithEvents Label2 As System.Web.UI.WebControls.Label
Protected WithEvents SqlConnection1 As
System.Data.SqlClient.SqlConnection

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.SqlConnection1 = New System.Data.SqlClient.SqlConnection()
'
'SqlConnection1
'
Me.SqlConnection1.ConnectionString = "data
source=forstudents.ws;initial catalog=finalproject;persist security
info=Fal" & _
"se;user id=finalproject;pwd=netstudent;workstation id=ARTS-
COMPUTER;packet size=" & _
"4096"

End Sub

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form
Designer
'Do not modify it using the code editor.
InitializeComponent()

End Sub

#End Region

Public dsColumns As New DataSet()
Public daColumns As SqlDataAdapter

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here

If Not IsPostBack Then

' Set up a new SQLCommand to get the user table names
from the SQL sysobjects system table
Dim cmdTables As New SqlCommand("SELECT name FROM
dbo.sysobjects WHERE type = 'U' order by name", SqlConnection1)
' Create the DataReader
Dim drTables As SqlDataReader

' Open the connection because we are not using a
DataAdapter
SqlConnection1.Open()

' Get the table names
drTables = cmdTables.ExecuteReader()

' Bind the DropDownList to the DataReader
DropDownList1.DataSource = drTables
' The name to display
DropDownList1.DataTextField = "name"
' Bind the data
DropDownList1.DataBind()

' Close the DataReader and the Connection
drTables.Close()
SqlConnection1.Close()

End If

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click

' Fill DataAdapter
sbFillDataAdapter(DropDownList1.SelectedItem.Text,
SqlConnection1)

' Set the dataGrid dataSource and Bind
Me.Label2.Text = Trim(DropDownList1.SelectedItem.Text) & "
Structure"
DataGrid1.DataSource = dsColumns.Tables
(DropDownList1.SelectedItem.Text)
DataGrid1.DataBind()

End Sub

Private Sub DataGrid1_PageIndexChanged(ByVal source As Object,
ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs)
Handles DataGrid1.PageIndexChanged
' Fill DataAdapter
' sbFillDataAdapter(DropDownList1.SelectedItem.Text,
SqlConnection1)
DataGrid1.CurrentPageIndex = e.NewPageIndex
DataGrid1.DataBind()
End Sub

Public Sub sbFillDataAdapter(ByVal strTable As String, ByVal
SqlConnection1 As SqlConnection)

dsColumns.Reset()

' Set up the DataAdapter for the SQL system Stored Procedure
SP_COLUMNS
' This stored procedure returns column information for a
given table
daColumns = New SqlDataAdapter("sp_columns", SqlConnection1)

' Set up the CommandType for a Stored Procedure
daColumns.SelectCommand.CommandType =
CommandType.StoredProcedure
' Input Param Table Name
daColumns.SelectCommand.Parameters.Add(New SqlParameter
("@table_name", SqlDbType.VarChar))
daColumns.SelectCommand.Parameters("@table_name").Value =
strTable
daColumns.SelectCommand.Parameters("@table_name").Direction
= ParameterDirection.Input

' Fill the DataAdapter
daColumns.Fill(dsColumns, strTable)
End Sub

End Class

Share: 

 

No Answers Found. Be the First, To Post Answer.

 
Didn't find what you were looking for? Find more on Trouble paging a datagrid Or get search suggestion and latest updates.




Tagged: