angel7170
asked on
Populate a Listview with data from SQL database in VB.Net 2008
Hello,
I have a form with a listview of two columns "Employee Name" and "Component"
Now, I need to populate data for these two columns from a SQL database dataset.
How to achieve that?
Please assist.
Thank you
I have a form with a listview of two columns "Employee Name" and "Component"
Now, I need to populate data for these two columns from a SQL database dataset.
How to achieve that?
Please assist.
Thank you
ASKER
Hi, I tried using like this, but it's giving an error message "Execute Reader
: Connection Property has not been initilalized.
Please help. Thank you
Dim connectionstring As String = "Data Source=SAEOP201\OVOPS; Password=P44df?x&PQ9Ofyzxh 19u; User Id=ReportTableViewer"
Dim intNumError As Integer
Dim SQLCMD As SqlClient.SqlCommand
Dim rdGetData As SqlClient.SqlDataReader
Using Miconnection As New SqlClient.SqlCommand(conne ctionstrin g)
Try
If ListView1.Items.Count > 0 Then
ListView1.Items.Clear()
End If
SQLCMD = New SqlClient.SqlCommand("SELE CT Full_name, Component from VW_CONTACTS")
rdGetData = SQLCMD.ExecuteReader
Dim intCount As Decimal = 0
While rdGetData.Read
ListView1.Items.Add("Full_ name") 'col no. 1
ListView1.Items.Add("Compo nent") 'col no. 2
intCount = intCount + 1
End While
rdGetData.Close()
rdGetData = Nothing
Catch Exp As Exception
intNumError = Err.Number()
MsgBox("[ " & CStr(intNumError) + " ] " + Err.Description, MsgBoxStyle.Critical, " (Program Error)")
End Try
End Using
: Connection Property has not been initilalized.
Please help. Thank you
Dim connectionstring As String = "Data Source=SAEOP201\OVOPS; Password=P44df?x&PQ9Ofyzxh
Dim intNumError As Integer
Dim SQLCMD As SqlClient.SqlCommand
Dim rdGetData As SqlClient.SqlDataReader
Using Miconnection As New SqlClient.SqlCommand(conne
Try
If ListView1.Items.Count > 0 Then
ListView1.Items.Clear()
End If
SQLCMD = New SqlClient.SqlCommand("SELE
rdGetData = SQLCMD.ExecuteReader
Dim intCount As Decimal = 0
While rdGetData.Read
ListView1.Items.Add("Full_
ListView1.Items.Add("Compo
intCount = intCount + 1
End While
rdGetData.Close()
rdGetData = Nothing
Catch Exp As Exception
intNumError = Err.Number()
MsgBox("[ " & CStr(intNumError) + " ] " + Err.Description, MsgBoxStyle.Critical, " (Program Error)")
End Try
End Using
You need to add your connection on your sqlCommand
SQLCMD = New SqlClient.SqlCommand("SELE CT Full_name, Component from VW_CONTACTS", Miconnection )
SQLCMD = New SqlClient.SqlCommand("SELE
ASKER
It's giving an error when I added the connection
Value of type 'System.Data.SQLClient.SQL Command cannot be converted to 'System.Data.SQLClient.SQL Connection
Please help.
Value of type 'System.Data.SQLClient.SQL
Please help.
Dim connectionstring As String = "Data Source=SAEOP201\OVOPS; Password=P44df?x&PQ9Ofyzxh 19u; User Id=ReportTableViewer"
Dim conn As New SqlConnection(connString)
Dim cmdString As String = "SELECT Full_name, Component from VW_CONTACTS"
Dim cmd As New SqlCommand(cmdString, conn)
conn.Open()
Dim reader As SqlDataReader = cmd.ExecuteReader()
If ListView1.Items.Count > 0 Then
ListView1.Items.Clear()
End If
While reader.Read()
Dim str(2) As String
Dim itm As ListViewItem
str(0) = reader("Full_name").ToStri ng()
str(1) = reader("Component").ToStri ng()
itm = New ListViewItem(str)
ListView1.Items.Add(itm)
End While
conn.Close()
Dim conn As New SqlConnection(connString)
Dim cmdString As String = "SELECT Full_name, Component from VW_CONTACTS"
Dim cmd As New SqlCommand(cmdString, conn)
conn.Open()
Dim reader As SqlDataReader = cmd.ExecuteReader()
If ListView1.Items.Count > 0 Then
ListView1.Items.Clear()
End If
While reader.Read()
Dim str(2) As String
Dim itm As ListViewItem
str(0) = reader("Full_name").ToStri
str(1) = reader("Component").ToStri
itm = New ListViewItem(str)
ListView1.Items.Add(itm)
End While
conn.Close()
ASKER
Nothing happens. I have placed this code in
Tabpage2_Click property...
Tabpage2_Click property...
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
note...
The Public Sub FillList you must set it in your Form's Class (Form Code)
The Public Sub FillList you must set it in your Form's Class (Form Code)
ASKER
Works awesome. Thank you very much jtoutou
ASKER
Thank you Dhaest for your assistance too..
Glad i helped you
http://www.daniweb.com/forums/thread49338.html
Open in new window