Question Why is this slow (code)?

darshan

New member
Joined
Mar 23, 2021
Messages
2
Programming Experience
3-5
VB.NET:
Private Sub acdContracts_ItemDataBound(sender As Object, e As AccordionItemEventArgs) Handles acdContracts.ItemDataBound
        Dim rptContractDetails
        Dim dtcontractlines
        Dim dvContractDetails As DataView
        Dim strFilter As String
        Dim iContractNumber As Integer
        Dim drvContract As DataRowView
        Dim lblSale, lblPurch, lblTotalDifference As Label
        Dim bolShowSo, bolshowPo As Boolean
        Dim csc As ContractStatusCodes

        If rblContractType.SelectedValue = "SO" Then
            bolShowSo = True
            bolshowPo = False
        Else
            bolShowSo = False
            bolshowPo = True
        End If

        If e.ItemType = AccordionItemType.Header Then
            lblPurch = e.AccordionItem.FindControl("lblPurchNum")
            lblSale = e.AccordionItem.FindControl("LblSaleNum")
            lblPurch.Visible = bolshowPo
            lblSale.Visible = bolShowSo
            lblTotalDifference = e.AccordionItem.FindControl("lblTotalDifference")

            DirectCast(e.AccordionItem.FindControl("lblHeaderPurchStatus"), Label).Visible = bolshowPo
            DirectCast(e.AccordionItem.FindControl("lblHeaderSaleStatus"), Label).Visible = bolShowSo

            If Convert.ToInt64(lblTotalDifference.Text.Replace(",", "")) < 0 Then
                lblTotalDifference.CssClass = "Validationerror"
            End If

            csc = DirectCast(e.AccordionItem.FindControl("lblHeaderPurchStatus"), Label).Text

            If csc = ContractStatusCodes.ClosedByUser Then
                DirectCast(e.AccordionItem.FindControl("lblHeaderPurchStatus"), Label).Text = "Closed"
                DirectCast(e.AccordionItem.FindControl("lblHeaderFunction"), Label).Text = "Reopen"
            Else
                DirectCast(e.AccordionItem.FindControl("lblHeaderPurchStatus"), Label).Text = csc.ToString
            End If

            'lblHeaderSaleStatus
            csc = DirectCast(e.AccordionItem.FindControl("lblHeaderSaleStatus"), Label).Text

            If csc = ContractStatusCodes.ClosedByUser Then
                DirectCast(e.AccordionItem.FindControl("lblHeaderSaleStatus"), Label).Text = "Closed"
            Else
                DirectCast(e.AccordionItem.FindControl("lblHeaderSaleStatus"), Label).Text = csc.ToString
            End If

            If rblContractType.SelectedValue = "PO" Then
                DirectCast(e.AccordionItem.FindControl("lblName"), Label).Text = "Supplier"
                If DirectCast(Session(ContractHeaderSessionName), DataTable).Select("PurchNum = " & lblPurch.Text)(0).Item("SupplierName").ToString <> DBNull.Value.ToString Then
                    DirectCast(e.AccordionItem.FindControl("lblNameData"), Label).Text = DirectCast(Session(ContractHeaderSessionName), DataTable).Select("PurchNum = " & lblPurch.Text)(0).Item("SupplierName")
                End If
            Else
                DirectCast(e.AccordionItem.FindControl("lblName"), Label).Text = "Consumer"
                If DirectCast(Session(ContractHeaderSessionName), DataTable).Select("SaleNum = " & lblSale.Text)(0).Item("ConsumerName").ToString <> DBNull.Value.ToString Then
                    DirectCast(e.AccordionItem.FindControl("lblNameData"), Label).Text = DirectCast(Session(ContractHeaderSessionName), DataTable).Select("SaleNum = " & lblSale.Text)(0).Item("ConsumerName")
                End If
            End If
        ElseIf e.ItemType = AccordionItemType.Content Then
            dtcontractlines = Session(ContractLineSessionName)
            drvContract = e.Item
            rptContractDetails = e.AccordionItem.FindControl("rptContractDetails")

            If drvContract.Item("ContractType") = "PO" Then
                iContractNumber = drvContract.Item("PurchNum")
                strFilter = "PurchNum = " & iContractNumber
            Else
                iContractNumber = drvContract.Item("SaleNum")
                strFilter = "SaleNum = " & iContractNumber
            End If

            If Not rptContractDetails Is Nothing And iContractNumber > 0 And Not dtcontractlines Is Nothing Then
                dvContractDetails = dtcontractlines.DefaultView
                dvContractDetails.RowFilter = strFilter
                rptContractDetails.DataSource = dvContractDetails
                rptContractDetails.DataBind()
            End If
        End If
    End Sub
 
Last edited by a moderator:
This is your second post that is nothing but unformatted code and a vague title. You need to do better. If you think that your code takes longer to execute than it should then use the tools that VS provides to analyse it. At the very least, you can provide us with far more relevant information. You can use the Stopwatch class to time sections of your code and work out exactly how long it takes to execute and where the time may be being spent. If you need to create a new test project and write code from scratch, building it up bit by bit until it starts to appear slow then that's what you should do. You need to make the effort to everything you can first and then come here with all the relevant information, provided to us in a clear explanation of the problem. Doing nothing for yourself, dumping all your code here and expecting us to do all the work doesn't really cut it. We're here to help, but that still requires you to do your part.
 
Back
Top