Click here to Skip to main content
15,894,337 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am designing an EV Tracker for Pokemon: Dawn of Darkness and I had everything working in a beta setup but I'm having trouble converting it to a workable finish.

The original design for this program had a database with just one table and it pulled the information from that table. However, this was no conducive to people who had multiple account. So, I am making changes that will create a new table for each account the player has, they can add new accounts to the list by click File > New, giving it an account name, and choosing a starter pokemon. Then they click Confirm and it creates a new table for the account and inserts a record for their starter pokemon.

The first thing that comes up when the program starts is an account select page, which contains a confirm button, cancel button, and a combobox that is populated with the names of every table in the database. The program is supposed to populate the different controls on Form1 based on the data in the table selected during this first part, and it's supposed to populate the data right after the user clicks okay. However, I get an error on the line that is supposed to fill the dataset.

I know some people are going to get upset that I'm using global variables but it's the only way I could think of do this, so please try to move past it.

Imports System.Data.OleDb

Public Class LoginForm1

    Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click

        'closes the loginform1 and reopens the main form
        Me.Close()
        Form1.ShowInTaskbar = True
        Form1.WindowState = FormWindowState.Normal

        Form1.PokeTeam.Items.Clear()

        DbConnection.ConnectionString = strConnectionString

        DbConnection.Open()

        Dim SqlQry As String = "SELECT * FROM " & ActiveDb & ""

        DbConnection = New OleDbConnection(strConnectionString)

        Try
            ' Open connection
            DbConnection.Open()

            'create data adapter
            Dim da As OleDbDataAdapter = New OleDbDataAdapter(SqlQry, DbConnection)

            'create dataset
            Dim ds As DataSet = New DataSet

            'fill dataset
            da.Fill(ds, ActiveDb)

            'get data table
            Dim dt As DataTable = ds.Tables(ActiveDb)

            'display data
            Dim row As DataRow

            'populate the combobox with all the pokemon listed in the table.
            For Each row In dt.Rows
                Form1.PokeTeam.Items.Add(row("Pokemon"))
            Next row
        Catch ex As OleDbException
            MsgBox("Error: " & ex.ToString & vbCrLf)
        Finally
            ' Close connection
            DbConnection.Close()
        End Try
    End Sub

    Private Sub Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel.Click
        Me.Close()
        Form1.ShowInTaskbar = True
        Form1.WindowState = FormWindowState.Normal
    End Sub

    Private Sub LoginForm1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        'retrieves the table names from the database and populates the combobox
        Module1.ActiveDb = "PokemonTable"
        Dim dtTableNames As DataTable
        Using DbConnection As New OleDb.OleDbConnection(Module1.strConnectionString)
            DbConnection.Open()
            dtTableNames = DbConnection.GetOleDbSchemaTable(OleDb.OleDbSchemaGuid.Tables, _
                New Object() {Nothing, Nothing, Nothing, "TABLE"})
        End Using
        Dim dr As DataRow
        For Each dr In dtTableNames.Rows
            AccountCbo.Items.Add(dr.Item("TABLE_NAME"))
        Next
        Console.ReadLine()

        AccountCbo.SelectedIndex = 0

        Module1.ActiveDb = AccountCbo.SelectedItem
    End Sub
End Class


The error comes up after I click OK and it points me to da.Fill(ds, ActiveDb)

The ActiveDb variable, once I get this part working, will be used throughout the code to determine which table the information will be pulled from. Can anybody tell me why this code isn't working? I can provide more information if this isn't enough.
Posted

1 solution

I found the answer myself and it was plain as day, I can't believe I didn't see it sooner. I never declared a new instance for the ActiveDb variable. it was as simple as adding

Module1.ActiveDb = New String("PokemonTable")


to the start of the code block.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900