Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Array Help Please.

Omega99Omega99 Member Posts: 19
I am trying to create an array then populate a dropdown list with it. I am not sure what I am doing wrong here. I am just learning about arrays, so any help with this would be greatly appreciated.

Sample Code:

<%
Dim myCN
Dim myRS


X = 0

Set myCN = CreateObject ("ADODB.Connection")
Set myRS = CreateObject ("ADODB.Recordset")

myCN.ConnectionString="Provider=MSDataShape; Driver=SQL Server;Server=sqlserver; UID=sa; PWD=newpass; DATABASE=myDB"

myCN.Open
myRS.ActiveConnection = myCN

myRS.Open "SELECT Name FROM tblEmp ORDER BY Name"
Dim MyArray(0 to 300)as string
if not myRS.EOF then

do while not myRS.EOF
MyArray (X) = myRS ("Name")
X = X + 1
Response.write "<option value="MyArray (X)"</option>"
myRS.MoveNext
loop
myRS.close
end if

%>

Comments

  • ManningManning Member Posts: 1,621
    [b][red]This message was edited by Manning at 2004-4-30 16:58:2[/red][/b][hr]
    : I am trying to create an array then populate a dropdown list with it. I am not sure what I am doing wrong here. I am just learning about arrays, so any help with this would be greatly appreciated.
    :
    : Sample Code:
    :
    : <%
    : Dim myCN
    : Dim myRS
    :
    :
    : X = 0
    :
    : Set myCN = CreateObject ("ADODB.Connection")
    : Set myRS = CreateObject ("ADODB.Recordset")
    :
    : myCN.ConnectionString="Provider=MSDataShape; Driver=SQL Server;Server=sqlserver; UID=sa; PWD=newpass; DATABASE=myDB"
    :
    : myCN.Open
    : myRS.ActiveConnection = myCN
    :
    : myRS.Open "SELECT Name FROM tblEmp ORDER BY Name"
    : Dim MyArray(0 to 300)as string
    : if not myRS.EOF then
    :
    : do while not myRS.EOF
    : MyArray (X) = myRS ("Name")
    : X = X + 1
    : Response.write "<option value="MyArray (X)"</option>"
    : myRS.MoveNext
    : loop
    : myRS.close
    : end if
    :
    : %>


    There's no reason for the array in this case. Try this instead:

    [code]
    <%
    Dim myCN
    Dim myRS

    Set myCN = CreateObject ("ADODB.Connection")
    Set myRS = CreateObject ("ADODB.Recordset")

    myCN.ConnectionString="Provider=MSDataShape; Driver=SQL " & _
    "Server;Server=sqlserver; UID=sa; PWD=newpass; DATABASE=myDB"

    myCN.Open
    myRS.ActiveConnection = myCN

    myRS.Open "SELECT Name FROM tblEmp ORDER BY Name"
    do while not myRS.EOF
    Response.write "<option value=""" & myRS("Name") & """>" & myRS("Name") & ""
    myRS.MoveNext
    loop
    myRS.close
    %>
    [/code]
  • Omega99Omega99 Member Posts: 19
    : [b][red]This message was edited by Manning at 2004-4-30 16:58:2[/red][/b][hr]
    : : I am trying to create an array then populate a dropdown list with it. I am not sure what I am doing wrong here. I am just learning about arrays, so any help with this would be greatly appreciated.
    : :
    : : Sample Code:
    : :
    : : <%
    : : Dim myCN
    : : Dim myRS
    : :
    : :
    : : X = 0
    : :
    : : Set myCN = CreateObject ("ADODB.Connection")
    : : Set myRS = CreateObject ("ADODB.Recordset")
    : :
    : : myCN.ConnectionString="Provider=MSDataShape; Driver=SQL Server;Server=sqlserver; UID=sa; PWD=newpass; DATABASE=myDB"
    : :
    : : myCN.Open
    : : myRS.ActiveConnection = myCN
    : :
    : : myRS.Open "SELECT Name FROM tblEmp ORDER BY Name"
    : : Dim MyArray(0 to 300)as string
    : : if not myRS.EOF then
    : :
    : : do while not myRS.EOF
    : : MyArray (X) = myRS ("Name")
    : : X = X + 1
    : : Response.write "<option value="MyArray (X)"</option>"
    : : myRS.MoveNext
    : : loop
    : : myRS.close
    : : end if
    : :
    : : %>
    :
    :
    : There's no reason for the array in this case. Try this instead:
    :
    : [code]
    : <%
    : Dim myCN
    : Dim myRS
    :
    : Set myCN = CreateObject ("ADODB.Connection")
    : Set myRS = CreateObject ("ADODB.Recordset")
    :
    : myCN.ConnectionString="Provider=MSDataShape; Driver=SQL " & _
    : "Server;Server=sqlserver; UID=sa; PWD=newpass; DATABASE=myDB"
    :
    : myCN.Open
    : myRS.ActiveConnection = myCN
    :
    : myRS.Open "SELECT Name FROM tblEmp ORDER BY Name"
    : do while not myRS.EOF
    : Response.write "<option value=""" & myRS("Name") & """>" & myRS("Name") & ""
    : myRS.MoveNext
    : loop
    : myRS.close
    : %>
    : [/code]
    :


    I did initially do it this way. I need to be able to pass the "Name" variable to the next page. I was having a hard time passing the variable from the drop down list to the next page. Do you know a way to get the Name variable to the next page doing this?
  • ManningManning Member Posts: 1,621
    : : [b][red]This message was edited by Manning at 2004-4-30 16:58:2[/red][/b][hr]
    : : : I am trying to create an array then populate a dropdown list with it. I am not sure what I am doing wrong here. I am just learning about arrays, so any help with this would be greatly appreciated.
    : : :
    : : : Sample Code:
    : : :
    : : : <%
    : : : Dim myCN
    : : : Dim myRS
    : : :
    : : :
    : : : X = 0
    : : :
    : : : Set myCN = CreateObject ("ADODB.Connection")
    : : : Set myRS = CreateObject ("ADODB.Recordset")
    : : :
    : : : myCN.ConnectionString="Provider=MSDataShape; Driver=SQL Server;Server=sqlserver; UID=sa; PWD=newpass; DATABASE=myDB"
    : : :
    : : : myCN.Open
    : : : myRS.ActiveConnection = myCN
    : : :
    : : : myRS.Open "SELECT Name FROM tblEmp ORDER BY Name"
    : : : Dim MyArray(0 to 300)as string
    : : : if not myRS.EOF then
    : : :
    : : : do while not myRS.EOF
    : : : MyArray (X) = myRS ("Name")
    : : : X = X + 1
    : : : Response.write "<option value="MyArray (X)"</option>"
    : : : myRS.MoveNext
    : : : loop
    : : : myRS.close
    : : : end if
    : : :
    : : : %>
    : :
    : :
    : : There's no reason for the array in this case. Try this instead:
    : :
    : : [code]
    : : <%
    : : Dim myCN
    : : Dim myRS
    : :
    : : Set myCN = CreateObject ("ADODB.Connection")
    : : Set myRS = CreateObject ("ADODB.Recordset")
    : :
    : : myCN.ConnectionString="Provider=MSDataShape; Driver=SQL " & _
    : : "Server;Server=sqlserver; UID=sa; PWD=newpass; DATABASE=myDB"
    : :
    : : myCN.Open
    : : myRS.ActiveConnection = myCN
    : :
    : : myRS.Open "SELECT Name FROM tblEmp ORDER BY Name"
    : : do while not myRS.EOF
    : : Response.write "<option value=""" & myRS("Name") & """>" & myRS("Name") & ""
    : : myRS.MoveNext
    : : loop
    : : myRS.close
    : : %>
    : : [/code]
    : :
    :
    :
    : I did initially do it this way. I need to be able to pass the "Name" variable to the next page. I was having a hard time passing the variable from the drop down list to the next page. Do you know a way to get the Name variable to the next page doing this?


    That depends on what you mean.

    Do you want to pass ALL the Name records from the tblEmp table? If so, it's best to just perform the query again on the next page.

    If you mean you want to pass just the Name that they select from the drop-down, then that is handled by the form. I assumed the code you posted was a subset of that form. Here is a full example:

    [code]


    <%
    Dim myCN
    Dim myRS

    Set myCN = CreateObject ("ADODB.Connection")
    Set myRS = CreateObject ("ADODB.Recordset")

    myCN.ConnectionString="Provider=MSDataShape; Driver=SQL " & _
    "Server;Server=sqlserver; UID=sa; PWD=newpass; DATABASE=myDB"

    myCN.Open
    myRS.ActiveConnection = myCN

    myRS.Open "SELECT Name FROM tblEmp ORDER BY Name"
    do while not myRS.EOF
    Response.write "<option value=""" & myRS("Name") & """>" & myRS("Name") & ""
    myRS.MoveNext
    loop
    myRS.close
    %>



    [/code]

    Now, when you hit the "To Page 2!" button, it send the form contents to page2.asp. On that page, you can refer to their selections by referencing the Request.Form collection. To get the name from the combo box, it would be Request.Form("cboName")
  • Omega99Omega99 Member Posts: 19
    : : : [b][red]This message was edited by Manning at 2004-4-30 16:58:2[/red][/b][hr]
    : : : : I am trying to create an array then populate a dropdown list with it. I am not sure what I am doing wrong here. I am just learning about arrays, so any help with this would be greatly appreciated.
    : : : :
    : : : : Sample Code:
    : : : :
    : : : : <%
    : : : : Dim myCN
    : : : : Dim myRS
    : : : :
    : : : :
    : : : : X = 0
    : : : :
    : : : : Set myCN = CreateObject ("ADODB.Connection")
    : : : : Set myRS = CreateObject ("ADODB.Recordset")
    : : : :
    : : : : myCN.ConnectionString="Provider=MSDataShape; Driver=SQL Server;Server=sqlserver; UID=sa; PWD=newpass; DATABASE=myDB"
    : : : :
    : : : : myCN.Open
    : : : : myRS.ActiveConnection = myCN
    : : : :
    : : : : myRS.Open "SELECT Name FROM tblEmp ORDER BY Name"
    : : : : Dim MyArray(0 to 300)as string
    : : : : if not myRS.EOF then
    : : : :
    : : : : do while not myRS.EOF
    : : : : MyArray (X) = myRS ("Name")
    : : : : X = X + 1
    : : : : Response.write "<option value="MyArray (X)"</option>"
    : : : : myRS.MoveNext
    : : : : loop
    : : : : myRS.close
    : : : : end if
    : : : :
    : : : : %>
    : : :
    : : :
    : : : There's no reason for the array in this case. Try this instead:
    : : :
    : : : [code]
    : : : <%
    : : : Dim myCN
    : : : Dim myRS
    : : :
    : : : Set myCN = CreateObject ("ADODB.Connection")
    : : : Set myRS = CreateObject ("ADODB.Recordset")
    : : :
    : : : myCN.ConnectionString="Provider=MSDataShape; Driver=SQL " & _
    : : : "Server;Server=sqlserver; UID=sa; PWD=newpass; DATABASE=myDB"
    : : :
    : : : myCN.Open
    : : : myRS.ActiveConnection = myCN
    : : :
    : : : myRS.Open "SELECT Name FROM tblEmp ORDER BY Name"
    : : : do while not myRS.EOF
    : : : Response.write "<option value=""" & myRS("Name") & """>" & myRS("Name") & ""
    : : : myRS.MoveNext
    : : : loop
    : : : myRS.close
    : : : %>
    : : : [/code]
    : : :
    : :
    : :
    : : I did initially do it this way. I need to be able to pass the "Name" variable to the next page. I was having a hard time passing the variable from the drop down list to the next page. Do you know a way to get the Name variable to the next page doing this?
    :
    :
    : That depends on what you mean.
    :
    : Do you want to pass ALL the Name records from the tblEmp table? If so, it's best to just perform the query again on the next page.
    :
    : If you mean you want to pass just the Name that they select from the drop-down, then that is handled by the form. I assumed the code you posted was a subset of that form. Here is a full example:
    :
    : [code]
    :
    :
    : <%
    : Dim myCN
    : Dim myRS
    :
    : Set myCN = CreateObject ("ADODB.Connection")
    : Set myRS = CreateObject ("ADODB.Recordset")
    :
    : myCN.ConnectionString="Provider=MSDataShape; Driver=SQL " & _
    : "Server;Server=sqlserver; UID=sa; PWD=newpass; DATABASE=myDB"
    :
    : myCN.Open
    : myRS.ActiveConnection = myCN
    :
    : myRS.Open "SELECT Name FROM tblEmp ORDER BY Name"
    : do while not myRS.EOF
    : Response.write "<option value=""" & myRS("Name") & """>" & myRS("Name") & ""
    : myRS.MoveNext
    : loop
    : myRS.close
    : %>
    :
    :
    :
    : [/code]
    :
    : Now, when you hit the "To Page 2!" button, it send the form contents to page2.asp. On that page, you can refer to their selections by referencing the Request.Form collection. To get the name from the combo box, it would be Request.Form("cboName")
    :
    I really feel like I am getting close to cracking this problem. I really appreciate your help! I just want the one name value that is selected. I am now getting a value passed to the second page, but it is a numerical value. For example if I pick the 4th name in the dropdown list I get the value of 4 when I do a response.write. Instead of the name Doe, John I get the number 4. Is there a way to grab the text from the dropdown list?

    Thanks again,

    Bryan

  • ManningManning Member Posts: 1,621
    : I really feel like I am getting close to cracking this problem. I really appreciate your help! I just want the one name value that is selected. I am now getting a value passed to the second page, but it is a numerical value. For example if I pick the 4th name in the dropdown list I get the value of 4 when I do a response.write. Instead of the name Doe, John I get the number 4. Is there a way to grab the text from the dropdown list?


    You control the passed value by specifying a VALUE parameter in the tag. For example, here are 3 different ways of making a select option:

    [code]

    Doe, John
    Doe, John
    Doe, John

    [/code]

    If the user were to select the first item, Request.Form("test") would be "4" because that's what we said its VALUE was. The second item in the list would give a Request.Form("test") value of "John Doe" for the same reason as the first. This is how you need your option to look if you want to pass the name instead of ID.

    I'm not sure what the 3rd option would do, because I always specify a value. I believe it would give a Request.Form("test") value of "Doe, John", but it's always better to supply a value rather than rely on the unknown.
  • Omega99Omega99 Member Posts: 19
    : : I really feel like I am getting close to cracking this problem. I really appreciate your help! I just want the one name value that is selected. I am now getting a value passed to the second page, but it is a numerical value. For example if I pick the 4th name in the dropdown list I get the value of 4 when I do a response.write. Instead of the name Doe, John I get the number 4. Is there a way to grab the text from the dropdown list?
    :
    :
    : You control the passed value by specifying a VALUE parameter in the tag. For example, here are 3 different ways of making a select option:
    :
    : [code]
    :
    : Doe, John
    : Doe, John
    : Doe, John
    :
    : [/code]
    :
    : If the user were to select the first item, Request.Form("test") would be "4" because that's what we said its VALUE was. The second item in the list would give a Request.Form("test") value of "John Doe" for the same reason as the first. This is how you need your option to look if you want to pass the name instead of ID.
    :
    : I'm not sure what the 3rd option would do, because I always specify a value. I believe it would give a Request.Form("test") value of "Doe, John", but it's always better to supply a value rather than rely on the unknown.
    :
    Thanks Manning,

    You were a great help it works!!!!

    I really appreciate your help!
  • x_rayx_ray Member Posts: 71
    : [b][red]This message was edited by Manning at 2004-4-30 16:58:2[/red][/b][hr]
    : : I am trying to create an array then populate a dropdown list with it. I am not sure what I am doing wrong here. I am just learning about arrays, so any help with this would be greatly appreciated.
    : :
    : : Sample Code:
    : :
    : : <%
    : : Dim myCN
    : : Dim myRS
    : :
    : :
    : : X = 0
    : :
    : : Set myCN = CreateObject ("ADODB.Connection")
    : : Set myRS = CreateObject ("ADODB.Recordset")
    : :
    : : myCN.ConnectionString="Provider=MSDataShape; Driver=SQL Server;Server=sqlserver; UID=sa; PWD=newpass; DATABASE=myDB"
    : :
    : : myCN.Open
    : : myRS.ActiveConnection = myCN
    : :
    : : myRS.Open "SELECT Name FROM tblEmp ORDER BY Name"
    : : Dim MyArray(0 to 300)as string
    : : if not myRS.EOF then
    : :
    : : do while not myRS.EOF
    : : MyArray (X) = myRS ("Name")
    : : X = X + 1
    : : Response.write "<option value="MyArray (X)"</option>"
    : : myRS.MoveNext
    : : loop
    : : myRS.close
    : : end if
    : :
    : : %>
    :
    :
    : There's no reason for the array in this case. Try this instead:
    :
    : [code]
    : <%
    : Dim myCN
    : Dim myRS
    :
    : Set myCN = CreateObject ("ADODB.Connection")
    : Set myRS = CreateObject ("ADODB.Recordset")
    :
    : myCN.ConnectionString="Provider=MSDataShape; Driver=SQL " & _
    : "Server;Server=sqlserver; UID=sa; PWD=newpass; DATABASE=myDB"
    :
    : myCN.Open
    : myRS.ActiveConnection = myCN
    :
    : myRS.Open "SELECT Name FROM tblEmp ORDER BY Name"
    : do while not myRS.EOF
    : Response.write "<option value=""" & myRS("Name") & """>" & myRS("Name") & ""
    : myRS.MoveNext
    : loop
    : myRS.close
    : %>
    : [/code]
    :
    the problem is u r incrementing before outputing.
Sign In or Register to comment.