Link to home
Start Free TrialLog in
Avatar of arikamb
arikamb

asked on

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);"
        Else
            conn.ConnectionString = "server=" & txtServer.Text & ";" _
              & "user id=" & txtUsername.Text & ";" _
              & "password=" & txtPassword.Text & ";" _
              & "database=" & txtDBName.Text & ";"
        End If

        Try
            conn.Open()
            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!")
            'conn.Close()
        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")
            Else
                MessageBox.Show("Error Connecting to Database: " & myerror.Message)
            End If
        Finally
            'conn.Dispose()
            '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
        Try
            conn.Close()
            conn.Dispose()
            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

=================================================================
Avatar of Arthur_Wood
Arthur_Wood
Flag of United States of America image

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?

AW
Avatar of arikamb
arikamb

ASKER

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!
Avatar of Howard Cantrell
Hi,

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

.....http://msdn.microsoft.com/vbasic/downloads/samples/101samples.aspx

Link for ASP programs...

http://msdn.microsoft.com/asp.net/downloads/kits/default.aspx 
ASKER CERTIFIED SOLUTION
Avatar of BClements
BClements

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of arikamb

ASKER

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=;"
        Try
            conn.Open()

            Try
                myCommand.Connection = conn
                myCommand.CommandText = SQL

                myAdapter.SelectCommand = myCommand
                myAdapter.Fill(myData)

                dgvBase.DataSource = myData
                'dgvStatus.AutoSizeColumns(DataGridViewAutoSizeColumnCriteria.HeaderAndDisplayedRows)
            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

        'myAdapter.Fill(myData)
        '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.