combobox problem

Marvin_G

Member
Joined
Jul 30, 2013
Messages
7
Programming Experience
1-3
hello guys below is my code. when i select / fetch data from mysql database at the first time, it can run, the second time i run without changing anything, i get this error, "Object reference not set to an instance of an object." at line that i marked below (combo box). The combo box load list of tables from a database. Please help me. Thanks

Imports MySql.Data.MySqlClient
Public Class Form1

    Dim conn As MySqlConnection
    Dim reader As MySqlDataReader
    Dim command As MySqlCommand
    Dim adapter As MySqlDataAdapter
    Dim dt As DataTable
    Dim var As Integer
    Dim p_error As Integer
    Dim cmbselected As String


    Private Sub MainFunction(ByVal lol)
        If (var = 0) Then
            MessageBox.Show("please Connect to a database.")
            Exit Sub
        End If


        Select Case lol
            Case "Select"
                Try
                    cmbselected = cmbTable.Text
                    command.CommandText = "select * from " & cmbselected & ""    <-----HERE'S THE PROBLEM OCCURED
                    command.CommandType = CommandType.Text
                    command.Connection = conn
                    cmbselected = ""
                    adapter = New MySqlDataAdapter(command)


                    dt = New DataTable
                    adapter.Fill(dt)


                    Dim cb As New MySqlCommandBuilder(adapter)


                    With DataGridView1
                        .AutoGenerateColumns = True
                        .DataSource = dt
                    End With

                    command.Dispose()
                    command = Nothing
                    conn.Close()
                    conn.Dispose()

                Catch ex As MySqlException
                    MessageBox.Show("Error1: " & ex.Message)
                
                End Try

            Case "Update"
               
                Try

                    DataGridView1.EndEdit()
                    adapter.Update(dt)

                    MessageBox.Show("Table Updated!")
                Catch ex As MySqlException
                    MessageBox.Show("Error1: " & ex.Message)
                 
                End Try
        End Select

    End Sub

    Private Sub setConnection()
        Try
            conn = New MySqlConnection
            'locahost,test,root,quest
            conn.ConnectionString = "Server='" & txtServer.Text & "';Database='" & txtDB.Text & "';UID='" & txtUID.Text & "';Password='" & txtPwd.Text & "'"
            conn.Open()
            p_error = 0
        Catch ex As MySqlException
            MessageBox.Show("Error1: " & ex.Message)
            p_error = 1
        End Try

    End Sub

    Private Sub BtnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnExit.Click
        Application.Exit()
    End Sub


    Private Sub BtnFetch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnFetch.Click
        MainFunction("Select")
    End Sub

    Private Sub btnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect.Click
        If (txtDB.Text = "" Or txtPwd.Text = "" Or txtServer.Text = "" Or txtUID.Text = "") Then
            MessageBox.Show("Please complete above data! Thank you")
            Exit Sub
        End If
        If (var = 1) Then
            MessageBox.Show("A database already connected!")
            Exit Sub
        End If
        setConnection()
        If (p_error = 0) Then
            var = ConnectionState.Open
            MessageBox.Show("Connection Successfull!")
            p_error = 1
            FillCmbBox()
        End If


    End Sub

    Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
        MainFunction("Update")
        'DataGridView1.EndEdit()
        'adapter.Update(dt)
    End Sub


    Private Sub FillCmbBox()
        command = New MySqlCommand
        dt = New DataTable
        adapter = New MySqlDataAdapter
        command.Connection = conn
        command.CommandText = "SELECT TABLE_NAME FROM information_schema.TABLES WHERE TABLE_SCHEMA = 'test';"
        command.CommandType = CommandType.Text
        command.Connection = conn
        adapter = New MySqlDataAdapter(command)
        dt = New DataTable
        adapter.Fill(dt)
        Dim cb As New MySqlCommandBuilder(adapter)
        With cmbTable
            .DataSource = dt
            .DisplayMember = "Table_Name"

        End With

    End Sub

End Class
 
Last edited by a moderator:
You're setting 'command' to Nothing after using it so, unless you set it to something again after that, of course it's going to be Nothing when you try to execute that code the next time.
 
Back
Top