错误:行/列不存在数据 [英] Error: No data exists for the row/column

查看:47
本文介绍了错误:行/列不存在数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到以下错误:行/列不存在数据.

它应该检索图像

sSql = "SELECT TOP 1 * FROM Attendance ORDER BY Attendance_id DESC"
        Dim cmd As New OleDbCommand(sSql, con)
        Dim dr As OleDbDataReader = cmd.ExecuteReader()

        dr.Read()
        lab1id.Text = dr.GetValue(1).ToString
        lab1fname.Text = dr.GetValue(2).ToString
        lab1lname.Text = dr.GetValue(3).ToString
        lab1position.Text = dr.GetValue(4).ToString
        lab1subject.Text = dr.GetValue(5).ToString

        dr.Close()

        sSql = "select Pfile from Faculty where StId = '" & lab1id.Text & "'"
        Dim pcmd As New OleDbCommand(sSql, con)
        Dim pdr As OleDbDataReader = cmd.ExecuteReader()
        pdr.Read()
        Dim bits As Byte() = CType(dr("Pfile"), Byte())
        Dim memo As New MemoryStream(bits)
        Dim myimg As New Bitmap(memo)
        imgRetrieve.Image = myimg
        pdr.Close()

推荐答案

dr.GetValue(N) 是从零开始的序数.更改您的索引:

The dr.GetValue(N) is zero-based ordinal. Change your indices:

While dr.Read()
    lab1id.Text = dr.GetValue(0).ToString
    lab1fname.Text = dr.GetValue(1).ToString
    lab1lname.Text = dr.GetValue(2).ToString
    lab1position.Text = dr.GetValue(3).ToString
    lab1subject.Text = dr.GetValue(4).ToString
End While

While pdr.Read() '             | you got a typo here. Change `dr` to `pdr`.
    Dim bits As Byte() = CType(pdr("Pfile"), Byte())
    Dim memo As New MemoryStream(bits)
    Dim myimg As New Bitmap(memo)
    imgRetrieve.Image = myimg
End While

考虑将您的代码更改为如下所示:

Consider changing your code to something like this:

Using command As OleDbCommand = con.CreateCommand()
    command.CommandText = "SELECT TOP 1 * FROM Attendance ORDER BY Attendance_id DESC;"
    Using reader As OleDbDataReader = command.ExecuteReader()
        While reader.Read()
            lab1id.Text = reader.Item("id").ToString
            lab1fname.Text = reader.Item("fname").ToString
            lab1lname.Text = reader.Item("lname").ToString
            lab1position.Text = reader.Item("position").ToString
            lab1subject.Text = reader.Item("subject").ToString
        End While
    End Using
End Using

Using command As OleDbCommand = con.CreateCommand()
    command.CommandText = "select Pfile from Faculty where StId = @StId;"
    command.Parameters.AddWithValue("@StId", lab1id.Text)
    Using reader As OleDbDataReader = command.ExecuteReader()
        While reader.Read()
            Dim bits As Byte() = CType(reader.Item("Pfile"), Byte())
            Using stream As New MemoryStream(bits)
                imgRetrieve.Image = Bitmap.FromStream(stream)
            End Using
        End While
    End Using
End Using

这篇关于错误:行/列不存在数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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