Unbound Gridview Delete Row Issue

  • Thread starter Thread starter officegeek
  • Start date Start date
O

officegeek

I have a gridview that is populated via page_load. I can get the
delete to work but get an error after it deletes. The error is below
the index value that is returned is the one that is deleted so that is
why I think I get an error. Any help would be great. I am in the
process of moving from classic ASP to .NET so i am not well versed
yet.

Index was out of range. Must be non-negative and less than the size of
the collection. Parameter name: index


Below is the codebehind


Protected Sub DeleteRow(ByVal sender As Object, ByVal e As
GridViewCommandEventArgs)

Try
Dim index As Integer = Convert.ToInt32(e.CommandArgument)

Dim selectedRow As GridViewRow =
DirectCast(e.CommandSource, GridView).Rows(index)

If e.CommandName = "deletefeed" Then
Dim feedid As String = selectedRow.Cells(0).Text

Dim strSQL As String = "delete from xmlfeeds where
xmlfeedid=" & feedid
Dim DBConnClient As New OleDbConnection(strConnString)
Dim cmd As New OleDbCommand(strSQL, DBConnClient)
cmd.Connection.Open()
cmd.ExecuteNonQuery()
cmd.Connection.Close()

errormessage.Text = "The XML Feed was deleted!"

End If

Catch ex As Exception
errormessage.Text = ex.Message
End Try

End Sub

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load

Dim strSQL As String = "Select * from xmlfeeds where
xmlfeedstatus=0"
Dim DBConnClient As New OleDbConnection(strConnString)
Dim cmd As New OleDbCommand(strSQL, DBConnClient)
cmd.Connection.Open()
Try
Dim GetXMLFeeds As OleDbDataReader = cmd.ExecuteReader()

GridView1.DataSource = GetXMLFeeds
DataBind()

GetXMLFeeds.Close()
Catch ex As Exception
errormessage.Text = ex.Message
Finally
cmd.Connection.Close()


End Try
End Sub
 
Here is my code:

Private Sub DataGrid1_DeleteCommand(ByVal source As System.Object, ByVal e
As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles
DataGrid1.DeleteCommand

Dim newrdp As New RequestDataProvider

DsUsers1 = newrdp.getuserslist

DsUsers1.Tables(0).Rows(e.Item.ItemIndex).Delete()

Dim newdp As New RequestDataProvider

newdp.deleteuser(DsUsers1)

Response.Redirect("manageusers.aspx")

End Sub



Review it and modify it. I beleive that your problem is that the Web is
"stateless". So when the datagrid is populated from that dataset it disposes
of the dataset after it loads. So you have to read again and then write

OR

you have better code you just need the Handles Datagrid1.deletecommand

You are arent calling your method at all from what i saw from your source
code.

The easiest way to geneate some methods is to use the dropdowns on top of
visual studio. In the left one chose the object/control that you want to
write code for and in the right the event that the method you want to
implement. If it is highlighted then it is already implemented. If not then
it will add the begining of the method to the end of your code.
 
Back
Top