Search code examples
mysqldatabasevb.netsqldatareaderdatareader

Invalid attempt to Read when Reader is Closed (VB.Net)


I am currently having this error in my function. The case is I need to send an SMS to each of the customer who has their schedule going down the present time, so I have an SQL Query to Select the entry with ongoing schedule and for each entry the SQL Data Reader (READER) has read, It will execute my SMS Code. But I am getting this error.

Here is my code :

Public Function onschedule()
    MysqlConn.ConnectionString = ServerString

    Dim READER As MySqlDataReader
    Dim mobileNum, customername, datestart, msg As String

    Try
        MysqlConn.Open()
        Dim query As String

        query = "SELECT DATE_FORMAT(ratedemand_datestart, '%M %d, %Y') as 'Date Start',ratedemand_contact as 'Contact Number',sales_customername as 'Customer Name' FROM bnb.ratedemand WHERE ratedemand_status = 'Reserved' AND NOW() = ratedemand_datestart AND rate_category='Billiard'"
        COMMAND = New MySqlCommand(query, MysqlConn)
        READER = COMMAND.ExecuteReader

        While READER.Read
            mobileNum = READER.GetString("Contact Number") '09856994598'
            datestart = READER.GetString("Date Start")     'November 2, 2017 12:00 AM'
            customername = READER.GetString("Customer Name") 'Bruce R. Wayne'

            'SMS Code Starts Here'

            If SerialPort.IsOpen Then
                SerialPort.Close()
            End If

            msg = customername + ", this is an automated text from Sta. Lucia East Grand Mall Billiard Hall confirming your 'Billiard Schedule' on " + datestart + " (Right Now). We are reminding you that if you are 20 minutes behind your schedule. It will be automatically cancelled."

            SerialPort.PortName = "COM4"
            SerialPort.BaudRate = 9600
            SerialPort.Parity = Parity.None
            SerialPort.StopBits = StopBits.One
            SerialPort.DataBits = 8
            SerialPort.Handshake = Handshake.RequestToSend
            SerialPort.DtrEnable = True
            SerialPort.RtsEnable = True
            SerialPort.NewLine = vbCrLf
            SerialPort.Open()

            If SerialPort.IsOpen() Then
                SerialPort.Write("AT" & vbCrLf)
                SerialPort.Write("AT+CMGF=1" & vbCrLf)
                SerialPort.Write("AT+CMGS=" & Chr(34) & mobileNum & Chr(34) & vbCrLf)
                SerialPort.Write("Message: " & msg & Chr(26))
            Else
                MetroMessageBox.Show(Admin_Menu, "Port not Available", "System Information", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            End If
        End While

        MysqlConn.Close()

    Catch ex As Exception
        MsgBox(ex.Message)
    Finally
        MysqlConn.Dispose()
    End Try

    Return True
End Function

Solution

  • Dont underestimate the need for closing the reader :)

    Try
      While Reader.Read()
        ...
      End While
    Finally
      Reader.Close
      MysqlConn.Close()
    End Try