Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

[SOLVED] Vb.net Mysql Info To Label Instead Of Datagridview

VB.NET vb .net label datagridview gridview mysql grid

  • This topic is locked This topic is locked
20 replies to this topic

#1 Niell

Niell

    CC Newcomer

  • Member
  • PipPip
  • 11 posts
  • Location:Belgium

Posted 14 April 2012 - 08:45 AM

My program connects fine to the database ( local server ), it also does the correct thing with a datagridview but i can't get it to work with labels, then it says "System.Data.DataTableCollection" instead of the values.
This is my project code:
Imports MySql.Data.MySqlClient
Imports System
Imports System.Data.DataTableCollection
Imports System.Data
Public Class Form1
	Private connStr As String = "Database=members;" & _
					"Data Source=localhost;" & _
					"User Id=root;Password="
	Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
	End Sub

	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		'Hide login controls
		Label1.Visible = False
		Label2.Visible = False
		Button1.Visible = False
		CheckBox1.Visible = False
		TextBox1.Visible = False
		TextBox2.Visible = False
		'Show in-game controls

		retriveDataToDataGrid()
	End Sub

	Public Sub retriveDataToDataGrid()
		Try
			Dim query As String = "SELECT my_energy FROM members"
			Dim connection As New MySqlConnection(connStr)
			Dim da As New MySqlDataAdapter(query, connection)
			Dim ds As New DataSet()

			Label5.Text = da.ToString

			connection.Close()

		Catch ex As Exception
			MsgBox(ex.Message)

		End Try
	End Sub
End Class

Any idea what im doing wrong? :/
  • 0

#2 tavichh

tavichh

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 399 posts
  • Location:Delaware
  • Programming Language:C#
  • Learning:C++, PHP, JavaScript

Posted 14 April 2012 - 10:09 AM

What do you mean by labels? You want the column's or row's to show in labels?
  • 0
Send a like my way if I helped you or if you found my post useful or even just enjoyed reading my post.
Thank you :)

#3 papabear

papabear

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 472 posts
  • Location:DarkSide

Posted 14 April 2012 - 06:32 PM

this is where your problem is if I ever understand your problem, you want to get the value of my_energy from your database am I right? If yes, then there is a problem with this part of code


Public Sub retriveDataToDataGrid()
                Try
                        Dim query As String = "SELECT my_energy FROM members"
                        Dim connection As New MySqlConnection(connStr)
                        Dim da As New MySqlDataAdapter(query, connection)
                        Dim ds As New DataSet()

                        Label5.Text = da.ToString

                        connection.Close()

                Catch ex As Exception
                        MsgBox(ex.Message)

                End Try
End Sub


you might want to use a MySqlCommand and a MySqlDataReader Instead of using dataAdapter for this..

here's an example for you

Public Sub retriveDataToDataGrid()

                Try
                        Dim query As String = "SELECT my_energy FROM members"
                        Dim connection As New MySqlConnection(connStr)
                        Dim sqlCmd as new MySqlCommand
                        Dim Dreader as new MySqlDataReader


                        with sqlCmd
                                 .connection = connection
                                 .commandtext = query
                        end with

                         dreader = sqlCmd.executereader()

                        do while dreader.read
                              label5.text = dreader("my_energy")
                        loop

                        dreader.close()
                        connection.dispose()
                        connection.Close()

                Catch ex As Exception
                        MsgBox(ex.Message)

                End Try
End Sub
The subroutine above will give label5.text the last row value of my_energy
let me know if what I understand was correct :)
  • 1
Life has no CTRL+Z
Never Forget To HIT "LIKE" If I Helped

#4 Niell

Niell

    CC Newcomer

  • Member
  • PipPip
  • 11 posts
  • Location:Belgium

Posted 15 April 2012 - 12:23 AM

this is where your problem is if I ever understand your problem, you want to get the value of my_energy from your database am I right? If yes, then there is a problem with this part of code


Public Sub retriveDataToDataGrid()
				Try
						Dim query As String = "SELECT my_energy FROM members"
						Dim connection As New MySqlConnection(connStr)
						Dim da As New MySqlDataAdapter(query, connection)
						Dim ds As New DataSet()

						Label5.Text = da.ToString

						connection.Close()

				Catch ex As Exception
						MsgBox(ex.Message)

				End Try
End Sub


you might want to use a MySqlCommand and a MySqlDataReader Instead of using dataAdapter for this..

here's an example for you

Public Sub retriveDataToDataGrid()

				Try
						Dim query As String = "SELECT my_energy FROM members"
						Dim connection As New MySqlConnection(connStr)
						Dim sqlCmd as new MySqlCommand
						Dim Dreader as new MySqlDataReader


						with sqlCmd
								 .connection = connection
								 .commandtext = query
						end with

						 dreader = sqlCmd.executereader()

						do while dreader.read
							  label5.text = dreader("my_energy")
						loop

						dreader.close()
						connection.dispose()
						connection.Close()

				Catch ex As Exception
						MsgBox(ex.Message)

				End Try
End Sub
The subroutine above will give label5.text the last row value of my_energy
let me know if what I understand was correct :)


Thanks ill try that.

What I actually want to do is let the user login and then show there "energy", "nerve", etc... Guess i just have to change the query for that if i want to select my_energy from user 1 for example.

I also found this error when I used your code:


Error 1 'MySql.Data.MySqlClient.MySqlDataReader.Friend Sub New(cmd As MySql.Data.MySqlClient.MySqlCommand, statement As MySql.Data.MySqlClient.PreparableStatement, behavior As System.Data.CommandBehavior)' is not accessible in this context because it is 'Friend'. C:\Users\Dev\documents\visual studio 2010\Projects\HWatch\HWatch\Form1.vb 30 17 HWatch
  • 0

#5 papabear

papabear

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 472 posts
  • Location:DarkSide

Posted 15 April 2012 - 12:45 AM

Thanks ill try that.

What I actually want to do is let the user login and then show there "energy", "nerve", etc... Guess i just have to change the query for that if i want to select my_energy from user 1 for example.

I also found this error when I used your code:


Error 1 'MySql.Data.MySqlClient.MySqlDataReader.Friend Sub New(cmd As MySql.Data.MySqlClient.MySqlCommand, statement As MySql.Data.MySqlClient.PreparableStatement, behavior As System.Data.CommandBehavior)' is not accessible in this context because it is 'Friend'. C:\Users\Dev\documents\visual studio 2010\Projects\HWatch\HWatch\Form1.vb 30 17 HWatch


can you put this two


Dim sqlCmd as new MySqlCommand
Dim Dreader as new MySqlDataReader

into the declaration area of your code? The declaration area is the line after you declare your class. make the two into like this


Public sqlCmd as new MySqlCommand
Public Dreader as MySqlDataReader

sorry I used to declare all my database variables in a module and make them public so that I can use them everytime without declaring them again and again :)
  • 1
Life has no CTRL+Z
Never Forget To HIT "LIKE" If I Helped

#6 Niell

Niell

    CC Newcomer

  • Member
  • PipPip
  • 11 posts
  • Location:Belgium

Posted 15 April 2012 - 12:52 AM

I get an error while trying to get the information: "Connection must be valid and open", my local server is running tho.
  • 0

#7 papabear

papabear

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 472 posts
  • Location:DarkSide

Posted 15 April 2012 - 12:56 AM

I get an error while trying to get the information: "Connection must be valid and open", my local server is running tho.


can you add this before the

with sqlcmd


connection.open

  • 1
Life has no CTRL+Z
Never Forget To HIT "LIKE" If I Helped

#8 Niell

Niell

    CC Newcomer

  • Member
  • PipPip
  • 11 posts
  • Location:Belgium

Posted 15 April 2012 - 01:00 AM

Thanks its working now.
Any idea how I would select the "my_energy" from a specific user? I could send you the sql file.
Thanks for the help
  • 0

#9 papabear

papabear

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 472 posts
  • Location:DarkSide

Posted 15 April 2012 - 01:01 AM

Thanks its working now.
Any idea how I would select the "my_energy" from a specific user? I could send you the sql file.
Thanks for the help


does the user have a username, password and a userid?
  • 0
Life has no CTRL+Z
Never Forget To HIT "LIKE" If I Helped

#10 Niell

Niell

    CC Newcomer

  • Member
  • PipPip
  • 11 posts
  • Location:Belgium

Posted 15 April 2012 - 01:03 AM

playerid, playername, player_login_name, player_login_pass, my_email
  • 0

#11 papabear

papabear

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 472 posts
  • Location:DarkSide

Posted 15 April 2012 - 01:08 AM

here's an algorithm for you.

First after the player log in, I will get his PLAYERID and store it into a global variable for example I declare a public variable like this

Public PlayerID  as string 'use integer if the playerid is integer or numerical in the database

After that I would use an sql query like this

"SELECT my_energy FROM members WHERE playerid = '" & PLAYERID & "'"

the small caps playerid was the field into your database and the all caps one is the variable into your program :)

Happy coding
  • 1
Life has no CTRL+Z
Never Forget To HIT "LIKE" If I Helped

#12 Niell

Niell

    CC Newcomer

  • Member
  • PipPip
  • 11 posts
  • Location:Belgium

Posted 15 April 2012 - 01:16 AM

Wow, thanks alot, now i just gotta make the login system, I also want to show other info than energy but I will be able to do that on my own.

Thanks alot :)
  • 0





Also tagged with one or more of these keywords: VB.NET, vb, .net, label, datagridview, gridview, mysql, grid

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download