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
how do i display data from database to listview?
Page 1 of 16 Replies - 1988 Views - Last Post: 02 October 2012 - 06:01 AM
Replies To: how do i display data from database to listview?
#2
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
#3
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?
It's very similar code.
What exactly is the error your getting?
#4
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:-
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.
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.
#5
Re: how do i display data from database to listview?
Posted 01 October 2012 - 08:52 PM
#6
Re: how do i display data from database to listview?
Posted 01 October 2012 - 09:14 PM
jgferguson, on 01 October 2012 - 03:37 PM, said:
Hi,
Been a while since I used ADO but the method was somethign like:-
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.
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)
#7
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.
I wrote this without the use of the IDE so there may be errors but I don't think I missed anything of importance.
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
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote




|