There is no row at position X
I seem to have a problem with the following code:
the follow code is in a click event of a new button:
Code:
sqlBindingSource.AddNew()
the follow code is in a click event of a delete button:
Code:
Try
If MessageBox.Show("Delete?", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
sqlDs.Tables("Items").Rows(sqlBindingSource.Position).Delete()
Dim slqCmdBuilder As New SqlCommandBuilder(sqlAdapter)
Me.sqlAdapter.Update(Me.sqlDs.Tables("Items"))
slqCmdBuilder.Dispose()
slqCmdBuilder = Nothing
End If
Catch ex As Exception
MessageBox.Show(ex.Message, "Error Occurred", MessageBoxButtons.OK, MessageBoxIcon.Error)
Me.sqlDs.Tables("Items").RejectChanges()
End Try
my problem is that every time i add 5 row to bindingsource through bindingnavigator and then try to delete one of the i get an error "There is no row at position X". can anyone plz help?
Re: There is no row at position X
AddNew creates a new row but doesn't actually add it to the underlying DataTable. It can't, because the row is initially empty, which will likely violate various constraints. You call AddNew and the row is created. You populate the fields of the row and then call EndEdit to commit the row to the DataTable.
Also, you should not be accessing the row through the DataTable. You've got a BindingSource; use it. The RemoveCurrent method of the BindingSource will delete the current row.
Re: There is no row at position X
it worked fine with RemoveCurrent....
thnx mate