edora Posted September 8, 2003 Posted September 8, 2003 Hi guys, I have a datagrid with the delete command in the template. What I want is, whenever I click delete, I should be able to delete that particular row. My code is like below. Private Sub DataGrid1_DeleteCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.DeleteCommand Dim strConn As String = "Data Source=isdc01;Initial Catalog=Internet_SMS;user id=sa;pwd=;" Dim DeleteCmd As String = "DELETE from COntact_List Where id = @System_ID" Dim MyConn As New SqlConnection(strConn) Dim Cmd As New SqlCommand(DeleteCmd, MyConn) Cmd.Parameters.Add(New SqlParameter("@System_ID", DataGrid1.DataKeys(CInt(e.Item.ItemIndex)))) MyConn.Open() Cmd.ExecuteNonQuery() MyConn.Close() BindData() End Sub Thanks in advanced, Edora Theo Quote
edora Posted September 8, 2003 Author Posted September 8, 2003 Sorry, Forgot, When I try to run, I will get this error Cmd.Parameters.Add(New SqlParameter("@System_ID", DataGrid1.DataKeys(CInt(e.Item.ItemIndex)))) WIth the error command - Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index Can anyone help me to find our what is the error about? Thanks Quote
Moderators Robby Posted September 9, 2003 Moderators Posted September 9, 2003 I don't get why you're using a Delete statement as well as adding a Command.Parameter. Executing the Delete should surfice. Quote Visit...Bassic Software
edora Posted September 9, 2003 Author Posted September 9, 2003 Well, I have change my code like below Private Sub DataGrid1_DeleteCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.DeleteCommand Dim System_ID As String = CType(e.Item.FindControl("Delete"), LinkButton).Text Dim myTable As DataTable = CType(Session("Contact_ListTable"), DataTable) Dim myRows As DataRowCollection = myTable.Rows Dim thisRow As DataRow = myRows.Find(System_ID) If Not (thisRow Is Nothing) Then myRows.Remove(thisRow) Session("Contact_ListTable") = myTable DataGrid1.DataSource = Session("Contact_ListTable") DataGrid1.DataBind() End Sub but I still get an error which is Line 56: Dim System_ID As String = CType(e.Item.FindControl("Delete"), LinkButton).Text and the error command is Object reference not set to an instance of an object. Anyone could help me to figure out which is wrong? Thanks in advanced, Edora Theo Quote
Moderators Robby Posted September 10, 2003 Moderators Posted September 10, 2003 I don't have .NET here, but this should work... Dim System_ID As String dim LB as LinkButton = CType(e.Item.FindControl("Delete"), LinkButton) System_ID = LB.Text Quote Visit...Bassic Software
edora Posted September 15, 2003 Author Posted September 15, 2003 Hi guys, Since i cannot get the solution after I tried quite a number of sample, I've decided to change my code. Which is, when i mark the checkbox, and click the button delete outside the datagrid, it will delete the selected rows. Private Sub cmdButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdButton.Click For Each myDataGridItem In DataGrid2.Items If chkSelected.Checked Then (I need some code here to delete the selected row) End If Next End Sub Hoping for help... Thanks Quote
edora Posted September 16, 2003 Author Posted September 16, 2003 Hi guys, Finally I got it, so this is the sample working code! Private Sub cmdButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdButton.Click Dim a As CheckBox For Each myDataGridItem In DataGrid2.Items a = myDataGridItem.FindControl("chkSelection") ' Response.Write(CType(DataGrid2.Items(7).FindControl("chkSelection"), CheckBox).Checked()) If a.Checked Then m_objSMS.delete(myDataGridItem.Cells(2).Text()) End If Next End Sub Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.