VB.NET阅读器已关闭

时间:2015-03-11 20:12:11

标签: vb.net

我的代码允许我使用我的数据库中的客户详细信息向客户发送电子邮件。

Try
            mysqlconn.Open()
            GetEmailCredentials(credentials)
            query = "SELECT First_name, surname, email_address FROM customer,booking WHERE booking.BookingID='" & y & "' AND customer.customerID=booking.customerID"
            command = New MySqlCommand(query, mysqlconn)
            MsgBox("ref1")
            reader = command.ExecuteReader
            MsgBox("ref2")
            While reader.Read()
                MsgBox("ref3")
                Try
                    MsgBox(reader.GetString("email_address") & reader.GetString("First_name") & reader.GetString("surname"))
                    MsgBox("ref4")
                    Dim emailmessage As New MailMessage()
                    emailmessage.From = New MailAddress(credentials(0))
                    emailmessage.To.Add(reader.GetString("email_address"))
                    emailmessage.Subject = "EHCC Booking"
                    emailmessage.Body = "Hello " & reader.GetString("First_name") & " " & reader.GetString("surname") & Environment.NewLine & "One of your bookings has been deleted." & Environment.NewLine & "Please contact me for further details."
                    Dim smtp As New SmtpClient("smtp.gmail.com")
                    smtp.Port = 587
                    smtp.EnableSsl = True
                    smtp.Credentials = New System.Net.NetworkCredential(credentials(0), credentials(1))
                    smtp.Send(emailmessage)
                    MsgBox("ref5")
                Catch ex As Exception
                    errors.Enqueue("Email details are incorrect")
                End Try
                mysqlconn.Close()
            End While
        Catch ex As MySqlException
            errors.Enqueue(ex.Message)
        Finally
            mysqlconn.Dispose()
        End Try

我继续收到错误消息“读取器关闭时无效尝试读取”。 我弹出所有参考消息框,以及输出查询结果的参考消息框。查询问好,所以我不知道发生了什么。我无法看到我关闭了读者的任何地方,直到我关闭了mysqlconnection。 对不起,如果我的解释清楚的话。

1 个答案:

答案 0 :(得分:1)

更改此

While reader.Read()
    ...
    mysqlconn.Close()
End While

到此

While reader.Read()
    ...
End While
mysqlconn.Close()