Hi Everyone,
I had write a simple program using visual basic 2008 express edition for displaying the voting. I am using mySQL as my database, the program have to retrieve data by a SQL statement from my database. Whenever i click on start button, it should be refresh and retrieve the lastest data in mySQL database.
My problem is that when i load the program, so called a "formload", and click on the start button, it will display the correct data from database, but when i click the button second time, it does not refresh and display the latest data.
This program is done by me who is a newbie in visual basic, this is the only problem i met, i have to settle it in 1 weeks time. But until now i still cant find the solution, anyone please help me on that?
My code for my program start button as below
Private Sub btn2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn2.Click
End
End Sub
Private Sub btn1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnstart.Click
Dim MysqlConn As MySqlConnection
MysqlConn = New MySqlConnection()
' Define the SQL to grab data from table.
SQL = "SELECT count('content') FROM loglog where content = 'M1'"
'Connection String
MysqlConn.ConnectionString = "server=localhost;" _
& "user id=root;" _
& "password=;" _
& "database=vgsmdb"
' Try, Catch, Finally
Try
MysqlConn.Open()
myCommand.Connection = MysqlConn
myCommand.CommandText = SQL
myAdapter.SelectCommand = myCommand
myAdapter.Fill(myData)
DataGridView1.DataSource = myData
Catch myerror As MySqlException
MessageBox.Show("Cannot connect to database: " & myerror.Message)
Finally
MysqlConn.Close()
MysqlConn.Dispose()
End Try
' Define the SQL to grab data from table.
SQL2 = "SELECT count('content') FROM loglog where content = 'M2'"
'Connection String
MysqlConn.ConnectionString = "server=localhost;" _
& "user id=root;" _
& "password=;" _
& "database=vgsmdb"
' Try, Catch, Finally
Try
MysqlConn.Open()
myCommand.Connection = MysqlConn
myCommand.CommandText = SQL2
myAdapter.SelectCommand = myCommand
myAdapter.Fill(myData2)
DataGridView2.DataSource = myData2
Catch myerror As MySqlException
MessageBox.Show("Cannot connect to database: " & myerror.Message)
Finally
MysqlConn.Close()
MysqlConn.Dispose()
End Try
' Define the SQL to grab data from table.
SQL3 = "SELECT count('content') FROM loglog where content = 'M3'"
'Connection String
MysqlConn.ConnectionString = "server=localhost;" _
& "user id=root;" _
& "password=;" _
& "database=vgsmdb"
' Try, Catch, Finally
Try
MysqlConn.Open()
myCommand.Connection = MysqlConn
myCommand.CommandText = SQL3
myAdapter.SelectCommand = myCommand
myAdapter.Fill(myData3)
DataGridView3.DataSource = myData3
Catch myerror As MySqlException
MessageBox.Show("Cannot connect to database: " & myerror.Message)
Finally
MysqlConn.Close()
MysqlConn.Dispose()
End Try
' Define the SQL to grab data from table.
SQL4 = "SELECT count('content') FROM loglog where content = 'F1'"
'Connection String
MysqlConn.ConnectionString = "server=localhost;" _
& "user id=root;" _
& "password=;" _
& "database=vgsmdb"
' Try, Catch, Finally
Try
MysqlConn.Open()
myCommand.Connection = MysqlConn
myCommand.CommandText = SQL4
myAdapter.SelectCommand = myCommand
myAdapter.Fill(myData4)
DataGridView4.DataSource = myData4
Catch myerror As MySqlException
MessageBox.Show("Cannot connect to database: " & myerror.Message)
Finally
MysqlConn.Close()
MysqlConn.Dispose()
End Try
' Define the SQL to grab data from table.
SQL5 = "SELECT count('content') FROM loglog where content = 'F2'"
'Connection String
MysqlConn.ConnectionString = "server=localhost;" _
& "user id=root;" _
& "password=;" _
& "database=vgsmdb"
' Try, Catch, Finally
Try
MysqlConn.Open()
myCommand.Connection = MysqlConn
myCommand.CommandText = SQL5
myAdapter.SelectCommand = myCommand
myAdapter.Fill(myData5)
DataGridView5.DataSource = myData5
Catch myerror As MySqlException
MessageBox.Show("Cannot connect to database: " & myerror.Message)
Finally
MysqlConn.Close()
MysqlConn.Dispose()
End Try
' Define the SQL to grab data from table.
SQL6 = "SELECT count('content') FROM loglog where content = 'F3'"
'Connection String
MysqlConn.ConnectionString = "server=localhost;" _
& "user id=root;" _
& "password=;" _
& "database=vgsmdb"
' Try, Catch, Finally
Try
MysqlConn.Open()
myCommand.Connection = MysqlConn
myCommand.CommandText = SQL6
myAdapter.SelectCommand = myCommand
myAdapter.Fill(myData6)
DataGridView6.DataSource = myData6
Catch myerror As MySqlException
MessageBox.Show("Cannot connect to database: " & myerror.Message)
Finally
MysqlConn.Close()
MysqlConn.Dispose()
End Try
Btnsave.Enabled = False
btnedit.Enabled = True
End Sub
I had attach the GUI as well!
Really appreciates if someone can help me on this..
Thanks!