Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
By using the following code I can get only one column in database rather than all columns,

VB
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ListView1.Items.Clear()
        ' Create the column headers.
        ListView1.Columns.Add("Id", 10, HorizontalAlignment.Left)
        ListView1.Columns.Add("Word", 10, HorizontalAlignment.Left)
        ListView1.Columns.Add("Meaning", 10, HorizontalAlignment.Left)
        ListView1.Columns.Add("Gujarati", 10, HorizontalAlignment.Left)
        ListView1.Columns.Add("Syn", 10, HorizontalAlignment.Right)
        ListView1.Columns.Add("Ant", 10, HorizontalAlignment.Left)
        ' Start with details view.
        Dim cn As SqlConnection = GetDbConnection()
        ' Select records.


        Dim cmd As New SqlCommand("select * from Dictionary", cn)
        Dim data_reader As SqlDataReader = cmd.ExecuteReader()
        'data_reader = cmd.ExecuteReader()

        Do While data_reader.Read()
            Dim new_item As New ListViewItem(data_reader.Item("Id").ToString)
            new_item.SubItems.Add(data_reader.Item("Word").ToString)
            new_item.SubItems.Add(data_reader.Item("Meaning").ToString)
            new_item.SubItems.Add(data_reader.Item("Gujarati").ToString)
            new_item.SubItems.Add(data_reader.Item("Syn").ToString)
            new_item.SubItems.Add(data_reader.Item("Ant").ToString)
            ListView1.Items.Add(new_item)
            Debug.WriteLine(new_item.Text & " : " & _
                new_item.SubItems(0).Text & ", " & _
                new_item.SubItems(1).Text & ", " & _
                new_item.SubItems(2).Text & ", " & _
                new_item.SubItems(3).Text & ", " & _
                new_item.SubItems(4).Text)
        Loop
        'Dim wid As Integer
        'For i As Integer = 0 To ListView1.Columns.Count - 1
        '    ListView1.Columns(i).Width = -2
        '    wid += ListView1.Columns(i).Width
        'Next i
        'Me.ClientSize = New Size(wid + 20, Me.ClientSize.Height)


    End Sub
    Private Function GetDbConnection() As SqlConnection

        ' Compose the connect string.
        Dim cn As New SqlConnection("Data Source=SANJAY-26EEFF19\SQLEXPRESS;Initial Catalog=Dict;Integrated Security=True")
        ' Open a database connection.
        cn.Open()
        ' Return the connection.
        Return cn
    End Function
Posted
Updated 21-Mar-11 1:37am
v3
Comments
Abdul Quader Mamun 19-Mar-11 6:50am    
I think this is not proper code for binding.
Dalek Dave 19-Mar-11 7:24am    
I think you're right.
sima2011 19-Mar-11 10:38am    
then pls suggest me proper code for binding database through listview

Have a read of this article

listview with a datareader[^]

That is using the technique you are using. But databinding is done using

dim dt as datatable 

'fill datatable here 

listview1.datasource = dt
 
Share this answer
 
v2
MIDL
Dim cn As New SqlConnection("Data Source=SANJAY-26EEFF19\SQLEXPRESS;Initial Catalog=Dict;Integrated Security=True")



plz replcae the SANJAY-26EEFF19 with you computer name and Dict with your database name.


Dim cmd As New SqlCommand("select * from Dictionary", cn)


replcae the Dictionary with your table name
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900