Monday, March 26, 2012

This is an ASP.NET complite program and it works fine. An error is genereded if

Hi,

Following is an ASP.NET complite program and it works fine. An error is genereded if there is one record at the last page of the DataGrid.
Here is the error generated by the system System.Web.HttpException: Invalid CurrentPageIndex value. It must be >= 0 and < the PageCount.
is there any way to fix this problem?

Here's the Code:
<%@dotnet.itags.org. Import Namespace="System.Data" %>
<%@dotnet.itags.org. Import Namespace="System.Data.SQLClient" %>
<html>
<head>
<meta name="GENERATOR" Content="ASP Express 2.1">
<title>Deleting from DataBase using a DataGrid</title>
<script language="VB" runat="server">

Sub MyDataGrid_DeleteCommand(s As Object, e As DataGridCommandEventArgs )
Dim strConn as String = "server=YourServer;uid=YourUID;pwd=YourPWD;database=YourDB"
Dim DeleteCmd As String = "DELETE from safetable Where id = @dotnet.itags.org.Id"
Dim MyConn as New SQLConnection(strConn)
Dim Cmd as New SqlCommand(DeleteCmd, MyConn)
Cmd.Parameters.Add(New SqlParameter("@dotnet.itags.org.ID", MyDataGrid.DataKeys(CInt(e.Item.ItemIndex))))
MyConn.Open()
Cmd.ExecuteNonQuery()
MyConn.Close()
BindData
End Sub

Sub Page_Change(sender As Object, e As DataGridPageChangedEventArgs)
MyDataGrid.CurrentPageIndex = e.NewPageIndex
BindData
End Sub

Sub Page_Load(Source As Object, E As EventArgs)
If Not Page.IsPostBack Then
BindData()
End If
End Sub

Sub BindData()
Dim strConn as String = "server=YourServer;uid=YourUID;pwd=YourPWD;database=YourDB"
Dim MySQL as string = "Select * from safetable Order by id desc"
Dim MyConn as New SQLConnection(strConn)
Dim ds as DataSet=New DataSet()
Dim Cmd as New SQLDataAdapter(MySQL,MyConn)
Cmd.Fill(ds,"safetable")
MyDataGrid.Datasource=ds.Tables("safetable").DefaultView
MyDataGrid.DataBind()
End Sub
</script>
</head>
<body>
<form runat="server" method="post">
<asp:Datagrid runat="server"
Id="MyDataGrid"
GridLines="Both"
cellpadding="0"
cellspacing="0"
Headerstyle-BackColor="#8080C0"
Headerstyle-Font-Name="Arial"
Headerstyle-Font-Bold="True"
Headerstyle-Font-Size="14"
BackColor="#8080FF"
Font-Name="Arial"
Font-Size="11"
AlternatingItemStyle-BackColor="#C0C0C0"
AlternatingItemStyle-Font-Name="Arial"
AlternatingItemStyle-Font-Size="11"
BorderColor="Black"
AllowPaging = "True"
PageSize = "8"
PagerStyle-Mode = "NumericPages"
PagerStyle-HorizontalAlign="Center"
PagerStyle-PageButtonCount = "10"
OnPageIndexChanged = "Page_Change"
AutogenerateColumns="False"
OnDeleteCommand="MyDataGrid_DeleteCommand"
DataKeyField="id"
Width="50%">
<Columns>
<asp:ButtonColumn Text="Delete" HeaderText="Delete" CommandName="Delete"></asp:ButtonColumn>
<asp:BoundColumn DataField="fname" HeaderText="fname"></asp:BoundColumn>
<asp:BoundColumn DataField="lname" HeaderText="lname"></asp:BoundColumn>
</Columns>
</asp:DataGrid>
</form>Here is the table structure for SQL

CREATE TABLE [safetable] (
[id] [int] IDENTITY (1, 1) NOT NULL ,
[fname] [char] (10) NULL ,
[lname] [char] (10) NULL
) ON [PRIMARY]
GO
Sub Page_Change(sender As Object, e As DataGridPageChangedEventArgs)
Dim pageIndex As Integer
If e.NewPageIndex >= 0 And e.NewPageIndex < MyDataGrid.PageCount
' Valid page index.
pageIndex = e.NewPageIndex
Else If e.NamePageIndex < 0
' Move to the first page.
pageIndex = 0
Else
' Move to the last page.
pageIndex = MyDataGrid.PageCount
End If

MyDataGrid.CurrentPageIndex = pageIndex
BindData
End Sub
Thanks for your support but I am getting same error. When I am at the last page on the datagrid, there is only one record is shown. I delete that record and it is deleted successfully. Since there is no data to show on the last page of the datagird, system cannot bind it. Thats why I am getting this error. After deleting the last record on the last page of the datagrid, it should display previous page.
Same solution... :)
Move the code in the Page_Change event into a separate function, and use that function only to DataBind(), then add calls to that function in both Page_Change and MyDataGrid_DeleteCommand events.
Your solution is not clear to me! Is there any way that you can create a sample?

Thanks
It's untested, but should work.

Sub MyDataGrid_DeleteCommand(s As Object, e As DataGridCommandEventArgs )
Dim strConn as String = "server=YourServer;uid=YourUID;pwd=YourPWD;database=YourDB"
Dim DeleteCmd As String = "DELETE from safetable Where id = @.Id"
Dim MyConn as New SQLConnection(strConn)
Dim Cmd as New SqlCommand(DeleteCmd, MyConn)
Cmd.Parameters.Add(New SqlParameter("@.ID", MyDataGrid.DataKeys(CInt(e.Item.ItemIndex))))
MyConn.Open()
Cmd.ExecuteNonQuery()
MyConn.Close()
BindData
End Sub

Sub Page_Change(sender As Object, e As DataGridPageChangedEventArgs)
MyDataGrid.CurrentPageIndex = GetValidPageIndex(e.NewPageIndex)
BindData
End Sub

Sub Page_Load(Source As Object, E As EventArgs)
If Not Page.IsPostBack Then
BindData()
End If
End Sub

Sub BindData()
Dim strConn as String = "server=YourServer;uid=YourUID;pwd=YourPWD;database=YourDB"
Dim MySQL as string = "Select * from safetable Order by id desc"
Dim MyConn as New SQLConnection(strConn)
Dim ds as DataSet=New DataSet()
Dim Cmd as New SQLDataAdapter(MySQL,MyConn)

Cmd.Fill(ds, "safetable")
With MyDataGrid
.Datasource = ds.Tables("safetable").DefaultView
.CurrentPageIndex = GetValidPageIndex(.CurrentPageIndex)
.DataBind()
End With
End Sub

Function GetValidPageIndex(ByVal intCurrentIndex As Integer) As Integer
Dim pageIndex As Integer
If e.NewPageIndex >= 0 And e.NewPageIndex < MyDataGrid.PageCount
' Valid page index.
pageIndex = e.NewPageIndex
Else If e.NamePageIndex < 0
' Move to the first page.
pageIndex = 0
Else
' Move to the last page.
pageIndex = MyDataGrid.PageCount
End If
End Function
Hi,

Is it posible that you can take a look at the your function GetValidPageIndex?. I had a error with it.
I made some modification to your function GetValidPageIndex and I still get error. Here is the code that I made modification.

Function GetValidPageIndex(ByVal intCurrentIndex As Integer) As Integer
Dim pageIndex As Integer
If intCurrentIndex >= 0 And intCurrentIndex < MyDataGrid.PageCount
' Valid page index.
GetValidPageIndex = intCurrentIndex
Else If intCurrentIndex < 0
' Move to the first page.
GetValidPageIndex = 0
Else
' Move to the last page.
GetValidPageIndex = MyDataGrid.PageCount
End If
End Function
I add following code and It worked fine.

Dim Result As Integer = MyDataGrid.Items.Count Mod MyDataGrid.PageSize

If Result = 1 Then
MyDataGrid.CurrentPageIndex = ( MyDataGrid.PageCount - 2)
End If

0 comments:

Post a Comment