2013-11-23 78 views
1

我擁有的是一個小型消息傳遞函數,它基本上可以向一個用戶或一組用戶發送消息。有兩個mysql表;一個爲使用該系統的用戶調用用戶,另一個用於存儲消息的消息。該系統可以發送到特定的用戶,但發送給一羣人時,VB給我visual basic 2012連接必須有效且開放

InvalidOperationException異常被處理:連接應該是開放和有效

下面的代碼中給出。

Dim receiver, subject, message As String 
Dim user As Integer 
Dim MySqlConnection As New MySqlConnection 
Dim MyAdapter As New MySqlDataAdapter 
Dim command As New MySqlCommand 
Dim mydata As MySqlDataReader 
Private Sub sendButton_Click(sender As Object, e As EventArgs) Handles sendButton.Click 

user = loginForm.user 
If recieverTextBox.Text = "" And studentCheckBox.Checked = False And facultyCheckBox.Checked = True Then 

     subject = subjectTextBox.Text 
     message = messageRichTextBox.Text 
     MySqlConnection = New MySqlConnection 
     MySqlConnection.ConnectionString = "server=localhost; User ID=root; password=''; database=sis_db" 
     Dim query = "insert into messages (date, sender, receiver, subject, message) select CURRENT_DATE, '" & user & "',user_id,'" & subject & "', '" & message & "' from users where user_type='faculty';" 

     Try 
      MySqlConnection.Open() 
      command.CommandText = query 
      MyAdapter.SelectCommand = command 
      mydata = command.ExecuteReader 'the error message points here' 
      MsgBox("Messages sent", MsgBoxStyle.OkOnly, Title:="SUCCESS!") 
      recieverTextBox.Clear() 
      subjectTextBox.Clear() 
      messageRichTextBox.Clear() 
     Catch ex As MySqlException 
      MsgBox("DATABASE ERROR!") 
     End Try 

     MySqlConnection.Close() 

    ElseIf 
+1

請把整個代碼。哪一行發生錯誤? Stacktrace也是?在我看來,connectino在使用後已關閉,然後嘗試使用關閉的連接。 –

回答

0

你可以替換DateTime.Now代替

你測試的連接選擇CURRENT_DATE?它可能只是一個錯字

相關問題