Hello there, iam new to this forum but iam also new with retrieving data from a mysql database to an label in vb.net.
Iam using vb 2010 and my mysql is hosted on a server.
Here is my code:
Imports MySql.Data.MySqlClient
Imports System.Data.Sql
Imports System.Data.SqlClient
Public Class Gamefrm
Dim con As New MySqlConnection
Dim cmd As New MySqlCommand
Dim reader As MySqlDataReader
Private Sub gamefrm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
con = New MySqlConnection("Server==********;; User Id=********; Password=********; Database==********;")
Dim cmd As New MySqlCommand()
cmd.CommandText = "SELECT health,strength FROM playerstats WHERE playerid='" & playeridlbl.Text & "'"
cmd.CommandType = CommandType.Text
cmd.Connection = con
con.Open()
reader = cmd.ExecuteReader()
While reader.Read = True
healthlbl.Text = reader.Item(1).ToString()
strengthlbl.Text = reader.Item(2).ToString()
End While
Catch ex As Exception
MsgBox(ex.Message)
If con.State <> ConnectionState.Closed Then
con.Close()
End If
con.Close()
End Try
Try
con = New MySqlConnection("Server==********;; User Id=********; Password=********; Database==********;")
Dim cmd As New MySqlCommand()
cmd.CommandText = "SELECT money,weapon,ammunition FROM playerinventory WHERE playerid='" & playeridlbl.Text & "'"
cmd.CommandType = CommandType.Text
cmd.Connection = con
con.Open()
reader = cmd.ExecuteReader()
While reader.Read = True
moneylbl.Text = reader.Item(1).ToString()
weaponlbl.Text = reader.Item(2).ToString()
ammunitionlbl.Text = reader.Item(3).ToString()
End While
Catch ex As Exception
MsgBox(ex.Message)
If con.State <> ConnectionState.Closed Then
con.Close()
End If
con.Close()
End Try
End Sub
Private Sub Gamefrm_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
Mainfrm.Show()
Loginfrm.UsernameTextBox.Text = ""
Loginfrm.PasswordTextBox.Text = ""
End Sub
End Class
I masked the username and password for the database, but i hope someone can help me :).