Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net IOException未处理。错误的文件名或编号_Vb.net_Ioexception - Fatal编程技术网

Vb.net IOException未处理。错误的文件名或编号

Vb.net IOException未处理。错误的文件名或编号,vb.net,ioexception,Vb.net,Ioexception,我写这篇文章是为了从文件中读取选定的索引并显示其详细信息,但我在标题中说明了错误 我的代码: 'Declare variables Dim OrdersFile As String Dim NumRecsFound As Integer Dim orderString As String Dim searchID As Integer 'Find the selected order ID orderString = lstOrder.Text searchID = Val(Microsoft

我写这篇文章是为了从文件中读取选定的索引并显示其详细信息,但我在标题中说明了错误

我的代码:

'Declare variables
Dim OrdersFile As String
Dim NumRecsFound As Integer
Dim orderString As String
Dim searchID As Integer

'Find the selected order ID
orderString = lstOrder.Text
searchID = Val(Microsoft.VisualBasic.Left(orderString, 10))

'Set up the path to the orders file
OrdersFile = Application.StartupPath & "/orders.dat"

'Open the orders file in random access mode
FileOpen(1, OrdersFile, OpenMode.Random, , , Len(OrderRec))
'For each record in the file
RecPos = 1
'Read in every record until the end of the file is reached
Do While Not EOF(1)
    FileGet(1, OrderRec, RecPos)

    'If the record matches selected order, display the order file
    If OrderRec.OrderID = searchID Then
        NumRecsFound = NumRecsFound + 1

        With OrderRec
            txtOrderID.Text = .OrderID
            txtOrderDate.Text = .OrderDate
            txtPrice.Text = .Price
            cmbCustomers.Text = .CustomerID
            cmbPizza.Text = .PizzaID
        End With
    End If

    'Lock fields until edit is selected
    txtOrderID.Enabled = False
    txtFee.Enabled = False
    txtOrderDate.Enabled = False
    txtStatus.Enabled = False
    txtPrice.Enabled = False
    cmbCustomers.Enabled = False
    cmbPizza.Enabled = False

    'Close the orders file and leave the subroutine
    FileClose(1)

    'If the records don't match then get the next record
    RecPos = RecPos + 1
Loop

'If no records are found then output message and close the file
If NumRecsFound = 0 Then
    MsgBox("Order " & searchID & " could not be found. Please try again.")
    FileClose(1)
End If
FileClose(1)
错误出现在第19行;“在不执行EOF(1)时执行”


任何帮助都将不胜感激。谢谢。

你说错误在线路上

Do While Not EOF(1)
    FileGet(1, OrderRec, RecPos)
    .......
但是你有这个

   'Close the orders file and leave the subroutine
    FileClose(1)

    'If the records don't match then get the next record
    RecPos = RecPos + 1
Loop
因此,如果您有多条记录,那么在第二个循环中,您的代码将失败,并出现上述错误

当您真正读取完记录后,将FleClose(1)调用移到循环之外……
当然,这就留下了如何处理多个记录的问题

最后,我们在VB.NET中,旧的VB6函数应该尽快被转储。因此,除非您只是维护一个旧项目,否则请花一些时间将此代码更改为使用





请随意添加…

这是一个与您的问题提示没有直接关系的提示:如果您是在VB.NET中编程,为什么要使用这么多VB6方法(比.NET方法旧得多)?如果您习惯于使用VB6编程,您可能会依赖某些VB6方法,但您的代码几乎是100%VB6@瓦罗卡巴斯写的也是一样,一个suffice@Steve好啊谢谢你让我知道(这里的每个人都没有给出相关的建议,也没有人解决实际问题LOL)。。。嗯,那里没有什么好东西。您的文件中有多少记录?@varocabas好的,谢谢。我是一名学生,在某种程度上遵循模板,但做一些自己的事情。我的老师说我们是用vb.net编写代码的。谢谢你告诉我。