Click here to Skip to main content
15,891,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
HI, I am new to vb.net. Can someone teach me how to show data from the sql database to the vb.net listview.

For example:
My db has a Data column, and i would like to have only the previous 20 days are showed on the listview.

If someone can provide me some source code that would be alot helpfull.

Thank you very much.
Posted
Updated 10-Mar-20 4:15am

C#
Dim rs As OleDb.OleDbDataAdapter    'Line 50
rs = New OleDb.OleDbDataAdapter("SELECT * FROM TableName WHERE(dDate >= @startDate) AND (dDate <= @endDate)", con)

Dim param1, param2 As OleDbParameter
param1 = New OleDbParameter("@startDate", datePicker1.Value.ToShortDateString)
param2 = New OleDbParameter("@endDate", datePicker2.Value.ToShortDateString)
rs.SelectCommand.Parameters.AddRange(New OleDbParameter() {param1, param2})
Dim dt As New DataTable("TableName")
rs.Fill(dt)

If dt.Rows.Count > 0 Then

End If
 
Share this answer
 
it is ok if it is vulnerable at the moment.
I am just trying to make a program for personal use. btw i edited some code and still cant make it run.



C#
 Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click


        mysqlconn = New MySqlConnection
        mysqlconn.ConnectionString = "server=localhost;user=root;password=root;database=database"
        Dim Reader As MySqlDataReader

        Dim strQ As String = String.Empty
        Dim conn As MySqlConnection
            Dim cmd As MySqlCommand
            Dim da As MySqlDataAdapter
            Dim ds As DataSet
            Dim itemcoll(100) As String
            strQ = "SELECT * FROM database.benta where customerID = '" & txtnos.Text "'"
            cmd = New SqlCommand(strQ, conn)
        da = New SqlDataAdapter(cmd)
        ds = New DataSet
        da.Fill(ds, "Table")
            Dim i As Integer = 0
            Dim j As Integer = 0
            ' adding the columns in ListView
            For i = 0 To ds.Tables(0).Columns.Count - 1
                Me.lva.Columns.Add(ds.Tables(0).Columns(i).ColumnName.ToString())
            Next
            'Now adding the Items in Listview
            For i = 0 To ds.Tables(0).Rows.Count - 1
                For j = 0 To ds.Tables(0).Columns.Count - 1
                    itemcoll(j) = ds.Tables(0).Rows(i)(j).ToString()
                Next
                Dim lvi As New ListViewItem(itemcoll)
                Me.lva.Items.Add(lvi)
            Next
End Sub
 
Share this answer
 
Imports System.Data.SqlClient
Public Class Form1
Dim conn As SqlConnection
Dim cmd As SqlCommand
Dim da As SqlDataAdapter
Dim ds As DataSet
Dim itemcoll(100) As String
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.ListView1.View = View.Details
Me.ListView1.GridLines = True
conn = New SqlConnection("Data Source=SQLEXPRESS;Initial Catalog=Northwind;Persist Security Info=True;User ID=id;Password=pass")
Dim strQ As String = String.Empty
strQ = "SELECT * FROM Northwind.dbo.Products"
cmd = New SqlCommand(strQ, conn)
da = New SqlDataAdapter(cmd)
ds = New DataSet
da.Fill(ds, "Table")
Dim i As Integer = 0
Dim j As Integer = 0
' adding the columns in ListView
For i = 0 To ds.Tables(0).Columns.Count - 1
Me.ListView1.Columns.Add(ds.Tables(0).Columns(i).ColumnName.ToString())
Next
'Now adding the Items in Listview
For i = 0 To ds.Tables(0).Rows.Count - 1
For j = 0 To ds.Tables(0).Columns.Count - 1
itemcoll(j) = ds.Tables(0).Rows(i)(j).ToString()
Next
Dim lvi As New ListViewItem(itemcoll)
Me.ListView1.Items.Add(lvi)
Next
End Sub
End Class
 
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