1 Replies - 2292 Views - Last Post: 18 November 2009 - 02:59 AM Rate Topic: -----

#1 greenbluekidz   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 59
  • Joined: 05-January 09

How do I add records from an Access DB to a ListView control in VB.Net

Post icon  Posted 18 November 2009 - 12:32 AM

I am developing a Windows application that reads from a database and creates a total and subtotals for the records in the database. I am supposed to include a Try...Catch statement that reads the records in an Access db table and adds an item to the ListView control for each record. Each field in the record should be added as a column in the ListView control.

I have already gone through and created my Form_Load event that creates the columns needed in the ListView. I have created the beginning of the Try...Catch statement that opens the database connection. I do not know the syntax I should be using to add the record to a ListView control.

Normally I would use something along the lines of to read the data:

strOutput &= "Product Name: " & Convert.ToString(objDataReader.Item("ProductName")) & ControlChars.Tab & ControlChars.NewLine




Can someone provide me with an example that I can use? My text has no examples regarding this and I am a little overwhelmed with what Google is presenting me.

My complete code thus far is:

Option Strict On
Imports System.Data.OleDb

Public Class Form1
	Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		' Adding ListView Columns
		lstDatabaseInfo.Columns.Add("ProductID", 70, HorizontalAlignment.Left)
		lstDatabaseInfo.Columns.Add("ProductName", 80, HorizontalAlignment.Left)
		lstDatabaseInfo.Columns.Add("NumberInStock", 95, HorizontalAlignment.Left)
		lstDatabaseInfo.Columns.Add("Location", 70, HorizontalAlignment.Left)
		lstDatabaseInfo.Columns.Add("DateOfLastReorder", 110, HorizontalAlignment.Left)
		lstNumberAndLocation.Columns.Add("NumberInStock", 95, HorizontalAlignment.Left)
		lstNumberAndLocation.Columns.Add("Location", 70, HorizontalAlignment.Left)
	End Sub
	Private Const gstrConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
													"Data Source=C:\Chapter10\Inventory.mdb"
	Private Function InventoryTotals() As String
		Dim objConnection As OleDbConnection
		Dim objDataReader As OleDbDataReader
		Dim objCommand As OleDbCommand
		Dim strReport As String = ""

		Try
			objConnection = New OleDbConnection(gstrConnectionString)
			objCommand = New OleDbCommand()
			objCommand.Connection = objConnection
			objCommand.Connection.Open()
			objCommand.CommandText = "SELECT * FROM Inventory ORDER BY Location"
			objDataReader = objCommand.ExecuteReader
			Do While (objDataReader.Read())

			Loop
			objDataReader.Close()
			Return strReport
		Catch objException As Exception
			MessageBox.Show("A database error occurred.", "Database error")
			End
		End Try
	End Function




Thanks in advance!

Is This A Good Question/Topic? 0
  • +

Replies To: How do I add records from an Access DB to a ListView control in VB.Net

#2 greenbluekidz   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 59
  • Joined: 05-January 09

Re: How do I add records from an Access DB to a ListView control in VB.Net

Posted 18 November 2009 - 02:59 AM

I figured it out...thanks!!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1