Tristan S.
Tristan S.

Reputation: 21

Listview generating SQL data slowly, SQL queries in 1 sec, where generating data in listview takes 15+ seconds

How can I optimize this code to generate the data in the list view faster? The SQL query takes < 1 second to execute, but this code takes 15 seconds plus

Dim connn As SqlClient.SqlConnection
Dim cmdd As SqlClient.SqlCommand
Dim da As SqlClient.SqlDataAdapter
Dim ds As DataSet
Dim itemcoll(100) As String

If NameComboBox.Text = "" Then
    Me.JobList.Clear()
    Me.JobList.View = View.Details
    connn = New SqlClient.SqlConnection("Server Connection")
    Dim strQ As String = String.Empty
    strQ = "SELECT  Customer, job, Convert(Date, seals, 121) as [Complete]
    FROM Database Inner Join Job2 on Job1.jobref = Job2.jobreflink  
    WHERE [job] <> '' and seals <> '' and seals >= '1/1/2017' Order by jobnumber desc;"
    cmdd = New SqlClient.SqlCommand(strQ, connn)
    da = New SqlClient.SqlDataAdapter(cmdd)
    ds = New DataSet
    da.Fill(ds, "Table")
    Dim i As Integer = 0
    Dim j As Integer = 0
    For i = 0 To ds.Tables(0).Columns.Count - 1
    Me.JobList.Columns.Add(ds.Tables(0).Columns(i).ColumnName.ToString())
    Next
    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
         itemcoll(j) = ds.Tables(0).Rows(i)(j).ToString()
         Me.JobList.Items.Add(lvi)
    Next
End If

Upvotes: 1

Views: 70

Answers (1)

Aousaf Rashid
Aousaf Rashid

Reputation: 5738

why not use Listbox1.items.addrange rather than using oly add.?? it'll be a lot faster

Upvotes: 1

Related Questions