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

Posted on 2005-04-07
Last Modified: 2012-05-05
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 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

Question by:arikamb
    LVL 44

    Expert Comment

    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?


    Author Comment

    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!
    LVL 27

    Expert Comment


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


    Link for ASP programs...
    LVL 1

    Accepted Solution

    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.

    Author Comment

    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 = ";" _
                  & "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
    LVL 1

    Expert Comment

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
    It’s quite interesting for me as I worked with Excel using for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now