How can I populate a listview from another form passing the SQL statement from this form to the other form with the listview showing the result of the query?
You can create a sub procedure in the destination form that will take the SQL statment as a string. Then fill the ListView from that sub procedure.
'In DestinationForm Protected Friend Sub FillListView(ByVal sSQL As String) Dim da As New OleDbDataAdapter(sSQL, New OleDbConnection("YourStringHere")) Dim ds As New DataSet Try da.Fill(ds, "MyTable") If Not IsNothing(ds.Tables("MyTable")) Then If ds.Tables("MyTable").Rows.Count > 0 Then For Each dr As DataRow In ds.Tables("MyTable").Rows Dim lvi As New ListViewItem lvi.Text = dr("MyCol1") lvi.SubItems.Add(dr("MyCol2")) lvi.SubItems.Add(dr("MyCol3")) MyListView.Items.Add(lvi) Next Else MsgBox("No rows were returned by the query!", _ MsgBoxStyle.Information, "Oops!") End If Else MsgBox("No objects were returned from the database.", _ MsgBoxStyle.Information, "Oops!") End If Catch ex As Exception MsgBox(ex.Message, MsgBoxStyle.Critical, "Error!") Finally da.Dispose() ds.Dispose() End Try End Sub
To fill the ListView, call the procedure like so:
Edited 2 Years Ago by Begginnerdev
@Begginnerdev, Your codes are quite right. But it can perform an exception due to Closed DataConnection. Nowhere you open the Connection Object.
My apologies, I typed the previous code straight into the code window.
Don't mind, about the pointing to your codes unwilling mistake. It should be
Dim da As New OleDbDataAdapter(sSQL, New OleDbConnection("YourStringHere").Open)
Or also eit could be
Dim Conn as New OleDb.OledbConnection Conn.ConnectionString="Your Connection String" If Conn.State=OleDbconnection.Opened Then conn.Close() Conn.Open() Dim da As New OleDbDataAdapter(sSQL, Conn)
Isn't it about time forums rewarded their contributors?
Contribute to this discussion and earn rewards points that can be cashed out for dollars.
I have a 2d matrix with dimension (3, n) called A, I want to calculate the normalization and cross product of two arrays (b,z) (see the code please) for each column (for the first column, then the second one and so on).
the function that I created to find the ...
Hi. so this is actually a continuation from another question of mineHere but i was advised to start a new thread as the original question was already answered.
This is the result of previous question answered :
code for the listbox - datagridview interaction
At the top of the code ...
Hi. I have a form with list box : lst_product, datagridview : grd_order and button: btn_addline. lst_product has a list of product ids selected from database (MS Acess 2013) , grd_order is by default empty except for 2 headers and btn_addline adds rows to grd_order.
Private Sub btn_addline_Click(ByVal ...