Results 1 to 6 of 6

Thread: Please help with optimizing.

  1. #1

    Thread Starter
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464

    Please help with optimizing.

    Ok, this might be a simple question, but I have been taking a datarow that holds all the information for the page's controls and populating one control at a time. This is so inefficient, and I am wondering what you do.

    With a Win Form app, I set the controls tag properties to the column name that the control gets populated with. This allows me to run each control through a loop to find its own data and populate itself. Then I just do a for each loop looping the controls through the procedure to match up the data. Obviously Web Apps don't have the tag, and the control collections seem to also be a little more complex than what I have encountered on a windows app.

    Any suggestions?

    Here is some sample code I use on each page to fill the controls:
    Code:
    Private Sub BindPoleDetails()
            Dim dtPoleData As New DataTable
            Dim boPole As New BOPole
            Dim intID As Integer
    
            Try
                'Check for a querystring ID
                intID = CType(Request.QueryString.Item("PoleID"), Integer)
                dtPoleData = boPole.GetPole(intID)
    
                'TODO:Extend the textbox to add tags.
    
                If Not dtPoleData Is Nothing Then
                    If Not dtPoleData.Rows(0)("WO #") Is DBNull.Value Then
                        txtWorkOrderNum.Text = dtPoleData.Rows(0)("WO #")
                    End If
                    If Not dtPoleData.Rows(0)("Class") Is DBNull.Value Then
                        txtClass.Text = dtPoleData.Rows(0)("Class")
                    End If
                    If Not dtPoleData.Rows(0)("TRS") Is DBNull.Value Then
                        txtTownship.Text = dtPoleData.Rows(0)("TRS")
                    End If
                    If Not dtPoleData.Rows(0)("Service Address") Is DBNull.Value Then
                        txtServiceAddress.Text = dtPoleData.Rows(0)("Service Address")
                    End If
                    If Not dtPoleData.Rows(0)("Comments") Is DBNull.Value Then
                        txtComments.Text = dtPoleData.Rows(0)("Comments")
                    End If
                    If Not dtPoleData.Rows(0)("Install Date") Is DBNull.Value Then
                        txtInstallDate.Text = dtPoleData.Rows(0)("Install Date")
                    End If
                    If Not dtPoleData.Rows(0)("Mfr date") Is DBNull.Value Then
                        txtManufactureDate.Text = dtPoleData.Rows(0)("Mfr date")
                    End If
                    If Not dtPoleData.Rows(0)("Size") Is DBNull.Value Then
                        ddlSize.SelectedValue = dtPoleData.Rows(0)("Size")
                    End If
                    If Not dtPoleData.Rows(0)("Owner") Is DBNull.Value Then
                        ddlOwner.SelectedValue = dtPoleData.Rows(0)("Owner")
                    End If
                    If Not dtPoleData.Rows(0)("Mfr") Is DBNull.Value Then
                        ddlManufacturer.SelectedValue = dtPoleData.Rows(0)("Mfr")
                    End If
                    If Not dtPoleData.Rows(0)("Location") Is DBNull.Value Then
                        ddlCounty.SelectedValue = dtPoleData.Rows(0)("Location")
                    End If
                    If Not dtPoleData.Rows(0)("Type") Is DBNull.Value Then
                        ddlOwnerType.SelectedValue = dtPoleData.Rows(0)("Type")
                    End If
                    If Not dtPoleData.Rows(0)("InActive") Is DBNull.Value Then
                        cbInActive.Checked = dtPoleData.Rows(0)("InActive")
                    End If
                    If Not dtPoleData.Rows(0)("Rotten") Is DBNull.Value Then
                        chbRotten.Checked = dtPoleData.Rows(0)("Rotten")
                    End If
                    If Not dtPoleData.Rows(0)("RetireWO") Is DBNull.Value Then
                        txtRetireWONum.Text = dtPoleData.Rows(0)("RetireWO")
                    End If
                    If Not dtPoleData.Rows(0)("RetireDate") Is DBNull.Value Then
                        txtRetireDate.Text = dtPoleData.Rows(0)("RetireDate")
                    End If
                    If Not dtPoleData.Rows(0)("SequenceNumber") Is DBNull.Value Then
                        txtSequenceNumber.Text = dtPoleData.Rows(0)("SequenceNumber")
                    End If
                End If
            Catch ex As Exception
                'No PoleID in the query string.  Send user to the home page.
                Response.Redirect("default.aspx")
            End Try
        End Sub

  2. #2

    Thread Starter
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    Nevermind, I reduced it to something like this:
    Code:
    Public dtPoleData As New DataTable
    
    Private Sub BindPoleDetails()
            Dim boPole As New BOPole
            Dim intID As Integer
    
            Try
                'Check for a querystring ID
                intID = CType(Request.QueryString.Item("PoleID"), Integer)
                dtPoleData = boPole.GetPole(intID)
    
                If Not dtPoleData Is Nothing Then
                    Page.DataBind()
                End If
            Catch ex As Exception
                'No PoleID in the query string.  Send user to the home page.
                Response.Redirect("default.aspx")
            End Try
        End Sub
    I did this by setting the text properties and such to things like so in the actual aspx page:
    Code:
    <asp:textbox id="txtWorkOrderNum" runat="server" Width="100%" Text='<%# dtPoleData.Rows(0)("WONum") %>'></asp:textbox>
    Seems to work well, I don't know if it is optimized, but there is less back end code to write, and a any web designer could change the data bindings at any time which allows more flexibility if the underlying data structure changes.

    If anyone has any suggestions for me, please do tell.

  3. #3
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    Well I'm slightly new to ASP.NET, and I have a different design issues than you.

    But:

    We simply use a datareader, and simply connect to the database on the page creation, and populate the controls that way.
    VB Code:
    1. if not dr(0) is DbNull.Value then myText = ctype(dr(0),string)

    Ordinal lookups are roughly 15x faster.. and the datareader is quite faster than creating a datatable to hold the data.

    Now, like I said, I don't know your design goals, so all of that may make little sense to you if you are caching your pages.

  4. #4
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fort Collins, CO
    Posts
    366
    Ordinal lookups are roughly 15x faster
    Where'd you get that from? I'm not disagreeing, I just want to see the code/article/msdn doc that shows that difference.

  5. #5

    Thread Starter
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    Originally posted by nemaroller
    Well I'm slightly new to ASP.NET, and I have a different design issues than you.

    But:

    We simply use a datareader, and simply connect to the database on the page creation, and populate the controls that way.
    VB Code:
    1. if not dr(0) is DbNull.Value then myText = ctype(dr(0),string)

    Ordinal lookups are roughly 15x faster.. and the datareader is quite faster than creating a datatable to hold the data.

    Now, like I said, I don't know your design goals, so all of that may make little sense to you if you are caching your pages.
    Agreed. The code above is more of a starting point. I haven't even tried to optimize it for the web. Normally I use datareaders for everything. They just make sense because they are super fast.

    For web work, ALWAYS try to use a datareader than a datatable/dataset if you can.

  6. #6
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    Originally posted by pvb
    Where'd you get that from? I'm not disagreeing, I just want to see the code/article/msdn doc that shows that difference.
    Just from experience...

    On a high level, its like comparing the performance of a pointer

    vs

    the performance of a function that takes a string, and transverses across all datacolumns to find a match. And the code that runs to determine if one string = another string.
    VB Code:
    1. x = 2
    vs
    VB Code:
    1. 'also realize the more columns you have, the longer this takes.
    2. 'and the longer the names of your columns, the longer this takes
    3. 'and the more times you use mytable.columns("") to set all the
    4. 'data in your table, the time to complete keeps multiplying.
    5. Protected Function findOrdinalfromString(targetString) As Integer
    6. Dim match As Boolean = False
    7. For j = 0 To me.datacolumns.count-1
    8. For i = 0 To me.datacolumn(j).ColumnName.Length
    9.   If Not somedatacolumnheaderstring.char[i]  is targetstring.char[i]
    10.      Exit For
    11.   End If
    12.   If i = me.datacolumn(j).ColumnName.Length - 1 Then match = True
    13. Next
    14. If match return j
    15. Next
    16. Throw 'error
    17. End Function
    Last edited by nemaroller; Dec 14th, 2003 at 01:28 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width