Delete Record in datagrid not working
-
Hi , I have a datagrid in which I have a button column 'Delete' that works by calling 'Ondatagrid_deleteCommand'. My problems is when I try to delete a record by clicking on 'Delete' button in datagrid, I get the following error "Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index " I am clueless as to why this is happening since the same routine is working in other application which is exactly similar and ofcourse in which I am using a datagrid. The sub for delete command is as follows:
************************************************************************************************** Sub DataGrid_DeleteCommand(sender As Object, e As DataGridCommandEventArgs) 'Create the objects for the connection Dim myConnection As New SqlConnection(ReadDBConnectionString()) Dim DeleteCommand As SqlCommand = new SQLCommand() Dim Transaction as SQLTransaction 'Define the Query DeleteCommand.CommandText = "DELETE from chequedata where cheqregno= " &DataGrid.DataKeys(e.Item.ItemIndex) 'Add the Parameters to the Query with the actual Value DeleteCommand.Connection = myConnection 'Start Try Block Try 'Open Connection myConnection.Open() 'Begin Transaction Transaction = myConnection.BeginTransaction() 'Tell Command Object which Transaction to use DeleteCommand.Transaction = Transaction 'Execute the SQL Command DeleteCommand.ExecuteNonQuery() 'Nothing happend - Commit Transaction Transaction.commit() 'Show Message in Label lblMessage.Text = "Entry deleted" catch ex as Exception 'Failure occured - Rollback Transaction Transaction.RollBack() 'Write Message in Label lblMessage.Text = "Entry Not deleted" Finally 'Close connection myConnection.Close() DataGrid.EditItemIndex = -1 'Refresh the Grid and show changes RefreshGrid(Cint(ddlBranch.SelectedITem.Value),txtsearch.text) End Try End Sub ***************************************************************************************************
Thankyou all for all the help. It is much appreciated. Aartee. ...HE is watching Us All!