|
-
Sep 16th, 2004, 07:39 AM
#1
Thread Starter
Lively Member
Problem Deleting a record on the last page of a Data Grid
Hi,
I have an ASP.NET page and it works fine. I use datagrid and set page count to 5. When there is one record left on the last page and I try to delete it I get following error.
“System.Web.HttpException: Invalid CurrentPageIndex value. It must be >= 0 and < the PageCount. “
Is there any way that before I Bind Data, I can check if there is only one record left on the current page or there is any other solution to this problem?
Source Code:
Function CreateDataSource() As ICollection
Dim objConn As New SqlConnection(strConnString)
objConn.Open()
Const strSQL As String = "SELECT sName,sEMail,sPassword,sPin, dMemDate,sAppNo FROM tblUSERS WHERE bRealtor = 0 and sname <> 'X' ORDER BY sAppNo "
Dim objCmd As New SqlCommand(strSQL, objConn)
Dim objDR As SqlDataReader
objDR = objCmd.ExecuteReader()
Dim dt As DataTable
Dim dr As DataRow
'create a DataTable
dt = New DataTable
dt.Columns.Add(New DataColumn("sNAME", GetType(String)))
dt.Columns.Add(New DataColumn("sEmail", GetType(String)))
dt.Columns.Add(New DataColumn("sPassword", GetType(String)))
dt.Columns.Add(New DataColumn("sPin", GetType(String)))
dt.Columns.Add(New DataColumn("sAppNo", GetType(String)))
dt.Columns.Add(New DataColumn("dMemDate", GetType(Date)))
While objDR.Read()
dr = dt.NewRow()
dr(0) = objDR("sNAME")
dr(1) = objDR("sEmail")
dr(2) = objDR("sPassword")
dr(3) = objDR("sPin")
dr(4) = objDR("saPPno")
dr(5) = objDR("dMemDate")
dt.Rows.Add(dr)
End While
dvConsaltant = dt.DefaultView
CreateDataSource = dvConsaltant
dgConsultants.DataSource = dvConsaltant
dgConsultants.DataBind() 'This line of code gives error after deletion
End Function
Sub PagerButtonClick(sender As Object, e As EventArgs)
'used by external paging UI
Dim arg As String = sender.CommandArgument
Select arg
Case "next":
If (dgConsultants.CurrentPageIndex < (dgConsultants.PageCount - 1)) Then
dgConsultants.CurrentPageIndex += 1
End If
Case "prev":
If (dgConsultants.CurrentPageIndex > 0) Then
dgConsultants.CurrentPageIndex -= 1
End If
Case "last":
dgConsultants.CurrentPageIndex = (dgConsultants.PageCount - 1)
Case Else:
'page number
dgConsultants.CurrentPageIndex = Convert.ToInt32(arg)
End Select
Binddata
End Sub
Sub dgConsultants_Page(sender As Object, e As DataGridPageChangedEventArgs)
dgConsultants.CurrentPageIndex = e.NewPageIndex
Binddata
End Sub
sub BindData()
dgConsultants.DataSource = CreateDataSource()
dgConsultants.DataBind()
End Sub
Sub dgConsultants_Edit(sender As Object, e As DataGridCommandEventArgs)
dgConsultants.EditItemIndex = e.Item.ItemIndex
BindData()
dgConsultants.ShowFooter = False
End Sub
Sub dgConsultants_Cancel(sender As Object, e As DataGridCommandEventArgs)
dgConsultants.EditItemIndex = -1
BindData()
dgConsultants.ShowFooter = True
End Sub
Sub MyDataGrid_Delete(Sender As Object, E As DataGridCommandEventArgs)
dim strSQL as string
dim apn as string = dgConsultants.DataKeys(CInt(E.Item.ItemIndex))
Dim objConn2 As New SqlConnection(strConnString)
objConn2.Open()
strsql = "delete from tblUsers where sappno = '" & apn & "' "
Dim scmd2 As New SqlCommand(strSQL, objConn2)
scmd2.ExecuteNonQuery()
objConn2.Close()
BindData()
End Sub
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|