vb.net 错误:该行/列不存在数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21511171/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 16:37:52  来源:igfitidea点击:

Error: No data exists for the row/column

vb.netvb.net-2010

提问by user3260683

I get the following error: No data exists for the row/column.

我收到以下错误:No data exists for the row/column

It should retrieve the image

它应该检索图像

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()

回答by Bj?rn-Roger Kringsj?

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

dr.GetValue(N) 是从零开始的序数。改变你的指数:

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

回答by ekad

The problem is you never execute pcmd, see the following two lines:

问题是你从不执行pcmd,看下面两行:

Dim pcmd As New OleDbCommand(sSql, con)
Dim pdr As OleDbDataReader = cmd.ExecuteReader() ' cmd should be replaced with pcmd

I would suggest adding While...End While Statementwhen getting the values from drand pdr. You also need to parameterize the second query to avoid SQL Injection.

我建议drand获取值时添加While...End While Statementpdr。您还需要参数化第二个查询以避免SQL 注入

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

While 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
End While

dr.Close()

sSql = "select Pfile from Faculty where StId = @StId"
Dim pcmd As New OleDbCommand(sSql, con)
pcmd.Parameters.AddWithValue("@StId", lab1id.Text)

Dim pdr As OleDbDataReader = pcmd.ExecuteReader()

While 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
End While

pdr.Close()