• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 348
  • Last Modified:

I need to SEE my database in VB.NET... connects but cant "view" mysql database...

Alright. Just gave the basic, important coding below.  I can currently connect to my MySQL database online. But I am having no luck getting a "view" of my database in a datagrid or whatever.  I have tried everything and have had no luck thus far.  What I am looking to do is allow a view of any table in my database. I have 13 tables I could possibly "view".  This is a time-sensitive issue and I need to get this stuff taken care of already.  If you need any more information or a full codeset or whatever, let me know.  Again, I already have the connection....how do i query or view the database itself and put a table into a datagrid or listbox or whatever?? thanks in advance!

CODE: ============================================================

Imports MySql.Data.MySqlClient
'Imports System.Data

Public Class frmLogin
    Inherits System.Windows.Forms.Form
    Public conn As MySqlConnection

#Region " Windows Form Designer generated code "

    Private Sub cmdLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdLogin.Click
        conn = New MySqlConnection()
        If txtServer.Text = "Best Buy" Then
            conn.ConnectionString = "server=(my server);" _
              & "user id=(my user id);" _
              & "password=(my password);"
            conn.ConnectionString = "server=" & txtServer.Text & ";" _
              & "user id=" & txtUsername.Text & ";" _
              & "password=" & txtPassword.Text & ";" _
              & "database=" & txtDBName.Text & ";"
        End If

            txtServerIn.Text = txtServer.Text
            txtUsernameIn.Text = txtUsername.Text
            txtPasswordIn.Text = txtPassword.Text
            txtDBNameIn.Text = txtDBName.Text
            txtServer.Text = ""
            txtUsername.Text = ""
            txtPassword.Text = ""
            txtDBName.Text = ""
            Me.Height = 344
            cmdConnDetails.Visible = True
            cmdDBTools.Visible = True
            lblConnStatus.Text = "Connected"
            MessageBox.Show("Connection OPENED Successfully!")
        Catch myerror As MySqlException
            Dim errorText As String
            errorText = myerror.Message

            If errorText = "Unable to connect to any of the specified MySQL hosts" Then
                MessageBox.Show("Please check the SERVER name you have entered and try again. Cannot connect to the database at this time.", "Database Connection Error: Server")
            ElseIf errorText.Substring(0, 13) = "Access denied" Then
                MessageBox.Show("Please check the USERNAME and/or PASSWORD you have entered and try again. Cannot connect to the database at this time.", "Database Connection Error: Username/Password")
                MessageBox.Show("Error Connecting to Database: " & myerror.Message)
            End If
            'lblConnStatus.Text = "Not Connected"
        End Try
    End Sub

    Private Sub cmdLogOut_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdLogOut.Click
            txtServerIn.Text = ""
            txtUsernameIn.Text = ""
            txtPasswordIn.Text = ""
            txtDBNameIn.Text = ""
            Me.Height = 280
            cmdConnDetails.Visible = False
            cmdDBTools.Visible = False
            lblConnStatus.Text = "Not Connected"
            MessageBox.Show("Connection CLOSED Successfully!")
        Catch myerror As MySqlException
        Catch sysError As NullReferenceException
            lblConnStatus.Text = "Not Connected"
            MessageBox.Show("No Connection To Be Closed!")
        End Try

    End Sub

1 Solution
I see no attempt, in your code, to actually read data from any table in your database.  You appear to connect to the database, and then do nothing.  Somewhere you need to execute SQL to read data from a table.

SQL might look like this:

Dim strSQL as String = "Select * from MyTable"

then you might use a Command object to excute the SQL, using the currently opened connection, to create a DataReader to pull the data from the database.

Do you understand how to create a Command Object and then use the Command.ExecuteReader to create a DataReader object?

arikambAuthor Commented:
Yep. I understadn the basics of it...i took out all attempts for the connection becuase no matter what i tried nothing seemed to work.  so i figured someone out here would be able to point me in the right direction for the coding.  i know hte SQL end of things pretty well. never been super good with VB.NET and this database connection stuff is a first for me in the programming realm.  everytime i tried to pull info into a datagrid or whatever in VB.NET it'd just say something about no database selected when i was ALREADY CONNECTED to the darn thing lol.  I'm just at a lose.   Any ideas on the coding i'll need to take the data fro mthe database into a datagrid (or whatever you'd recommend pulling it into)??  Any help is a plus. Thanks for the quick reply!

Here is where I found good examples for getting started in VB.Net


Link for ASP programs...

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

I don't see a data adapter or dataset used.  You have a connection, but don't do anything with it.

A data adapter will get the data you want (through a SQL query), then you fill a dataset from the data adapter.  A datagrid shows the information in the dataset.
arikambAuthor Commented:
Always errors out on Error #1 saying the "No Database Selected".   This stuff is frustrating cuz i know its supposed to work but i just dont get it. and my database is named in the server line. i just dont get it. if i'm doing somehting wrong please let me know. thanks!

Dim conn As New MySqlConnection()
        Dim myCommand As New MySqlCommand()
        Dim myAdapter As New MySqlDataAdapter()
        Dim myData As New DataTable()
        Dim SQL As String

        SQL = "SELECT * from margininfotable"

        conn.ConnectionString = "server=bbcompmargindb.computers1337.com;" _
              & "user id=(userid);" _
              & "password=(pass);" _
              & "database=;"

                myCommand.Connection = conn
                myCommand.CommandText = SQL

                myAdapter.SelectCommand = myCommand

                dgvBase.DataSource = myData
            Catch myerror As MySqlException
                MsgBox("#1 There was an error reading from the database: " & myerror.Message)
            End Try

        Catch myerror As MySqlException
            MessageBox.Show("#2 Error Connecting to Database: " & myerror.Message)
        End Try

        'myCommand.Connection = conn
        'myCommand.CommandText = SQL

        'myAdapter.SelectCommand = myCommand

        'dgvBase.DataSource = myData
    End Sub
Have you tried using the OdbcConnection and OdbcDataAdapter from the toolbox to see if they will make the connection.  I know the prefered way is to code everything, but using the toolbox and data adapter wizard might pin point your problem.

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now