0

I am having a really strange problem. I am creating a webpage that retrieves a list of links from a database and displays them. I retrieve the records and load them into an object list. However when I iterate through that list later I am getting duplicated items displayed.

The page to view the result is http://myhacc.azurewebsites.net/testlinks.aspx

There should be only 1 of each link and as you can see there are 2 of each. I havent the slightet as to why. I have been over my logic 10 times.

I am using a Microsoft SQL Server running on microsoft azure The Web Page is also hosted on microsoft azure as a web app.

I have checked my database and there are no duplicate entries in the database. the structure of the database is as follows

Table: myportal_lcat Field: id - integer Field: name - text

Table: myportal_lhref Field: catid - integer Field: dname - text Field: href - text

the code is as follows

<%@ Page Title="" Language="VB" MasterPageFile="~/Main.master" %>

<script runat="server">
    Private oCon As Data.SqlClient.SqlConnection
    Private oCon2 As Data.SqlClient.SqlConnection
    Private oCatCom As New Data.SqlClient.SqlCommand
    Private oCatRead As Data.SqlClient.SqlDataReader
    Private oItemCom As New Data.SqlClient.SqlCommand
    Private oItemRead As Data.SqlClient.SqlDataReader
    Private liLinks As New List(Of linkItem)

    Private Sub Page_Load() Handles Me.Load
        oCon = New Data.SqlClient.SqlConnection("Server=xxxx,1433;Database=xxxxx;User ID=xxxxxx;Password=xxxxx;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;")
        oCon2 = New Data.SqlClient.SqlConnection("Server=xxxxxx,1433;Database=xxxx;User ID=xxxx;Password=xxxxx;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;")
        oCon.Open()
        oCon2.Open()
        oCatCom.Connection = oCon
        oCatCom.CommandText = "Select * From myportal_lcat"
        oItemCom.Connection = oCon2
        oItemCom.CommandText = "Select * From myportal_lhref"
        oCatRead = oCatCom.ExecuteReader()
        oItemRead = oItemCom.ExecuteReader()
        Do While oItemRead.Read
            liLinks.Add(New linkItem With {.CatID = oItemRead("catid"), .DName = oItemRead("dname"), .HREF = oItemRead("href")})
        Loop
    End Sub

    Private Class linkItem
        Private m_CatID As Integer
        Private m_DName As String
        Private m_HREF As String
        Public Property CatID() As Integer
            Get
                CatID = m_CatID
            End Get
            Set(value As Integer)
                m_CatID = value
            End Set
        End Property
        Public Property DName() As String
            Get
                DName = m_DName
            End Get
            Set(value As String)
                m_DName = value
            End Set
        End Property
        Public Property HREF() As String
            Get
                HREF = m_HREF
            End Get
            Set(value As String)
                m_HREF = value
            End Set
        End Property
    End Class
</script>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
    My Portal - Test
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="PageHeader" Runat="Server">
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="PageContent" Runat="Server">
    <%Dim t As linkItem %>
    <%Do While oCatRead.Read()%>
        <section class="links">
            <h2><%=oCatRead("name") %></h2>
            <ul>
                <%For each t In liLinks %>
                    <%If t.CatID = oCatRead("id") Then %>
                        <li><a href="<%=t.HREF %>"><%=t.DName %></a></li>
                    <%End If %>
                <%Next %>
            </ul>
        </section>
    <%Loop%>
</asp:Content>
Velocibadgery
  • 3,670
  • 2
  • 12
  • 17
  • 1
    Is your page load running multiple times? You are going to have to debug this from your end. I don't see anything that would normally cause it load twice. – Sean Lange Mar 03 '16 at 20:38
  • It seems a bit wasteful to use 2 qeuries to get the data. It seems from your code your joining the 2 tables on an id column. So why not write it as one statement and safe yourself the hassle of having to join the tables with complicated loops... – Jeroen Mar 03 '16 at 20:48
  • Sean Lange - That was the problem the page load was running twice. I have no idea why it should run twice but it was. I moved the initialization of the list from the declaration to just above the do loop and that fixed the issue. – Velocibadgery Mar 03 '16 at 21:14
  • Jeroen. I wouldn't quite be sure how to display the information if it was all in one recordset. So i guess the simple answer is that i dont know how. However as this is just a personal page and nothing high traffic the resource cost of the extra query will be very low so the waste is acceptable. – Velocibadgery Mar 03 '16 at 21:16

1 Answers1

0

The problem is caused by the fact that when using asp.net masterpages the page_load gets called first in the masterpage and then again in the actual page. However the code from both runs twice.

As i was initializing the object list in the declaration when I added items to the list in the page load it added them twice as page_load was running twice. I moved the initialization of the object list into the page_load instead of declaration and this solved my issue.

Reference: Page_Load is firing twice in ASP.NET page

Community
  • 1
  • 1
Velocibadgery
  • 3,670
  • 2
  • 12
  • 17