在调用Read()"之前无效尝试访问字段

时间:2016-11-17 03:13:17

标签: vb.net visual-studio

我有这个问题:

  

"在调用Read()"之前无效尝试访问字段

textbox1到textbox3已经在运行,但它停留在textbox4中。我搜索了这个错误的答案,但仍然无法修复。我的代码在下面。有人可以帮忙吗?

Private Sub DataGridView1_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataGridView1.DoubleClick
    With DataGridView1

        If .SelectedRows.Count > 0 Then
            For i As Integer = .SelectedRows.Count - 1 To 0 Step -1

                con = New MySqlConnection
                con.ConnectionString = ServerLocalhost
                con.Open()
                ssql = "select * from bookinformation where BookNumber=" & "'" & DataGridView1.Rows(.SelectedRows(i).Index).Cells(0).Value() & "'"
                dcmd = New MySqlCommand(ssql, con)
                dreader = dcmd.ExecuteReader
                If dreader.Read Then
                    TextBox1.Text = dreader.Item("BookNumber")
                    TextBox2.Text = dreader.Item("BooksTitle")
                    TextBox3.Text = dreader.Item("Quantity")
                    TextBox4.Text = dreader.Item("PublicPrice")
                    TextBox5.Text = dreader.Item("OfficeRemit")
                    TextBox10.Text = dreader.Item("StudentRemit")
                    If inchanged = False Then
                        inchanged = True
                        TextBox7.Text = dreader.Item("SaleFund")
                        TextBox6.Text = dreader.Item("Saving")
                        TextBox8.Text = dreader.Item("SalesPerBook")
                        Label15.Text = dreader.Item("TypeofBook")
                        inchanged = False
                    End If
                    If Label15.Text = ("Hard Bound") Then
                        CheckBox3.CheckState = CheckState.Unchecked
                        CheckBox2.CheckState = CheckState.Unchecked
                        CheckBox1.CheckState = CheckState.Checked
                    ElseIf Label15.Text = ("Paper Bound") Then
                        CheckBox3.CheckState = CheckState.Unchecked
                        CheckBox2.CheckState = CheckState.Checked
                        CheckBox1.CheckState = CheckState.Unchecked
                    ElseIf Label15.Text = ("Complete Set") Then
                        CheckBox3.CheckState = CheckState.Checked
                        CheckBox2.CheckState = CheckState.Unchecked
                        CheckBox1.CheckState = CheckState.Unchecked
                    Else
                        CheckBox3.CheckState = CheckState.Unchecked
                        CheckBox2.CheckState = CheckState.Unchecked
                        CheckBox1.CheckState = CheckState.Unchecked
                        MsgBox("No Type Detected!")
                    End If

                    TextBox1.Enabled = False
                    TextBox2.Enabled = False
                    TextBox3.Enabled = False
                    TextBox4.Enabled = False
                    TextBox5.Enabled = False
                    TextBox10.Enabled = False
                    TextBox7.Enabled = False
                    TextBox6.Enabled = False
                    TextBox8.Enabled = False
                    CheckBox1.Enabled = False
                    CheckBox2.Enabled = False
                    CheckBox3.Enabled = False
                ElseIf id = 0 Then
                    MsgBox("No Record Found!", MsgBoxStyle.Information)
                End If

                '.Rows.RemoveAt(.SelectedRows(i).Index)
            Next
        Else
            ' MsgBox("Please select to Add")
        End If

    End With

1 个答案:

答案 0 :(得分:0)

试试这个:

    If dreader.Read Then
    dreader.Read()

等...