14 Replies - 17793 Views - Last Post: 10 February 2018 - 06:48 PM Rate Topic: -----

#1 nitin.magdum   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 09-July 11

Display MS Access Data in Listbox

Posted 09 July 2011 - 11:46 PM

Hi,
I need to populate a listbox with my MS Access data (database consists 1 column). I'm unable to achieve this using the code I wrote below. Can anyone guide me the right way to do it. Thanks!

Error: "Object reference not set to an instance of an object." Line: dbConnection.Open()

[Imports System.Data.OleDb
Imports System.Data.SqlClient
Imports System

Public Class Form1
Dim dbConnection As OleDbConnection
Dim dbCommand As OleDbCommand
Dim strInsert As String
Dim dbDataAdapter As OleDbDataAdapter
Dim ConnectString As String = "Provider = Microsoft.Jet.OLEDB.4.0;" & "Data Source =atg.mdb"

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

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim command As OleDbCommand = New OleDbCommand("SELECT Term FROM ATG")

dbConnection.Open()
Dim reader As OleDbDataReader = dbCommand.ExecuteReader()
Do While (reader.Read())
ListBox1.Items.Add(reader.GetInt32(0))
Loop
reader.Close()
dbConnection.Close()


End Sub
End Class]

Is This A Good Question/Topic? 0
  • +

Replies To: Display MS Access Data in Listbox

#2 smohd   User is offline

  • Critical Section
  • member icon


Reputation: 1824
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Display MS Access Data in Listbox

Posted 09 July 2011 - 11:57 PM

It is because you didnt initialize it. It should be
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim command As OleDbCommand = New OleDbCommand("SELECT Term FROM ATG")
dbConnection = New OleDbConnection(ConnectString)
'I initialize it with connection string before opening the connection
dbConnection.Open()
Dim reader As OleDbDataReader = dbCommand.ExecuteReader()
Do While (reader.Read())
ListBox1.Items.Add(reader.GetInt32(0))
Loop
reader.Close()
dbConnection.Close() 
End Sub 


Also :code:

This post has been edited by smohd: 09 July 2011 - 11:58 PM

Was This Post Helpful? 0
  • +
  • -

#3 nitin.magdum   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 09-July 11

Re: Display MS Access Data in Listbox

Posted 10 July 2011 - 12:04 AM

Hey Thanks,
However, I get this error once I run it.
Error: Object reference not set to an instance of an object. For the code: Dim reader As OleDbDataReader = dbCommand.ExecuteReader()
Was This Post Helpful? 0
  • +
  • -

#4 smohd   User is offline

  • Critical Section
  • member icon


Reputation: 1824
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Display MS Access Data in Listbox

Posted 10 July 2011 - 12:24 AM

Also you have to initialize your command with connection before use it for execution:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
dbConnection = New OleDbConnection(ConnectString)
'I initialize it with connection string before opening the connection
dbConnection.Open()
'It comes here after opening the connection
Dim command As New OleDbCommand("SELECT Term FROM ATG", dbConnection)
Dim reader As OleDbDataReader = dbCommand.ExecuteReader()
Do While (reader.Read())
ListBox1.Items.Add(reader.GetInt32(0))
Loop
reader.Close()
dbConnection.Close() 
End Sub 

Was This Post Helpful? 1
  • +
  • -

#5 noorahmad   User is offline

  • Untitled
  • member icon

Reputation: 209
  • View blog
  • Posts: 2,290
  • Joined: 12-March 09

Re: Display MS Access Data in Listbox

Posted 10 July 2011 - 12:25 AM

Don't forget to assign connection for OleDbCommand

OleDbCommand("SELECT Term FROM ATG", dbConnection )

Edit:
smohd, you are to fast... ;)

This post has been edited by noorahmad: 10 July 2011 - 12:27 AM

Was This Post Helpful? 1
  • +
  • -

#6 nitin.magdum   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 09-July 11

Re: Display MS Access Data in Listbox

Posted 10 July 2011 - 12:51 AM

Could you elaborate a little.. I'm just a beginner...
Was This Post Helpful? 0
  • +
  • -

#7 smohd   User is offline

  • Critical Section
  • member icon


Reputation: 1824
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Display MS Access Data in Listbox

Posted 10 July 2011 - 01:11 AM

This is how we do, in Object Oriented we initialize objects using the keyword new . Here we have two objects that needs to be initialized. The first one is a connection(here is dbConnection which is an object of OleDbConnection), and we pass connection string to its constructor .The second one is command which works as executor in DBMS(like when you click on execute when you are in real DBMS) which works after the connection is opened and we pass to its constructor query(SQL query to be executed) and an open connection(so it can know which connection we are working with) because the database must be open to a certain user so we can execute query.
If you have any problem understanding that, feel free to say it :)
Was This Post Helpful? 1
  • +
  • -

#8 nitin.magdum   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 09-July 11

Re: Display MS Access Data in Listbox

Posted 10 July 2011 - 01:37 AM

Thanks for the detailed explanation smohd & for the patience :)
So to initialize the command do I use [dbCommand = New OleDbCommand] also, isn't it already initialized by [Dim command As New OleDbCommand("SELECT Term FROM ATG", dbConnection) ]
Could you just tell me the exact code and how it works?

Thanks again..
Was This Post Helpful? 0
  • +
  • -

#9 smohd   User is offline

  • Critical Section
  • member icon


Reputation: 1824
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Display MS Access Data in Listbox

Posted 10 July 2011 - 01:53 AM

That means it has been initialized, but before you didnt include the connection. The last code I posted should work, unless if any problem happen please feel free to post it.
 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
dbConnection = New OleDbConnection(ConnectString) 'here I use the new keyword to initialize connection
'I initialize it with connection string before opening the connection
dbConnection.Open()
'It comes here after opening the connection
Dim command As New OleDbCommand("SELECT Term FROM ATG", dbConnection) ' initialize command and pass query and open connection to its constructor
Dim reader As OleDbDataReader = dbCommand.ExecuteReader() 'execute the query using the connection given and assign output to the reader
Do While (reader.Read()) ' loop here until reader finish reading and add every row to the list box
'I made some changes here, first update the listbox
ListBox1.BeginUpdate() 
ListBox1.Items.Add(reader.Item("Column name"))'this is more clearly to use column name 
ListBox1.EndUpdate()
Loop
reader.Close()
dbConnection.Close() 
End Sub  

Was This Post Helpful? 2
  • +
  • -

#10 nitin.magdum   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 09-July 11

Re: Display MS Access Data in Listbox

Posted 10 July 2011 - 05:17 AM

Hey smohd,
Looks like there is something to do with Adding References and the code is prefectly fine.
Attached File  Doc1.doc (196K)
Number of downloads: 535
Was This Post Helpful? 0
  • +
  • -

#11 Gasayap   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 10-February 18

Re: Display MS Access Data in Listbox

Posted 10 February 2018 - 06:27 PM

View Postsmohd, on 10 July 2011 - 01:53 AM, said:

That means it has been initialized, but before you didnt include the connection. The last code I posted should work, unless if any problem happen please feel free to post it.
 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
dbConnection = New OleDbConnection(ConnectString) 'here I use the new keyword to initialize connection
'I initialize it with connection string before opening the connection
dbConnection.Open()
'It comes here after opening the connection
Dim command As New OleDbCommand("SELECT Term FROM ATG", dbConnection) ' initialize command and pass query and open connection to its constructor
Dim reader As OleDbDataReader = dbCommand.ExecuteReader() 'execute the query using the connection given and assign output to the reader
Do While (reader.Read()) ' loop here until reader finish reading and add every row to the list box
'I made some changes here, first update the listbox
ListBox1.BeginUpdate() 
ListBox1.Items.Add(reader.Item("Column name"))'this is more clearly to use column name 
ListBox1.EndUpdate()
Loop
reader.Close()
dbConnection.Close() 
End Sub  


Hi, Error in line..

Dim reader As OleDbDataReader = dbCommand.ExecuteReader()


NullReferenceException was Unhandled {"Object reference not set to an instance of an object."}
Was This Post Helpful? 0
  • +
  • -

#12 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14094
  • View blog
  • Posts: 56,469
  • Joined: 12-June 08

Re: Display MS Access Data in Listbox

Posted 10 February 2018 - 06:30 PM

Is that a question or a statement?

(mind you the post was from 2011.. so.. you know.. old topic)
Was This Post Helpful? 0
  • +
  • -

#13 Gasayap   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 10-February 18

Re: Display MS Access Data in Listbox

Posted 10 February 2018 - 06:45 PM

View Postmodi123_1, on 10 February 2018 - 06:30 PM, said:

Is that a question or a statement?

(mind you the post was from 2011.. so.. you know.. old topic)


Im asking for a correct code. BTW im a bigenner
Was This Post Helpful? 0
  • +
  • -

#14 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14094
  • View blog
  • Posts: 56,469
  • Joined: 12-June 08

Re: Display MS Access Data in Listbox

Posted 10 February 2018 - 06:46 PM

Perhaps start a new topic showing your code, the issue explained, and the complete error messages.
Was This Post Helpful? 0
  • +
  • -

#15 Gasayap   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 10-February 18

Re: Display MS Access Data in Listbox

Posted 10 February 2018 - 06:48 PM

Beginner*

Ah.. ok thanks
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1