Problem with .accdb in VB

GooGyy

New member
Joined
Sep 2, 2011
Messages
2
Programming Experience
Beginner
HI!

I have a problem with getting data from Access database in VB. I have database file with 10 files and program which gets data from each database table and writes them into listbox. Now here is the problem: program gets column from table, that's planned, but it doesn't gets values for each row. it just writes for each row the same: "system.data.oledb.oledbdatareader". So.. how would i get values from table?

Here is the code:

Imports System.Data
Imports System.Data.OleDb
Imports System.Windows.Forms




Public Class Main_Menu
    Inherits System.Windows.Forms.Form
    Dim PwdCon As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath & "/ekratkočasnik.accdb;" 'Persist Security Info=False;" if your DB is not PWD protected
    Dim oledbcon As New OleDbConnection(PwdCon)


Private Sub IgreZaMalčkeToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles IgreZaMalčkeToolStripMenuItem.Click

        StatusLabel.Text = "Izbrana kategorija: IGRE ZA MALČKE"



        Try
            oledbcon.Open()
            'If oledbcon.State = ConnectionState.Open Then
            '    MessageBox.Show("opened")
            'End If
            Dim strSQL As String = "SELECT IME FROM Igre_za_Malčke"
            Dim cmd As OleDbCommand = New OleDbCommand(strSQL, oledbcon)
            Dim objread As OleDbDataReader
            objread = cmd.ExecuteReader
            While objread.Read
                Me.ListBox_IME.Items.Add(objread.ToString())
            End While
            objread.Close()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            oledbcon.Close()
            'If oledbcon.State = ConnectionState.Closed Then
            '    MessageBox.Show("closed")
            'End If
            Me.ListBox_IME.SelectedIndex = 0

        End Try


    End Sub


PS: Sorry for my english
 
Last edited by a moderator:
First up, let's correct your connection string. There's no need to inject Application.StartupPath into it because you can simply use |DataDirectory|. Also, it is a Windows file path, not a URL, so your slash is leaning the wrong way. This:
Dim PwdCon As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath & "/ekratkočasnik.accdb;"
should be this:
Dim PwdCon As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\ekratkočasnik.accdb;"
As for the question, the issue is that you're putting the wrong thing into the ListBox. You never actually get any data from the data reader. Each time you read a record, you call ToString on the data reader. That's not getting data. That's getting a String representation of that object. Like the majority of classes, data readers simply return the name of their type from ToString. If you want to display data from the data reader then you have to get data from the data reader. Check this out to learn how to properly use data readers and other ADO.NET objects:

Retrieving and Saving Data in Databases
 
Back
Top