1

I don't know what is happening to my program, it took me (4) four minutes to load the result of my code.... can someone tell me why? Can someone tell me how to fix this loading problem?

This is my code:

Imports System.Data.SqlClient
Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim str As String = "Data Source=######;Initial Catalog=###;Persist Security Info=True;User ID=#####;Password=#####"
        Dim con As New SqlConnection(str)
        Dim cmd As String = "Select ControlNo,EmpNo,CheckOutDate,CheckOutTime,TaxiNo,PlateNo,Model,Make from dbo.ChkInOut"
        Dim adpt As New SqlDataAdapter(com, con)
        Dim myDataSet As New DataSet()
        adpt.Fill(myDataSet, "dbo.ChkInOut")
        Dim myDataTable As DataTable = myDataSet.Tables(0)
        Dim tempRow As DataRow
        For Each tempRow In myDataTable.Rows
            'ListBox1.Items.Add((tempRow("ControlNo") & " (" & tempRow("EmpNo") & ")" & " (" & tempRow("CheckOutDate") & ")" & " (" & tempRow("CheckOutTime") & ")" & " (" & tempRow("TaxiNo") & ")" & " (" & tempRow("PlateNo") & ")" & " (" & tempRow("Model") & ")" & " (" & tempRow("Make") & ")"))
            'ListBox1.Items.Add((tempRow("ControlNo") & " (" & tempRow("EmpNo") & ")"))
            ListBox1.Items.Add(tempRow("ControlNo") & "            " & tempRow("EmpNo") & "            " & tempRow("CheckOutDate") & "            " & tempRow("CheckOutTime") & "            " & tempRow("TaxiNo") & "            " & tempRow("PlateNo") & "            " & tempRow("Model") & "            " & tempRow("Make") & "            ")
        Next
    End Sub

End Class
10
  • What is the size of table ChkInOut ? total rows ? Commented Jan 30, 2013 at 5:58
  • Code looks fine - could be slightly improved by declaring a DataTable (instead of DataSet) and fill that data table, since you only ever needs one single table - but that's not going to make a huge difference.... was there a lot of other activity on the server at this point in time? Was that table e.g. locked by a large INSERT statement or something like that? Commented Jan 30, 2013 at 6:25
  • @Behroz - it's quite large, 188,874 rows that is the total rows Commented Jan 30, 2013 at 6:27
  • @marc_s how to use that DataTable that your talking about? Commented Jan 30, 2013 at 6:30
  • @Danjor: added an answer to show how to use a DataTable instead of DataSet Commented Jan 30, 2013 at 6:36

2 Answers 2

4

firstly, I must agree with the above question, how much data is being returned. In addition to that, can I suggest that rather than looping through the DataTable and populating the ListBox, you rather bind the data:

Dim myDataSet As New DataSet()
adpt.Fill(myDataSet, "dbo.ChkInOut")

ListBox1.DataTextField = "yourtext"
ListBox1.DataValueField = "yourvalue"
ListBox1.Datasource = myDataSet
ListBox1.DataBind()

This might just increase the performance.

Sign up to request clarification or add additional context in comments.

1 Comment

error occured : 'dataTextField' is not a member of 'System.Windows.Forms.ListBox'
4

apologies... thought is was a web application.

Try this:

ListBox1.DataSource = myDataTable 
ListBox1.DisplayMember = "ColumnName"

15 Comments

I tried it, it works but the problem is I want to display all the column to the ListBox1 so I tried to put it this way: ListBox1.DisplayMember = "ControlNo,EmpNo,CheckOutDate,CheckOutTime,TaxiNo,PlateNo,Model,Make" and there is a display message in ListBox1 'System.Data.DataRowView'
I tried to used atleast 2 columns it shows the same message, can you help me what to do about it?
in your SQL query, build up a column of all the values. then, use that column for this. e.g. SELECT blah1, blah2, blah1 + ' ' + blah2 as FinalBlah FROM tablename then use FinalBlah as your value for the listbox - make sense?
wahh! it's hard to understand! what is the 'blah!' 'blah!' :(
SELECT col1, col2, col1 + ' ' + col2 as FinalCol FROM tablename. This help?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.