2014-11-21 4 views
0

그래서 데이터베이스와 함께 사용하는 인스턴트 메시징 기능이 있습니다. 메시지를 보낼 때마다 내 vb.net 응용 프로그램의 서식있는 텍스트 상자에 데이터베이스의 메시지 열에있는 내용을 인쇄합니다.데이터베이스 VB.NET 새로 고침

내 문제는 있습니다. 나는 "메시지 보내기"버튼을 두 번 눌러 기능을 작동시켜야합니다. 처음 클릭 할 때 아무 일도 일어나지 않습니다.

내가 잘못 생각한 사람이 있습니까? 매우 감사!

'-------For every row, print the message, skip a line, and add 1 so it goes to next msg-------- 
For i As Integer = 0 To tbl.Rows.Count - 1 
    rowIndex = i 
    strOutPut &= CStr(tbl.Rows(rowIndex)("Message")) & vbNewLine 
    i = i + 1 
Next 

가 왜 줄을 건너 뛰는 :

Try 
     '----------------Sends the message------------------------------------- 
     MysqlConn.Open() ' opening the connection to the DB 
     Dim query As String 
     query = "insert into dojodb.chats (Message) values ('" & txtMessage.Text & "')" 
     command = New MySqlCommand(query, MysqlConn) 
     reader = command.ExecuteReader 'executes the command and reads data from db 
     reader.Close() 


     '-------------------Retreives the message------------------------------------ 
     Dim sqlStr As String = "SELECT * FROM chats" 
     Dim chatcommand As New MySqlCommand(sqlStr, MysqlConn) 
     Dim rdr As MySqlDataReader = chatcommand.ExecuteReader() 
     Dim tbl As New DataTable 
     tbl.Load(rdr) 


     '-------For every row, print the message, skip a line, and add 1 so it goes to next msg-------- 
     For i As Integer = 0 To tbl.Rows.Count - 1 
      rowIndex = i 
      strOutPut &= CStr(tbl.Rows(rowIndex)("Message")) & vbNewLine 
      i = i + 1 
     Next 

     txtGroupChat.Text = strOutPut 
     strOutPut = "" 'clearing the string so that it does not print out duplicate info next time 

     '-------------------------End Retrieve------------------------------------------- 

     MysqlConn.Close() 
    Catch ex As Exception 
     MessageBox.Show(ex.Message) 'printing the exact error to help future testing if needed 
    Finally 
     MysqlConn.Dispose() 
    End Try 
End Sub 
+0

하지 질문에 대한 답변,하지만 경우에 당신이 그것을 인식하지 않은, 당신은 읽을 수 있습니다 [SQL Injection] (http://en.wikipedia.org/wiki/SQL_injection) –

+0

또한 데이터베이스에 메시지가 저장 될 때마다 자동으로 증가하는 "pk"라는 필드가 있습니다. 이 키는 기본 키이며 가장 오래된 메시지를 가장 최근에 정렬하여 텍스트 상자에 인쇄합니다. – foley

+0

@JamesThorpe, 제 데이터베이스가 안전합니다. 나는 민감한 연결 정보를 밝히지 않았다. – foley

답변

0

나는 당신의 문제가이 섹션 생각? 이렇게하면 표의 다른 모든 메시지가 기록되지 않으므로 두 번 눌러야 만 메시지가 표시됩니다. 당신은 수동으로 For 루프에서 인덱서를 증가 할 필요가 없습니다, 당신이 시도 제안 :

For i As Integer = 0 To tbl.Rows.Count - 1 
    rowIndex = i 
    strOutPut &= CStr(tbl.Rows(rowIndex)("Message")) & vbNewLine 
Next