6 Replies - 5754 Views - Last Post: 02 October 2012 - 06:01 AM Rate Topic: -----

#1 ruzel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 23-September 12

how do i display data from database to listview?

Posted 01 October 2012 - 12:45 AM

is my code correct?
can you help me with the error..
thanks....

Dim r As New ADODB.Recordset
Dim con As New ADODB.Connection
con.ConnectionString = "PROVIDER=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath & "\Database1.accdb"
con.Open()
ListView1.Items.Clear()
r = con.Execute("SELECT * FROM convert")
Dim i As Integer
i = 0
r.MoveFirst()
Do
ListView1.Items.Add(r.Fields(0).Value)
ListView1.Items(i).SubItems.Add(r.Fields(1).Value)
ListView1.Items(i).SubItems.Add(r.Fields(2).Value)
ListView1.Items(i).SubItems.Add(r.Fields(3).Value)
ListView1.Items(i).SubItems.Add(r.Fields(4).Value)
i = i + 1
r.MoveNext()
Loop Until r.EOF

Is This A Good Question/Topic? 0
  • +

Replies To: how do i display data from database to listview?

#2 artemix22  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 131
  • Joined: 21-January 12

Re: how do i display data from database to listview?

Posted 01 October 2012 - 01:34 AM

don't know with listview, but i can do this with DGV
Was This Post Helpful? 0
  • +
  • -

#3 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1533
  • View blog
  • Posts: 4,919
  • Joined: 25-September 09

Re: how do i display data from database to listview?

Posted 01 October 2012 - 05:19 AM

Have you thought about using the ADO.Net libraries for accessing your database?

It's very similar code.

What exactly is the error your getting?
Was This Post Helpful? 0
  • +
  • -

#4 jgferguson  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 31
  • Joined: 11-July 12

Re: how do i display data from database to listview?

Posted 01 October 2012 - 03:37 PM

Hi,

Been a while since I used ADO but the method was somethign like:-


dim r as ADODB.Recordset

dim cn as ADODB.Connection

dim sql as string = "select  ... "

r.open(sql, cn)

while not r.eof

dim lItem as ListViewItem = me.listview1.Items.add(r("FieldName"))
lItem.Subitems.Add(r("Fieldname2")
'and so on until all columns populated

r.MoveNext

wend





As CharlieMay says you should try using ADO.NET - a recordset can be replaced by a data reader and in the end uses less lines of code (no .MoveNext required).

Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#5 ruzel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 23-September 12

Re: how do i display data from database to listview?

Posted 01 October 2012 - 08:52 PM

View Postartemix22, on 01 October 2012 - 01:34 AM, said:

don't know with listview, but i can do this with DGV



Can you give me an idea using data grid view?
Was This Post Helpful? 0
  • +
  • -

#6 ruzel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 23-September 12

Re: how do i display data from database to listview?

Posted 01 October 2012 - 09:14 PM

View Postjgferguson, on 01 October 2012 - 03:37 PM, said:

Hi,

Been a while since I used ADO but the method was somethign like:-


dim r as ADODB.Recordset

dim cn as ADODB.Connection

dim sql as string = "select  ... "

r.open(sql, cn)

while not r.eof

dim lItem as ListViewItem = me.listview1.Items.add(r("FieldName"))
lItem.Subitems.Add(r("Fieldname2")
'and so on until all columns populated

r.MoveNext

wend





As CharlieMay says you should try using ADO.NET - a recordset can be replaced by a data reader and in the end uses less lines of code (no .MoveNext required).

Hope this helps.



i already changed my codes..
sorry, i really dont have an idea with my problem..

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#7 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1533
  • View blog
  • Posts: 4,919
  • Joined: 25-September 09

Re: how do i display data from database to listview?

Posted 02 October 2012 - 06:01 AM

Why did you change your connection to an OLEDB.OLEDBConnection when you originally had ADODB.Connection and are still trying to perform ADODB commands?

Here is a short example of using OleDB with the Reader object.

'instantiate an OleDB connection
Dim con as New Oledb.OledbConnection("PROVIDER=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath & "\Database1.accdb"
'Set up an SQL statement
Dim strSQL as string = "SELECT * FROM convert"
con.Open 'Open the connection
'Instantiate a command object and pass it your sql string variable along with the connection to use
Dim cmd as new Oledb.OledbCommand(strSQL, con)

'Here is where the reader comes in and execute reader is performed using the command object you specified above
Dim myReader as Oledb.OledbDataReader = cmd.ExecuteReader
'You now have a reader populated with any results that were produced from your Query.

'To get to these results you can loop through it 
While myReader.Read() ' Start a loop and keep running it until there are no more rows.
'Here is where you populate your objects that you use to display the row information.
'for the purpose of this explanation, I will display a msgbox of the first column
  MessageBox.Show(myReader(0).ToString) 'You can also specify the field name instead of the index number
End While
con.Close()


I wrote this without the use of the IDE so there may be errors but I don't think I missed anything of importance.

This post has been edited by CharlieMay: 02 October 2012 - 06:04 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1