0

I have a code which populates data in a datarepeater control, but that takes about 10 secs to load and freezes the UI so i though of using a backgroundworker.

But when I use the code in the backgroundworker it gives me saying 'Additional information: Object reference not set to an instance of an object.' as soon it reaches the datarepeater.Controls("namee of control").Text

Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
    'Sleep for a second, then update the textbox.
    Thread.Sleep(1000)




    Me.DataTableServicesTableAdapter.FillByBarberId(Me.ServicesDataSet.DataTableServices, barberId)



    Dim drNew As DataRow

    drNew = Me.ServicesDataSet.DataTableServices.NewRow()

    drNew.Item(2) = "Please select service"
    drNew.Item(3) = 0
    drNew.Item(9) = 0
    Me.ServicesDataSet.DataTableServices.Rows.InsertAt(drNew, 0)

    Dim currentItem As Microsoft.VisualBasic.PowerPacks.DataRepeaterItem
    Dim queueinClass As New IQB.cQueuing


    For i As Integer = 0 To DataRepeater1.ItemCount - 1
        DataRepeater1.CurrentItemIndex = i
        currentItem = DataRepeater1.CurrentItem
        Dim ServiceName As String = currentItem.Controls("NameLabel1").Text.Trim





        Dim sTime As Integer = currentItem.Controls("EstWaitTimeLabel1").Text
        Dim lblTime As Label = currentItem.Controls("EstWaitTimeLabel1")
        Dim Price As Double = currentItem.Controls("PriceLabel1").Text
        Dim lblPrice As Label = currentItem.Controls("PriceLabel1")

        ' set values





        ' Dim currency = CultureInfo.CurrentCulture

        Dim currencySymbol As String = System.Globalization.CultureInfo.CurrentCulture.NumberFormat.CurrencySymbol

        lblPrice.Text = currencySymbol & Math.Round(Price, 2)
        ' get estimated wait time
        Dim total As Integer = sTime


        Dim TotalMinute As Int32

        Dim Minute As Int32
        Dim Hour As Int32

        Try
            TotalMinute = CType(total, Int32)

            TotalMinute = TotalMinute Mod 1440

            Hour = TotalMinute \ 60
            Minute = TotalMinute Mod 60

            If Hour = 0 And Minute = 0 Then
                lblTime.Text = Hour & ":" & Form1.FormatTwoDigits(Minute) + " Mins"
            End If
            If Hour = 1 And Minute = 0 Then
                lblTime.Text = Hour & ":" & Form1.FormatTwoDigits(Minute) + " Hour"
            End If
            If Hour > 1 And Minute = 0 Then
                lblTime.Text = Hour & ":" & Form1.FormatTwoDigits(Minute) + " Hours"
            End If
            If Hour >= 1 And Minute <> 0 Then
                lblTime.Text = Hour & ":" & Form1.FormatTwoDigits(Minute) + " Mins"
            End If
            If Hour = 0 And Minute > 0 Then
                lblTime.Text = Hour & ":" & Form1.FormatTwoDigits(Minute) + " Mins"
            End If

        Catch ex As Exception

        End Try


        If ServiceName = "Please select service" Then
            lblTime.Text = ""
            lblPrice.Text = ""
        End If






    Next





End Sub
Trevor
  • 7,777
  • 6
  • 31
  • 50
  • 1
    Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it). Please see that link, it will address your exact issue. Also if you set breakpoints and step through the code, it will break on the line that is causing the issue... – Trevor Oct 06 '17 at 13:57
  • There problem is that the backgroundworker cannot access the control, hence why the error message, I have looked at using the invoke method but it don't seems do work the way I want I tried doing this: DataRepeater1.Invoke(Sub() Dim lblTest As Label = DataRepeater1.CurrentItem.Controls("NameLabel1") ServiceName = lblTest.Text Me.Refresh() End Sub) – user2857802 Oct 06 '17 at 14:01
  • 1
    Most important about writing code in the DoWork event handler is to ensure that such code is thread-safe. it just isn't, neither DataRepeater nor the controls you access through its Controls items nor their Text property should ever be used in a worker thread. Focus on *only* executing the query to obtain the data, the slow part. Use RunWorkerCompleted to use the results. – Hans Passant Oct 06 '17 at 14:03
  • The problem is the for loop where it gets the information for the datarepeater and takes 10 secs. that is why wanted to do this in the background. – user2857802 Oct 06 '17 at 14:14
  • A loop just isn't a problem, store the results in collection like a List(Of Something). – Hans Passant Oct 06 '17 at 14:36
  • @HansPassant can you explain better please, like an example of how to store these vaules and them displaying them after the do_Work Dim ServiceName As String = currentItem.Controls("NameLabel1").Text.Trim Dim sTime As Integer = currentItem.Controls("EstWaitTimeLabel1").Text Dim lblTime As Label = currentItem.Controls("EstWaitTimeLabel1") Dim Price As Double = currentItem.Controls("PriceLabel1").Text Dim lblPrice As Label = currentItem.Controls("PriceLabel1") – user2857802 Oct 06 '17 at 14:39

0 Answers0