Mutli dimensional Array in ASP

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sreemati
    New Member
    • Jun 2007
    • 42

    Mutli dimensional Array in ASP

    Hi Everyone,

    I have a store procedure returning 7 rows for 3 columns, basically I am retrieving:
    Descriptor Lower Range Upper Range
    D1 1 10
    D2 2 11
    D3 3 12
    D4 4 13
    D5 5 14
    D6 6 15
    D7 7 16

    Now on my ASP page there are Text values that can be entered for Descriptors D1 - D7 and I need to validate the form to ensure that all values for each descriptor mentioned are within the lower and upper range for that descriptor in the look up table.

    For example: If I have a user entering value of D4 from the form, than I have to find out if it is between 4-13, if not send them a pop up to say, please enter within that range.

    Solution I thought:
    Get the record set into array in the ASP page and than for each descriptor validate the form value entered is within the range.

    Problem: Is that I can not get my ASP page to return value more than [3,2] :(

    My Code in the ASP page is:

    [ASP]
    <%

    Dim ValConn, ValCmd, ValRS, ValParam, ValArray, Region, temp
    Dim intLastCol, intLastRow, intRow, intCol
    temp = "T"


    SET ValConn = Server.CreateOb ject("ADODB.Con nection")
    SET ValCmd = Server.CreateOb ject("ADODB.Com mand")
    SET ValRS = Server.CreateOb ject("ADODB.Rec ordset")
    Set ValParam=Server .CreateObject(" ADODB.Parameter ")
    ValConn.Open Application("Co nnection")

    SET ValCmd.ActiveCo nnection = ValConn


    ValCmd.CommandT ype = 4
    ValCmd.CommandT ext = "sp_Validate_Ra nge_Values"
    Set ValParam = ValCmd.CreatePa rameter("Region ", 200,1,1,temp)
    ValCmd.Paramete rs.Append ValParam

    SET ValRS = ValCmd.Execute

    ValArray = ValRS.GetRows

    ValRS.Close
    SET ValRS= Nothing



    'intLastCol = UBound(ValArray , 1)
    'intLastRow = Ubound(ValArray , 2)


    'For intRow = 0 to intLastCol
    'For intCol = 0 to intLastRow
    'Response.Write ValArray(intCol , intRow) & " "
    'Next
    'Response.Write "<Br>"
    'Next

    Response.Write ValArray (0,0,)
    Response.Write "<Br>"

    Response.Write ValArray (0,1)
    Response.Write "<Br>"

    Response.Write ValArray (0,2)
    Response.Write "<Br>"

    Response.Write ValArray (1,0)
    Response.Write "<Br>"

    Response.Write ValArray (1,1)
    Response.Write "<Br>"

    Response.Write ValArray (1,2)
    Response.Write "<Br>"

    Response.Write ValArray (2,0)
    Response.Write "<Br>"

    Response.Write ValArray (2,1)
    Response.Write "<Br>"

    Response.Write ValArray (2,2)
    Response.Write "<Br>"

    Response.Write ValArray (3,0)
    Response.Write "<Br>"

    Response.Write ValArray (3,1)
    Response.Write "<Br>"

    Response.Write ValArray (3,2)
    Response.Write "<Br>"

    Response.Write ValArray (4,0)
    Response.Write "<Br>"

    Response.Write ValArray (4,1)
    Response.Write "<Br>"

    Response.Write ValArray (4,2)
    Response.Write "<Br>"

    Response.Write ValArray (5,0)
    Response.Write "<Br>"

    Response.Write ValArray (5,1)
    Response.Write "<Br>"

    Response.Write ValArray (5,2)
    Response.Write "<Br>"



    ValConn.Close
    SET ValConn = Nothing


    Response.Write( "Hello")

    %>
    [/ASP]

    ERROR MESSAGE ON ASP PAGE IS:

    D1
    D2
    D3
    1
    2
    3
    10
    11
    13

    Microsoft VBScript runtime error '800a0009'

    Subscript out of range: '[number: 3]'

    /scidc/test.asp, line 74

    I believe the issue is because ASP is making an 2D array whereas I need a 3D array and I tired finding how to get multi dimensional array in ASP but none of them were useful, would be glad if someone can explain what is wrong and what can be a possible solution to make the code work.

    Thanks in advance,
    Sree
  • Nicodemas
    Recognized Expert New Member
    • Nov 2007
    • 164

    #2
    It seems you could skip the need for a big array if your SQL query does the testing for you. I have written a sample piece of psuedocode.

    Code:
    TABLE: Limits
    
    Descriptor | Lower_Range | Upper_Range
    D1              1            10
    D2              2            11
    D3              3            12
    D4              4            13
    D5              5            14
    D6              6            15
    D7              7            16
    SQL:

    [code=vbscript]"SELECT descriptor FROM Limits WHERE Lower_Range >= "& userInput_Lower &" AND Upper_Range <= "& userInput_Upper t &" AND descriptor = '"& userInput_Descr iptor &"'"[/code]

    VBScript Logic:
    [code=vbscript]
    <%
    '// your code which executes the query...
    '// returns recordset ("rs")

    if rs.eof then
    system_message = "you must enter a value with the valid range for the "& userInput_Descr iptor &" field."
    '// do some error processing...
    else
    '// do regular processing
    end if
    %>
    [/code]
    Last edited by Nicodemas; Dec 7 '07, 10:35 PM. Reason: Didn't explain myself

    Comment

    • Nicodemas
      Recognized Expert New Member
      • Nov 2007
      • 164

      #3
      You could do it your way, though.

      Code:
      TABLE: Limits
      
      Descriptor | Lower_Range | Upper_Range
      D1              1            10
      D2              2            11
      D3              3            12
      D4              4            13
      D5              5            14
      D6              6            15
      D7              7            16
      SQL:

      [code=vbscript]SELECT * FROM Limits[/code]

      VBScript Logic:

      [code=vbscript]
      <%
      ' your code executes the query, and
      ' returns recordset "rs"


      aResults = rs.getRows
      set rs = nothing


      ' 2d array will look like this:
      '============== =============== ===============
      'row | descriptor (0) | lower (1) | upper (2)
      '============== =============== ===============
      ' 0 D1 1 10
      ' 1 D2 2 11
      ' 2 D3 3 12
      ' et cetera, just like your database table


      ' To ask, "Did the user enter a valid lower range value for D2?", you would say
      '
      ' if userInput_Lower >= aResults(1, 1) then
      ' ... yes, is valid, do stuff
      ' else
      ' ... no, is invalid, do stuff
      ' end if

      ' Remember: get rows will create enough dimensions to accommodate
      ' the result set. The first number of the array represents the "column",
      ' the second number, the "row."
      %>
      [/code]

      Comment

      • sreemati
        New Member
        • Jun 2007
        • 42

        #4
        Originally posted by Nicodemas
        You could do it your way, though.

        Code:
        TABLE: Limits
        
        Descriptor | Lower_Range | Upper_Range
        D1              1            10
        D2              2            11
        D3              3            12
        D4              4            13
        D5              5            14
        D6              6            15
        D7              7            16
        SQL:

        [code=vbscript]SELECT * FROM Limits[/code]

        VBScript Logic:

        [code=vbscript]
        <%
        ' your code executes the query, and
        ' returns recordset "rs"


        aResults = rs.getRows
        set rs = nothing


        ' 2d array will look like this:
        '============== =============== ===============
        'row | descriptor (0) | lower (1) | upper (2)
        '============== =============== ===============
        ' 0 D1 1 10
        ' 1 D2 2 11
        ' 2 D3 3 12
        ' et cetera, just like your database table


        ' To ask, "Did the user enter a valid lower range value for D2?", you would say
        '
        ' if userInput_Lower >= aResults(1, 1) then
        ' ... yes, is valid, do stuff
        ' else
        ' ... no, is invalid, do stuff
        ' end if

        ' Remember: get rows will create enough dimensions to accommodate
        ' the result set. The first number of the array represents the "column",
        ' the second number, the "row."
        %>
        [/code]
        Thanks a lot for you help. Much appreciate.

        Sree

        Comment

        Working...