Click here to Skip to main content
15,887,585 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm trying to make my program go into the table in my database, called "tblEventsSigned" and then write data to a new row. However when the program gets to my Do Until loop to find an empty row is simply says there is no row at position 0 and I can't seem to fix it. Please help!


C#
SQL = "SELECT * FROM tblEventsSigned"
       DataAdapter = New OleDb.OleDbDataAdapter(SQL, Connection)
       DataAdapter.Fill(DataSet, "tblEventsSigned")
       Connection.Close()
       Dim CurrentRow As Integer = 1     'sets the current row to the first one in the database before it begins to search for an empty row

       Do Until DataSet.Tables("tblEventsSigned").Rows(CurrentRow).Item(1).ToString.Length = 0 'finds empty place
           CurrentRow += 1
       Loop

       Do Until lst_selectedEvents.Items.Count = 0
           DataSet.Tables("tblEventsSigned").Rows(CurrentRow).Item(1) = Username
           DataSet.Tables("tblEventsSigned").Rows(CurrentRow).Item(2) = lst_selectedEvents.SelectedItem.ToString
           DataAdapter.Update(DataSet, "tblEventsSigned")
       Loop


This is the code I use to start the connection.

C#
DatabaseProv = "PROVIDER=Microsoft.ACE.OLEDB.12.0;"   'sets up provider for db
        Database = "\House Events.accdb"
        Folder = "P:\6th Form Computing\09OlsenH\COMP4 Form\COMP4\bin\Debug" 'where the db is (my documents)
        DatabasePath = Folder & Database
        DatabaseSource = "Data Source = " & DatabasePath
        Connection.ConnectionString = DatabaseProv & DatabaseSource
        Connection.Open()


Variables:
VB.NET
im Connection As New OleDbConnection      'object used for connection
    Dim DatabaseProv As String
    Dim DatabaseSource As String
    Dim Folder As String
    Dim Database As String
    Dim DatabasePath As String
    Dim DataSet As New DataSet
    Dim DataAdapter As New OleDbDataAdapter
    Dim SQL As String


What I have tried:

Changing the code so that the variables are in the actual subroutine and then opening the connection there.
Posted
Updated 8-Mar-16 5:47am
v2
Comments
CHill60 8-Mar-16 11:48am    
Just add a new row, there is no need to find an "empty space"

1 solution

Array indexes start at zero, not one for starters, so this:
VB
Dim CurrentRow As Integer = 1
Do Until DataSet.Tables("tblEventsSigned").Rows(CurrentRow).Item(1).ToString.Length = 0 'finds
Will always start at the second row, not the first. If there are no rows, it will fail.

Other than that, you need to start by looking at your data: we don't have it so we can't run your code in the same way you can.
So use the debugger: put a breakpoint at the top of the method that shows the problem and run your app. When it enters the method, it will stop and pass control to you. Single step through your code line by line, looking at the variables in the debugger and working out what should happen before you execute each row. Did it happen exactly as you expected? If so, more on. If not, why not? It should become fairly obvious where the mistake is , but as I said - we can't run your code so you will have to start looking!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900