473,405 Members | 2,272 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Unbound Gridview Delete Row Issue

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
Nov 26 '07 #1
1 5737
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.

"officegeek" <sa***@stainlessoffroad.comwrote in message
news:46**********************************@e1g2000h sh.googlegroups.com...
>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

Dec 3 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: NateDawg | last post by:
I'm reposting this. I'm kinda in a bind untill i get this figured out, so if anyone has some input it would sure help me out. Ok, I’ve noticed a few gridview problems floating around the forum....
9
by: J055 | last post by:
Hi I have a very simple configuration of the GridView with paging and sorting. When I do a postback the DataBinding event fires twice - in both the ProcessPostData and PreRender stages of the...
3
by: tarscher | last post by:
Hi all, I have a grid that contains 7 columns from 3 tables (3 unique keys, 4 normal fields). I show this 7 columns on the gridview. I now want to add edit and delete functionality. This should...
5
by: Nick | last post by:
HI All: I have problem to delete row in grid view. It always needs to click twice delete button to delete row. but everytime it fires the deleting and deleted event. I have no idea why the...
2
by: Greg | last post by:
I have a gridview on my form which I have populated using a datareader. What I would like to do is to be able to remove specified records from the gridview, without affecting the source of the...
1
by: Greg | last post by:
I have a gridview on my form which I have populated using a datareader. What I would like to do is to be able to remove specified records from the gridview, without affecting the source of the...
11
by: Greg | last post by:
I have a gridview on my form which I have populated using a datareader. What I would like to do is to be able to remove specified records from the gridview, without affecting the source of the...
2
by: rgparkins | last post by:
So, I've bitten the bullet and am converting some of my asp.net 1.1 sites to asp.net 2.0, now after many issues I have come to a stop with the objectdatasource and gridviews and maybe someone can...
2
by: GISmatters | last post by:
I have unbound checkboxes in a nested gridview to allow multi-selection of "child" rows. For context, the parent gridview rows are for large "reports", the child rows are for various specific files...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.