?
Solved

Convert array to 2 dimentional

Posted on 2012-09-18
8
Medium Priority
?
745 Views
Last Modified: 2012-09-22
At the moment when button B3 is pressed Sub BindData3()
The array I have is incremented and CString being the connection is stored in the array.
Then finally, all contents of the array are displayed showing all database connections.
Now, I want to be able to do the following:
TableLabel is the corresponding table name for the selected database connection.
I want to be able to put CString in the first element & TableLabel in the second element of the array. I.E have a multi-dimentional array displaying all database connections along with each corresponding selected table name.
Below is the code I have so far:
Thanks in advance for any help given.
<%
Sub BindData3()
currentPage = Request.QueryString("PageID")
if currentPage = 0 then
Response.Write("<div style='position:absolute;width:837px;height:354px;left:0px;top:270px;font-size:xx-small;overflow:auto;border:1px solid #722F90;' id='DIV999'>")
Response.Write("header2:" & currentPage & "#BindData3():<br/>")
Dim strRowArray2(20, 1)
Dim strRow    
Dim strColumn
Dim RowCount
Dim ColumnCount
strColumn   = 1
CString     = Session("sessionCString")
TableLabel  = Session("sessionTableLabel")
RowCount    = Session("strRowCount")
'****************************************
%>
<%
if RowCount = "" then
RowCount = 0
End if
Dim myArray()
ReDim Preserve myArray(RowCount) 'Re-Declaring a dynamic array
Response.Write("322:h2:BD3:RowCount:" & RowCount & "#    CString:" & CString & "#")
if CString = "Provider=;Data Source=;User Id=;Password=;Initial Catalog=;" then

Else
if RowCount = 0 then
myArray(RowCount) = CString
for i=0 to uBound(myArray)
Response.Write "<br>(" & i & ")" & myArray(i) & "#"
Next
Session("sessionmyArray") = myArray
response.write "<br/>array assigned to session var..."
Dim strRowArray
    strRowArray = Session("sessionmyArray") ' strRowArray(i, j)
    Dim oCounter : oCounter = 0
If IsArray(strRowArray) Then
      Dim strCountRow, strCountCol
      strCountRow  = UBound(strRowArray, 1)
      'strCountCol = UBound(strRowArray, 2) ' or the other way around...
      response.write ",  array dimensions: " & strCountRow & " x " & strCountCol
Else
      response.write "<br/>no array"
End If
else
RowCount    = Session("strRowCount")
Dim myArrayTEMP
myArrayTEMP     = Session("sessionmyArray")
ReDim Preserve myArrayTEMP(RowCount) 'Re-Declaring a dynamic array
myArrayTEMP(RowCount) = CString
for i=0 to uBound(myArrayTEMP)
Response.Write "<br>(" & i & ")" & myArrayTEMP(i) & "#"
Next
Session("sessionmyArray") = myArrayTEMP
response.write "<br/>array assigned to session var..."
strRowArray = Session("sessionmyArray") ' strRowArray(i, j)
oCounter = 0
If IsArray(strRowArray) Then
      strCountRow = UBound(strRowArray, 1)
      'strCountCol = UBound(strRowArray, 2) ' or the other way around...
      response.write ",  array dimensions: " & strCountRow & " x " & strCountCol
Else
      response.write "<br/>no array"
End If
End If
End If
%>
<%
'****************************************
RowCount = RowCount + 1
Session("strRowCount") = RowCount
Response.Write("</div>")
end if  
End Sub
%>
<%
Select Case Request.Form("strButtonAction")
     Case "B1"
     BindData1()
     Case "B2"
     BindData2()
     Case "B3"
     BindData3()
     Case "B4"
     BindData4()
     Case "B5"
     BindData5()
     Case tempDefault
     'txtProvider = Session("xtProvider")= "<font color='red'>NOT LOGGED IN!!</font>"
End Select
%>
0
Comment
Question by:homeshopper
  • 4
  • 2
6 Comments
 

Author Comment

by:homeshopper
ID: 38413084
With the following code I have managed to create the multi-dimentional array.
The problem now is, all previous record elements of the array are cleared.
Thanks in advance for any help given.
(0)# Tab:
(1)# Tab:
(2)# Tab:
(3)# Tab:
(4)# Tab:
(5)# Tab:
(6)Provider=sqloledb;Data Source=Dinosaur1\sqlexpress;User Id=sa;Password=H6web97!;Initial Catalog=AdminDB2;# Tab:DbProvider
array assigned to session var..., array dimensions: 6 x 2

<%
Sub BindData3()
currentPage = Request.QueryString("PageID")
if currentPage = 0 then
Response.Write("<div style='position:absolute;width:837px;height:354px;left:0px;top:270px;font-size:xx-small;overflow:auto;border:1px solid #722F90;' id='DIV999'>")
Response.Write("header2:" & currentPage & "#BindData3():<br/>")
Dim strRow    
Dim strColumn
Dim RowCount
Dim ColumnCount
strColumn   = 1
CString     = Session("sessionCString")
TableLabel  = Session("sessionTableLabel")
RowCount    = Session("strRowCount")
'****************************************
%>
<%
if RowCount = "" then
RowCount = 0
End if
Dim myArray()
ReDim Preserve myArray(RowCount,2) 'Re-Declaring a dynamic array
Response.Write("321:h2:BD3:RowCount:" & RowCount & "#    CString:" & CString & "#    TableLabel:" & TableLabel & "#")
if CString = "Provider=;Data Source=;User Id=;Password=;Initial Catalog=;" then

Else
if RowCount = 0 then
myArray(RowCount,1) = CString
myArray(RowCount,2) = TableLabel
for i=0 to uBound(myArray)
Response.Write "<br>(" & i & ")" & myArray(i,1) & "#    Tab:" & myArray(i,2)
Next
Session("sessionmyArray") = myArray
response.write "<br/>array assigned to session var..."
Dim strRowArray()
    ReDim Preserve strRowArray(RowCount,2) 'Re-Declaring a dynamic array
    strRowArray(RowCount,2) = Session("sessionmyArray") ' strRowArray(i, j)
    Dim oCounter : oCounter = 0
If IsArray(strRowArray) Then
      Dim strCountRow, strCountCol
      strCountRow  = UBound(strRowArray, 1)
      strCountCol = UBound(strRowArray, 2) ' or the other way around...
      response.write ",  array dimensions: " & strCountRow & " x " & strCountCol
Else
      response.write "<br/>no array"
End If 'IsArray
%>
<%
else
RowCount    = Session("strRowCount")
Dim myArrayTEMP()
ReDim Preserve myArrayTEMP(RowCount,2) 'Re-Declaring a dynamic array
myArrayTEMP(RowCount,2)   = Session("sessionmyArray")
myArrayTEMP(RowCount,1)   = CString
myArrayTEMP(RowCount,2)   = TableLabel
for i=0 to uBound(myArrayTEMP)
Response.Write "<br>(" & i & ")" & myArrayTEMP(i,1) & "#    Tab:" & myArrayTEMP(i,2)
Next
Session("sessionmyArray") = myArrayTEMP
response.write "<br/>array assigned to session var..."
oCounter = 0
Dim strRowArray2()
    ReDim Preserve strRowArray2(RowCount,2) 'Re-Declaring a dynamic array
    strRowArray2(RowCount,2) = Session("sessionmyArray") ' strRowArray(i, j)
If IsArray(strRowArray2) Then
      Dim strCountRow2, strCountCol2
      strCountRow2  = UBound(strRowArray2, 1)
      strCountCol2 = UBound(strRowArray2, 2) ' or the other way around...
      response.write ",  array dimensions: " & strCountRow2 & " x " & strCountCol2
Else
      response.write "<br/>no array"
End If 'IsArray
%>
<%
End If 'RowCount
End If 'CString
%>
<%
'****************************************
RowCount = RowCount + 1
Session("strRowCount") = RowCount
Response.Write("</div>")
end if  
End Sub
%>
0
 

Author Comment

by:homeshopper
ID: 38418347
New Code below: when array is incremented, it looses previously stored values.
<%
Sub BindData3()
currentPage = Request.QueryString("PageID")
if currentPage = 0 then
Response.Write("<div style='position:absolute;width:837px;height:334px;left:0px;top:290px;font-size:xx-small;overflow:auto;border:1px solid #722F90;' id='DIV999'>")
Response.Write("header2:" & currentPage & "#BindData3():<br/>")
Dim strRow    
Dim strColumn
Dim RowCount
Dim ColumnCount
Dim dCounter
Dim oCounter
Dim strCountRow
Dim strCountCol
strColumn   = 1
CString     = Session("sessionCString")
TableLabel  = Session("sessionTableLabel")
RowCount    = Session("strRowCount")
'****************************************
%>
<%
if RowCount = "" then
RowCount = 0
End If
%>
<%
Dim myArray()
ReDim Preserve myArray(RowCount,2) 'Re-Declaring a dynamic array
if CString = "Provider=;Data Source=;User Id=;Password=;Initial Catalog=;" then

Else
%>
<%

%>
<%
if RowCount = 0 then
%>
<%
Response.Write("338:h2:BD3:RowCount:" & RowCount & "#    CString:" & CString & "#    TableLabel:" & TableLabel & "#")
myArray(RowCount,1)   = CString
myArray(RowCount,2)   = TableLabel
for i=0 to uBound(myArray)
'Response.Write "<br>(342:" & i & ")" & myArrayTEMP(i,1) & "#    Tab:" & myArrayTEMP(i,2)
Next
response.write "<br/>344:array assigned to session var..."
strCountRow     = UBound(myArray, 1)
strCountCol     = UBound(myArray, 2) ' or the other way around...
response.write ",  array dimensions: " & strCountRow & " x " & strCountCol
Session("sessionmyArray") = myArray
%>
<%
Else
%>
<%
RowCount    = Session("strRowCount")
Response.Write("355:h2:BD3:RowCount:" & RowCount & "#    CString:" & CString & "#    TableLabel:" & TableLabel & "#")
Dim myArrayTEMP()
ReDim Preserve myArrayTEMP(RowCount,2) 'Re-Declaring a dynamic array
myArrayTEMP(RowCount,2)   = Session("sessionmyArray")
myArrayTEMP(RowCount,1)   = CString
myArrayTEMP(RowCount,2)   = TableLabel
for i=0 to uBound(myArrayTEMP)
'Response.Write "<br>(362:" & i & ")" & myArrayTEMP(i,1) & "#    Tab:" & myArrayTEMP(i,2)
Next
response.write "<br/>364:array assigned to session var..."
strCountRow     = UBound(myArrayTEMP, 1)
strCountCol     = UBound(myArrayTEMP, 2) ' or the other way around...
response.write ",  array dimensions: " & strCountRow & " x " & strCountCol
Session("sessionmyArray") = myArrayTEMP
%>
<%
End If 'RowCount
End If 'CString
%>
<%
Response.Write("<br/>")
oCounter = 0
dCounter = 0
            For i = 0 To strCountRow
                For j = 0 To strCountCol -2
                    'If (strRowArray2(i, j) = "" ) Then
                    'Response.Write("(" & i & "," & j & ")" & strRowArray2(i,j) & "#")
                    'Else
                        If oCounter = 7 Then
                            'Response.Write("<br/>")
                            oCounter = 0
                        End If
                        If dCounter = 0 Then
                            Response.Write("388:(" & chr(i + 65) & ")" & myArray(i,1) & "<a href='../scriptFrame/tempDefault" & (currentPage) & ".asp?db22=" & myArray(i,1) & "&dt22=" & myArray(i,2) & "&intDetail22=" & j & "&strD22=False" & "&PageID=" & (currentPage) & " ' title='" & myArray(i,1) & myArray(i,2) & "' runat='server'>" & "<font color='#0000FF' size='1'>" & "#    Tab:" & myArray(i,2) & "</font>" & "</a><br/>")
                        Else
                            Response.Write("375:(" & chr(i + 65) & ")" & myArrayTEMP(i,1) & "<a href='../scriptFrame/tempDefault" & (currentPage) & ".asp?db22=" & myArrayTEMP(i,1) & "&dt22=" & myArrayTEMP(i,2) & "&intDetail22=" & j & "&strD22=False" & "&PageID=" & (currentPage) & " ' title='" & myArrayTEMP(i,1) & myArrayTEMP(i,2) & "' runat='server'>" & "<font color='#0000FF' size='1'>" & "#    Tab:" & myArrayTEMP(i,2) & "</font>" & "</a><br/>")
                        End If
                        oCounter = oCounter + 1
                        dCounter = dCounter + 1
                    'End If
                Next 'j
            Next 'i
%>
<%
'****************************************
RowCount = RowCount + 1
Session("strRowCount") = RowCount
Response.Write("</div>")
End If  
End Sub
%>
0
 
LVL 28

Expert Comment

by:Ark
ID: 38420449
IMHO 2D array is incorrect approach - it's usefull if you need data whic depends on 2 parameters (like a Table), i.e SomeValue=Your2DArray(ConnID,LabelID). In your case I suggest using Dictionary object with keys collection=TableLabels and values=CStrings (or reverse if you need find TableLabel by CString). You can store dictionary object with session variable as well and use Add/Remove methods to manipulate dictionary entries.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:homeshopper
ID: 38421374
Thank you for your suggestion, I'll keep the information in mind for the future.
However, in this instance I prefer to stick with 2D array.
I have quite a lot of connections strings CString held in an array along with corresponding table names also held in an array. With these 1D arrays I am able to put them all into just two session and retreive later to split the strings up into servername, password, userid, databasename & tablename etc. The problem comes when using 2 elements of one 2D array.
When retreiving information from session it only returns current elements.
The previously stored records get lost some how or maybe overwritten/cleared.
Thanks in advance for any help given.
0
 
LVL 28

Accepted Solution

by:
Ark earned 2000 total points
ID: 38423977
Dim myArrayTEMP() 'Create an empty array
ReDim Preserve myArrayTEMP(RowCount,2) 'Re-Declaring a dynamic array - now array contains (rowcount+1)x3 elements equals to vbEmpty
myArrayTEMP(RowCount,2)   = Session("sessionmyArray") 'Set last element(in last row and last column) to session variable - array(?). Probably you meant 
'myArrayTEMP=Session("sessionmyArray")  to assign array to new array? Or use for/next loop to fill myArrayTEMP.
myArrayTEMP(RowCount,1)   = CString 'Set lastrow and second column element to CString
myArrayTEMP(RowCount,2)   = TableLabel 'Reset last element to TableLabel
'Now you array contains vbEmpty value in all elements except of 2 last elements

Open in new window

Now brief sample on dictionary using
dim dict As Object
dict=Session("ConnData")
If dict Is Nothing then
   Set dict=CreateObject("Scripting.Dictionary")
End If
If dict.Exists("SomeTableLabel") Then
   Response.Write("SomeTableLabel exists, connString: " & dict("SomeTableLabel") ) 
Else
    dict.Add "SomeTableLabel", CString
   Response.Write("SomeTableLabel added with connString: " & dict("SomeTableLabel") ) 
End If
For each k in dict.keys
     Response.write("<br/>" & k & ": " & dict.Item(k))
Next
Session("ConnData")=dict

Open in new window

More on dictionary using at http://www.windowsitpro.com/article/vbscript/understanding-vbscript-the-dictionary-object-an-alternative-to-arrays
0
 

Author Comment

by:homeshopper
ID: 38424481
Thank you for your helpful comments.
I have now been able to complete the solution with 2D array as code listed below:
Also, thank you for the example & reference to using the dictionary object as an alternative
to using arrays. I'll award the points.
Tomorrow, I'll look into your alternative solution.
Thanks again, Ian.
<%
Sub BindData3()
currentPage = Request.QueryString("PageID")
if currentPage = 0 then
Response.Write("<div style='position:absolute;width:837px;height:306px;left:0px;top:318px;font-size:xx-small;overflow:auto;border:1px solid #722F90;' id='DIV999'>")
Response.Write("header2:" & currentPage & "#BindData3():<br/>")
Dim strRow    
Dim strColumn
Dim RowCount
Dim ColumnCount
Dim dCounter
Dim oCounter
Dim strCountRow
Dim strCountCol
strColumn   = 1
CString     = Session("sessionCString")
TableLabel  = Session("sessionTableLabel")
RowCount    = Session("strRowCount")
'****************************************
%>
<%
if RowCount = "" then
RowCount = 0
End If
%>
<%
Dim myArray()
ReDim Preserve myArray(RowCount,2) 'Re-Declaring a dynamic array
if CString = "Provider=;Data Source=;User Id=;Password=;Initial Catalog=;" then

Else
%>
<%

%>
<%
if RowCount = 0 then
%>
<%
Response.Write("338:h2:BD3:RowCount:" & RowCount & "#    CString:" & CString & "#    TableLabel:" & TableLabel & "#")
myArray(RowCount,1)   = CString
myArray(RowCount,2)   = TableLabel
response.write "<br/>341:array assigned to session var..."
strCountRow     = UBound(myArray, 1)
strCountCol     = UBound(myArray, 2) ' or the other way around...
response.write ",  array dimensions: " & strCountRow & " x " & strCountCol
Session("sessionmyArray") = myArray
%>
<%
Else
%>
<%
RowCount    = Session("strRowCount")
Response.Write("352:h2:BD3:RowCount:" & RowCount & "#    CString:" & CString & "#    TableLabel:" & TableLabel & "#")
Dim myArrayTEMP() 'Create an empty array
ReDim Preserve myArrayTEMP(RowCount,2) 'Re-Declaring a dynamic array - now array contains (rowcount+1)x3 elements equals to vbEmpty
for i=0 to uBound(myArrayTEMP)-1
myArrayTEMP(i,1) = Session("sessionmyArray")(i,1)
myArrayTEMP(i,2) = Session("sessionmyArray")(i,2)
Next
myArrayTEMP(RowCount,1)   = CString 'Set lastrow and second column element to CString
myArrayTEMP(RowCount,2)   = TableLabel 'Reset last element to TableLabel
response.write "<br/>361:array assigned to session var..."
strCountRow     = UBound(myArrayTEMP, 1)
strCountCol     = UBound(myArrayTEMP, 2) ' or the other way around...
response.write ",  array dimensions: " & strCountRow & " x " & strCountCol
Session("sessionmyArray") = myArrayTEMP
%>
<%
End If 'RowCount
End If 'CString
%>
<%
if Session("strConnection") = "Data Source=;User Id=;Password=;" then
   
    else
%>
<%
    Dim strRowArray
    strRowArray = Session("sessionmyArray") ' strRowArray(i, j)
If IsArray(strRowArray) Then
      strCountRow = UBound(strRowArray, 1)
      response.write "384:array dimensions: " & strCountRow & " x " & strCountCol
Else
      response.write "no array"
End If
%>
<%
Response.Write("<br/>")
For j = 0 To strCountRow
If j = 0 then
Response.Write("(" & chr(j + 65) & ")" & strRowArray(j,1) & "<a href='../scriptFrame/tempDefault" & (currentPage) & ".asp?db22=" & strRowArray(j,1) & "&dt22=" & strRowArray(j,2) & "&intDetail22=" & j & "&strD22=False" & "&PageID=" & (currentPage) & " ' title='" & strRowArray(j,1) & strRowArray(j,2) & "' runat='server'>" & "<font color='#0000FF' size='1'>" & strRowArray(j,2) & "</font>" & "</a><br/>")
Else
Response.Write("(" & chr(j + 65) & ")" & strRowArray(j,1) & "<a href='../scriptFrame/tempDefault" & (currentPage) & ".asp?db22=" & strRowArray(j,1) & "&dt22=" & strRowArray(j,2) & "&intDetail22=" & j & "&strD22=False" & "&PageID=" & (currentPage) & " ' title='" & strRowArray(j,1) & strRowArray(j,2) & "' runat='server'>" & "<font color='#0000FF' size='1'>" & strRowArray(j,2) & "</font>" & "</a><br/>")
End If
Next ' j
End If
%>
<%
'****************************************
RowCount = RowCount + 1
Session("strRowCount") = RowCount
Response.Write("</div>")
End If  
End Sub
%>
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Measuring Server's processing rate with a simple powershell command. The differences in processing rate also was recorded in different use-cases, when a server in free and busy states.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month15 days, 23 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question