You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VB.NET切换数据时提示“There is no row in position 1”问题咨询

解决VB.NET切换数据时出现"There is no row in position 1"的问题

嘿,我明白你遇到的困扰了——第一条数据能正常编辑,但切换到下一条就弹出There is no row in position 1的错误,后续操作也都报同样的问题。咱们来一步步拆解原因和解决办法:

问题根源

看你贴出来的ToUpdate_Load代码,核心问题出在这行SQL查询上:

sql = "SELECT * FROM Adds WHERE StudentID=" & student ID & ""

这个语句只会查询单个指定StudentID的学生数据,所以填充到DataSet里的表只有1行数据(索引为0)。当你尝试切换到下一条时,代码肯定是在访问索引为1的行,但表里根本不存在这一行,自然就抛出这个错误了。

解决方案

1. 修改查询逻辑,获取多行数据

首先要把查询改成能获取所有需要编辑的学生数据(或者按业务需求的范围查询),而不是只查单个ID:

' 如果需要加载所有学生数据,去掉WHERE条件
sql = "SELECT * FROM Adds"

' 如果需要按特定条件过滤(比如某个年级),可以写这样的条件,不要只限定单个ID
' sql = "SELECT * FROM Adds WHERE Grade = 'Grade 10'"

2. 新增行索引跟踪变量

在窗体类的顶部定义一个全局变量,用来记录当前显示的是哪一行:

Private currentRowIndex As Integer = 0

3. 重构数据加载与显示逻辑

把加载数据和显示当前行的逻辑拆分,避免重复代码,同时增加数据存在性判断:

Private Sub ToUpdate_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Try
        con = New OleDbConnection(connectionString)
        sql = "SELECT * FROM Adds" ' 修改后的查询语句
        da = New OleDbDataAdapter(sql, con)
        ds = New DataSet
        ' 建议把表名改成简洁的名字,避免特殊字符和长名称带来的问题
        da.Fill(ds, "StudentData") 

        ' 先判断表中是否有数据
        If ds.Tables("StudentData").Rows.Count > 0 Then
            currentRowIndex = 0
            DisplayCurrentRow() ' 调用单独的显示方法
        Else
            MsgBox("没有可编辑的学生数据")
            ' 清空所有文本框
            txtID.Clear()
            txtFirstName.Clear()
            txtMname.Clear()
            txtMiddleI.Clear()
            txtLastName.Clear()
            txtGrade.Clear()
            txtSection.Clear()
        End If
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub

' 抽离显示当前行的方法,复用代码
Private Sub DisplayCurrentRow()
    Dim studentTable = ds.Tables("StudentData")
    txtID.Text = studentTable.Rows(currentRowIndex).Item(0).ToString
    txtFirstName.Text = studentTable.Rows(currentRowIndex).Item(1).ToString
    txtMname.Text = studentTable.Rows(currentRowIndex).Item(2).ToString ' 注意:原变量名"txt Mname"有空格,要改成合法的命名比如txtMname
    txtMiddleI.Text = studentTable.Rows(currentRowIndex).Item(3).ToString
    txtLastName.Text = studentTable.Rows(currentRowIndex).Item(4).ToString
    txtGrade.Text = studentTable.Rows(currentRowIndex).Item(5).ToString
    txtSection.Text = studentTable.Rows(currentRowIndex).Item(6).ToString
End Sub

4. 实现切换数据的按钮逻辑

给上一条/下一条按钮添加点击事件,确保切换时先判断索引是否合法:

' 下一条按钮点击事件
Private Sub btnNext_Click(sender As Object, e As EventArgs) Handles btnNext.Click
    Dim studentTable = ds.Tables("StudentData")
    ' 判断是否还能往后切换
    If currentRowIndex < studentTable.Rows.Count - 1 Then
        currentRowIndex += 1
        DisplayCurrentRow()
    Else
        MsgBox("已经是最后一条数据啦")
    End If
End Sub

' 上一条按钮点击事件
Private Sub btnPrevious_Click(sender As Object, e As EventArgs) Handles btnPrevious.Click
    ' 判断是否还能往前切换
    If currentRowIndex > 0 Then
        currentRowIndex -= 1
        DisplayCurrentRow()
    Else
        MsgBox("已经是第一条数据啦")
    End If
End Sub

额外注意事项

  • 你的文本框变量名有空格(比如txt Mname),这在VB.NET里是不合法的,必须改成txtMname或txt_Mname这类符合命名规范的名称。
  • 原表名Q1PT_Butil-Gulanes-Lee C-Olarte (4)过长且包含特殊字符,建议改成简洁的名称(比如StudentData),减少出错概率。
  • 原SQL语句存在SQL注入风险,如果之后需要按单个ID查询,一定要用参数化查询:
    sql = "SELECT * FROM Adds WHERE StudentID = @StudentID"
    da = New OleDbDataAdapter(sql, con)
    da.SelectCommand.Parameters.AddWithValue("@StudentID", studentID) ' studentID是你的变量,注意不要有空格
    

内容的提问来源于stack exchange,提问作者Allyson Gulanes

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 09:12:17