ASP - What happens if an Array has gaps in it?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • DesignsOnline
    New Member
    • Apr 2013
    • 10

    ASP - What happens if an Array has gaps in it?

    Im trying to create an array by dynamically selecting values from several different fields in a table, but not all the fields will always have data in.

    So what I want to know is what would happen in the following situation if several of the fields at random had no values?

    Code:
    <%
    Select Pic1, Pic2, Pic3, Pic4, Pic5, Pic6, Pic7, Pic8, Pic9, Pic10 From Gallery Where GalleryID = 123
    %>
     
     
    <%
    Dim GalleryArray() 'Dynamic array
    GalleryArray(0) = "rsGallery.Item("Pic1").value"
    GalleryArray(1) = "rsGallery.Item("Pic2").value"
    GalleryArray(2) = "rsGallery.Item("Pic3").value"
    GalleryArray(3) = ""
    GalleryArray(4) = ""
    GalleryArray(5) = "rsGallery.Item("Pic6").value"
    GalleryArray(6) = "rsGallery.Item("Pic7").value"
    GalleryArray(7) = "rsGallery.Item("Pic8").value"
    GalleryArray(8) = ""
    GalleryArray(9) = "rsGallery.Item("Pic10").value"
    For Each item In GalleryArray
    %>
    <img src="<% Response.Write(item) %><br>
    Next
    %>
    In the example above ive left 3 of the aray values blank to simulate blank fields in the database.

    What I need to know is would the array:
    1) Break
    2) skip the blank values and move straight to the next value in the list?
    like this:
    <img src="Pic1.jpg"> <br>
    <img src="Pic2.jpg"> <br>
    <img src="Pic3.jpg"> <br>
    <img src="Pic6.jpg"> <br>
    <img src="Pic7.jpg"> <br>
    <img src="Pic8.jpg"> <br>
    <img src="Pic10.jpg" ><br>

    Or would it
    3)produce blank outputs to match the blank fields like this:
    <img src="Pic1.jpg"> <br>
    <img src="Pic2.jpg"> <br>
    <img src="Pic3.jpg"> <br>
    <img src=""><br>
    <img src=""><br>
    <img src="Pic6.jpg"> <br>
    <img src="Pic7.jpg"> <br>
    <img src="Pic8.jpg"> <br>
    <img src=""><br>
    <img src="Pic10.jpg" ><br>



    And is there a way of counting the number of actual values in a dynamic array?
  • DesignsOnline
    New Member
    • Apr 2013
    • 10

    #2
    What im trying to accomplish is number 2 in the possible outcomes above.

    Any ideas how I can do this if the way above is wrong?

    Comment

    • Rabbit
      Recognized Expert MVP
      • Jan 2007
      • 12517

      #3
      First, I have to assume that is not the code you're using since there's syntax errors everywhere. And second, your database is unnormalized and you should think about normalizing it so querying it is easier.

      It may result in scenario 1 if the values are null and not blank.

      It will result in scenario 3 otherwise.

      To get scenario 2, you would need to use an If statement to check whether or not the field contains a value before printing it out.

      Comment

      • DesignsOnline
        New Member
        • Apr 2013
        • 10

        #4
        Here is the actual code im using, at the moment im getting an error:

        Microsoft OLE DB Provider for ODBC Drivers error '80040e14'

        [Microsoft][ODBC Microsoft Access Driver] Syntax error (missing operator) in query expression 'ProductID ='.

        /NewSite/productdetails. asp, line 512


        That line refers to line 21 below: "rsGallery.Open ()" line ive done a response.write and I know that the ProductId value is there as it is being written correctly by that response.write, so it seems the error is here somehow:
        Code:
        WHERE ProductID = " + Replace(rsUpdate__MMColParam, "'", "''") + ""
        But here is the code anyway:
        Code:
        <%
        Dim rsGallery__MMColParam
        rsGallery__MMColParam = "0"
        If (Request.QueryString("ProductID") <> "") Then 
        
          rsGallery__MMColParam = Request.QueryString("ProductID")
        response.write rsGallery__MMColParam
        End If
        %>
        
        <%
        Dim rsGallery
        Dim rsGallery_numRows
        
        Set rsGallery = Server.CreateObject("ADODB.Recordset")
        rsGallery.ActiveConnection = MM_connTS_STRING
        rsGallery.Source = "SELECT BigPict, Pic2, Pic3, Pic4, Pic5, Pic6, Pic7, Pic8, Pic9, Pic10, Pic11, Pic12, Pic13, Pic14, Pic15, Pic16, Pic17, Pic18, Pic19, Pic20, Pic21, Pic22, Pic23, Pic24, Pic25, Pic26, Pic27, Pic28, Pic29, Pic30, Pic31, Pic32, Pic33, Pic34, Pic35, Pic36, Pic37, Pic38, Pic39, Pic40  FROM Products  WHERE ProductID = " + Replace(rsUpdate__MMColParam, "'", "''") + ""
        rsGallery.CursorType = 0
        rsGallery.CursorLocation = 2
        rsGallery.LockType = 1
        rsGallery.Open()
        
        rsGallery_numRows = 0
        
        
        Dim GalleryArray() 
        GalleryArray(0) = rsGallery.Fields.Item("BigPict").Value
        GalleryArray(1) = rsGallery.Fields.Item("Pic2").Value
        GalleryArray(2) = rsGallery.Fields.Item("Pic3").Value
        GalleryArray(3) = rsGallery.Fields.Item("Pic4").Value
        GalleryArray(4) = rsGallery.Fields.Item("Pic5").Value
        GalleryArray(5) = rsGallery.Fields.Item("Pic6").Value
        GalleryArray(6) = rsGallery.Fields.Item("Pic7").Value
        GalleryArray(7) = rsGallery.Fields.Item("Pic8").Value
        GalleryArray(8) = rsGallery.Fields.Item("Pic9").Value
        GalleryArray(9) = rsGallery.Fields.Item("Pic10").Value
        GalleryArray(10) = rsGallery.Fields.Item("Pic11").value
        GalleryArray(11) = rsGallery.Fields.Item("Pic12").value
        GalleryArray(12) = rsGallery.Fields.Item("Pic13").value
        GalleryArray(13) = rsGallery.Fields.Item("Pic14").value
        GalleryArray(14) = rsGallery.Fields.Item("Pic15").value
        GalleryArray(15) = rsGallery.Fields.Item("Pic16").value
        GalleryArray(16) = rsGallery.Fields.Item("Pic17").value
        GalleryArray(17) = rsGallery.Fields.Item("Pic18").value
        GalleryArray(18) = rsGallery.Fields.Item("Pic19").value
        GalleryArray(19) = rsGallery.Fields.Item("Pic20").value
        GalleryArray(20) = rsGallery.Fields.Item("Pic21").value
        GalleryArray(21) = rsGallery.Fields.Item("Pic22").value
        GalleryArray(22) = rsGallery.Fields.Item("Pic23").value
        GalleryArray(23) = rsGallery.Fields.Item("Pic24").value
        GalleryArray(24) = rsGallery.Fields.Item("Pic25").value
        GalleryArray(25) = rsGallery.Fields.Item("Pic26").value
        GalleryArray(26) = rsGallery.Fields.Item("Pic27").value
        GalleryArray(27) = rsGallery.Fields.Item("Pic28").value
        GalleryArray(28) = rsGallery.Fields.Item("Pic29").value
        GalleryArray(29) = rsGallery.Fields.Item("Pic30").value
        GalleryArray(30) = rsGallery.Fields.Item("Pic31").value
        GalleryArray(31) = rsGallery.Fields.Item("Pic32").value
        GalleryArray(32) = rsGallery.Fields.Item("Pic33").value
        GalleryArray(33) = rsGallery.Fields.Item("Pic34").value
        GalleryArray(34) = rsGallery.Fields.Item("Pic35").value
        GalleryArray(35) = rsGallery.Fields.Item("Pic36").value
        GalleryArray(36) = rsGallery.Fields.Item("Pic37").value
        GalleryArray(37) = rsGallery.Fields.Item("Pic38").value
        GalleryArray(38) = rsGallery.Fields.Item("Pic39").value
        GalleryArray(39) = rsGallery.Fields.Item("Pic40").value
        For Each item In GalleryArray
        
        %>
        
         <img src="http://bytes.com/images/<% Response.Write(item) %><br>
        <%
        Next
        %>
        Last edited by DesignsOnline; Apr 17 '13, 06:13 PM. Reason: typo

        Comment

        • DesignsOnline
          New Member
          • Apr 2013
          • 10

          #5
          I still need to resolve the error above, but to get arount it for the moment I manually added a valid productid to the SQL query and got the following error:

          Microsoft VBScript runtime error '800a0009'

          Subscript out of range


          The line it is referring to with this error is line 27 from the code in the post above :
          Code:
          GalleryArray(0) = rsGallery.Fields.Item("BigPict").Value
          A google search said that usually means you tried to access an element of an array that was either greater than its ubound or lower than its lbound.

          Or it means that the value you are trying load isnt there, but I know that the value from the field "BigPict" is there because I have manually checked the database, and im also loading it on another page...

          Im not using ubound or lbound in the code above, do I need to be, and if so, any help on how I would use them with my code above?

          Comment

          • Rabbit
            Recognized Expert MVP
            • Jan 2007
            • 12517

            #6
            If you are absolutely sure that there is a BigPict field in the table, then the next likely cause is that your query returned no rows because of a malformed SQL string.

            Please post what your SQL string looks like just before it is sent to the database. We also need to see the table metadata.

            Comment

            • DesignsOnline
              New Member
              • Apr 2013
              • 10

              #7
              Hi there, im sorry but I dont understatnd the question.
              (Im not a programmer, just a learner)

              The code shown in the posts above is the actual exact code from the page. I know that there is a value in the database for BigPict because on a number of other pages I am loading the image that is stored in it.

              If it helps the table is in an access database...


              I don't have to use this method if there is an easier way to acheive option 2 from my original post?

              Comment

              • Rabbit
                Recognized Expert MVP
                • Jan 2007
                • 12517

                #8
                I don't have to use this method if there is an easier way to acheive option 2 from my original post?
                Easier no. Better yes. The better, more correct way is to normalize the database which I mentioned in post #3. It requires more work though.

                The code shown in the posts above is the actual exact code from the page.
                Code is one thing. What the database gets is another. I need to see what the database is actually getting. I also need to know the table's meta data.

                This is code:
                Code:
                x = "abc"
                sql = "select * from table where id = " + x
                This is what the database sees:
                Code:
                select * from table where id = abc
                If that is what the database sees, it will result in an error, or it will not return the correct data because the data being passed does not match the metadata.

                That is why I can't use just the code alone to diagnose the issue. I need the code, what the database sees, and the metadata. Each is different but related. And I need each to track down the problem. Currently I only have the code. I am missing two pieces of important information for a complete picture.

                Comment

                • DesignsOnline
                  New Member
                  • Apr 2013
                  • 10

                  #9
                  Ive solved it, in case anyone is having the same problem what fixed it for me was I needed to specify to maximum possible number of records like this:

                  Dim GalleryArray(39 )

                  Previously I was using this:
                  Dim GalleryArray()

                  Comment

                  • DesignsOnline
                    New Member
                    • Apr 2013
                    • 10

                    #10
                    Here is the full code in case anyone needs it:

                    Code:
                    <%
                    Dim rsGallery__MMColParam
                    rsGallery__MMColParam = "0"
                    If (Request.QueryString("ProductID") <> "") Then 
                    
                      rsGallery__MMColParam = Request.QueryString("ProductID")
                    
                    End If
                    %>
                    
                    <%
                    Dim rsGallery
                    Dim rsGallery_numRows
                    
                    Set rsGallery = Server.CreateObject("ADODB.Recordset")
                    rsGallery.ActiveConnection = MM_connTS_STRING
                    rsGallery.Source = "SELECT BigPict, Pic2, Pic3, Pic4, Pic5, Pic6, Pic7, Pic8, Pic9, Pic10, Pic11, Pic12, Pic13, Pic14, Pic15, Pic16, Pic17, Pic18, Pic19, Pic20, Pic21, Pic22, Pic23, Pic24, Pic25, Pic26, Pic27, Pic28, Pic29, Pic30, Pic31, Pic32, Pic33, Pic34, Pic35, Pic36, Pic37, Pic38, Pic39, Pic40  FROM Products  WHERE ProductID = " & Replace(rsGallery__MMColParam, "'", "''") 
                    rsGallery.CursorType = 0
                    rsGallery.CursorLocation = 2
                    rsGallery.LockType = 1
                    rsGallery.Open()
                    %>
                    
                    
                    
                    <%
                    
                    Dim GalleryArray(39) 
                    GalleryArray(0) = rsGallery.Fields.Item("BigPict").Value
                    GalleryArray(1) = rsGallery.Fields.Item("Pic2").Value
                    GalleryArray(2) = rsGallery.Fields.Item("Pic3").Value
                    GalleryArray(3) = rsGallery.Fields.Item("Pic4").Value
                    GalleryArray(4) = rsGallery.Fields.Item("Pic5").Value
                    GalleryArray(5) = rsGallery.Fields.Item("Pic6").Value
                    GalleryArray(6) = rsGallery.Fields.Item("Pic7").Value
                    GalleryArray(7) = rsGallery.Fields.Item("Pic8").Value
                    GalleryArray(8) = rsGallery.Fields.Item("Pic9").Value
                    GalleryArray(9) = rsGallery.Fields.Item("Pic10").Value
                    GalleryArray(10) = rsGallery.Fields.Item("Pic11").value
                    GalleryArray(11) = rsGallery.Fields.Item("Pic12").value
                    GalleryArray(12) = rsGallery.Fields.Item("Pic13").value
                    GalleryArray(13) = rsGallery.Fields.Item("Pic14").value
                    GalleryArray(14) = rsGallery.Fields.Item("Pic15").value
                    GalleryArray(15) = rsGallery.Fields.Item("Pic16").value
                    GalleryArray(16) = rsGallery.Fields.Item("Pic17").value
                    GalleryArray(17) = rsGallery.Fields.Item("Pic18").value
                    GalleryArray(18) = rsGallery.Fields.Item("Pic19").value
                    GalleryArray(19) = rsGallery.Fields.Item("Pic20").value
                    GalleryArray(20) = rsGallery.Fields.Item("Pic21").value
                    GalleryArray(21) = rsGallery.Fields.Item("Pic22").value
                    GalleryArray(22) = rsGallery.Fields.Item("Pic23").value
                    GalleryArray(23) = rsGallery.Fields.Item("Pic24").value
                    GalleryArray(24) = rsGallery.Fields.Item("Pic25").value
                    GalleryArray(25) = rsGallery.Fields.Item("Pic26").value
                    GalleryArray(26) = rsGallery.Fields.Item("Pic27").value
                    GalleryArray(27) = rsGallery.Fields.Item("Pic28").value
                    GalleryArray(28) = rsGallery.Fields.Item("Pic29").value
                    GalleryArray(29) = rsGallery.Fields.Item("Pic30").value
                    GalleryArray(30) = rsGallery.Fields.Item("Pic31").value
                    GalleryArray(31) = rsGallery.Fields.Item("Pic32").value
                    GalleryArray(32) = rsGallery.Fields.Item("Pic33").value
                    GalleryArray(33) = rsGallery.Fields.Item("Pic34").value
                    GalleryArray(34) = rsGallery.Fields.Item("Pic35").value
                    GalleryArray(35) = rsGallery.Fields.Item("Pic36").value
                    GalleryArray(36) = rsGallery.Fields.Item("Pic37").value
                    GalleryArray(37) = rsGallery.Fields.Item("Pic38").value
                    GalleryArray(38) = rsGallery.Fields.Item("Pic39").value
                    GalleryArray(39) = rsGallery.Fields.Item("Pic40").value
                    For Each item In GalleryArray
                    
                    
                    %>
                    <% If item >"" Then %>
                     <img src="images/<% Response.Write(item) %>" /><br>
                     <%End If%>
                    <%
                    Next
                    %>

                    Comment

                    Working...