简体   繁体   English

刷新数据库VB.NET

[英]Refreshing Database VB.NET

So I have instant messaging functionality that I use with a database. 因此,我具有用于数据库的即时消息传递功能。 Each time a message is sent it prints what's in the message column in the database into a rich text box in my vb.net application 每次发送消息时,它都会将数据库中消息列中的内容打印到我的vb.net应用程序的富文本框中

My problem is. 我的问题是。 I have to click on the "send message" button twice to get the functionality to work, as the first time I click it, nothing happens 我必须单击两次“发送消息”按钮才能使用该功能,因为我第一次单击该按钮,没有任何反应

Does anyone have any idea where I'm gone wrong? 有人知道我哪里出错了吗? Much appreciated! 非常感激!

 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

I think your problem is this section: 我认为您的问题是此部分:

'-------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

Why are you skipping a line? 你为什么跳过一行? This will cause every other message in the table to not be written out, and therefore that's why you have to press it twice so that it shows up. 这将导致表中的所有其他消息都不会被写出,因此这就是为什么您必须按两次以使其显示出来的原因。 You don't need to manually increment the indexer in a For loop, I suggest you try this: 您无需在For循环中手动增加索引器,建议您尝试以下操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM