I have the following controls:
- textbox1 - this is where i type a message.
- listbox1 - this is where i display the messages.
- button1 - this is what posts my messages to the server.
- StatusStrip1.Title1 - this is where the current user's title will go. (i.e: Administrator, manager... etc.)
I have a table on a MYSQL database called "messages" and a column called "message". When i type something into textbox1 and click button1 i want to add a row to "messages" under the column "message" with the title and message as its value.(separated by a hyphen, dash, or other small delimiter)Then reload listbox1's contents to show the new message.
so i want the final message to be something like:
Administrator - Hello World!
I currently have the following code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim sqlpost As MySqlCommand = New MySqlCommand("INSERT INTO messages(message) VALUES(?name - ?message)"";", con)
sqlpost.Parameters.AddWithValue("?name", Title2.Text)
sqlpost.Parameters.AddWithValue("?message", TextBox1.Text)
Try
con.Close()
con.Open()
' Sending message.
If TextBox1.Text = "" Then
MessageBox.Show("You forgot to type a message!")
Else
sqlpost.ExecuteNonQuery()
' Reloading messages.
ListBox1.Items.Clear()
reader = sqlmessages.ExecuteReader()
While (reader.Read)
ListBox1.Items.Add(reader.Item("message"))
ListBox1.Items.Add("")
End While
reader.Close()
con.Close()
TextBox1.Text = ""
Label4.Text = "Message Sent!"
Timer2.Start()
End If
Catch myerror As MySqlException
MessageBox.Show("Error sending message to server: " & myerror.Message)
End Try
End Sub
I had it working before, but when i made changes to it, it came up with various sql statement syntax errors... (stuff like invalid truncated value of "Administrator", or invalid DOUBLE value "hello") and now it won't even display any messages currently in the "messages" table...)
If anyone could tell me what i'm doing wrong, or a more efficient way of doing this then i would greatly appreciate it! Thanks!