DELETE (DataGrid) Error

edora

Member
Joined
Aug 19, 2003
Messages
24
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
 
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
 
I dont get why youre using a Delete statement as well as adding a Command.Parameter.

Executing the Delete should surfice.
 
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
 
I dont have .NET here, but this should work...

Code:
Dim System_ID As String 
dim LB as LinkButton = CType(e.Item.FindControl("Delete"), LinkButton)
System_ID  = LB.Text
 
Hi guys,

Since i cannot get the solution after I tried quite a number of sample, Ive 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
 
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
 
Back
Top