似乎无法解决这个问题“在0号位置没有排”。错误。 [英] Can't seem to solve this "there is no row at position 0" error.

查看:74
本文介绍了似乎无法解决这个问题“在0号位置没有排”。错误。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让我的程序进入我的数据库中的表,名为tblEventsSigned,然后将数据写入新行。但是当程序到达我的Do Until循环找到一个空行时,简单地说在0位没有行,我似乎无法修复它。请帮忙!





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!


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.

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:

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.

推荐答案

数组索引从零开始,而不是一个用于初学者,所以这个:

Array indexes start at zero, not one for starters, so this:
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!


这篇关于似乎无法解决这个问题“在0号位置没有排”。错误。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆