Results 1 to 3 of 3

Thread: There is no row at position X

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2011
    Posts
    11

    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:
    1. sqlBindingSource.AddNew()

    the follow code is in a click event of a delete button:
    Code:
    1. Try
    2.             If MessageBox.Show("Delete?", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
    3.                 sqlDs.Tables("Items").Rows(sqlBindingSource.Position).Delete()
    4.  
    5.                 Dim slqCmdBuilder As New SqlCommandBuilder(sqlAdapter)
    6.  
    7.                 Me.sqlAdapter.Update(Me.sqlDs.Tables("Items"))
    8.  
    9.                 slqCmdBuilder.Dispose()
    10.                 slqCmdBuilder = Nothing
    11.  
    12.             End If
    13.         Catch ex As Exception
    14.             MessageBox.Show(ex.Message, "Error Occurred", MessageBoxButtons.OK, MessageBoxIcon.Error)
    15.             Me.sqlDs.Tables("Items").RejectChanges()
    16.         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?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2011
    Posts
    11

    Re: There is no row at position X

    it worked fine with RemoveCurrent....
    thnx mate

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width