codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:
Search Forums:
  looping thru an array - Going Crazy!!!  seala at 19:18 on Thursday, September 25, 2003
 

I need to look at each element in an array to write it to the web page, one element at a time. The previous page contained a form of dynamically created checkbox sets. The number of checkbox sets will always be unknown. Anyway, here's my question:

I've tried different code (due to circumstances that would be to lengthy to get into here, I need to use vbscript, be able to use javascript within vbscript code.

Here are examples I've tried and the results:

First attempt:
CkBoxArray="("
for each objItem in request.Form()
if left(objItem,5)="cksub" and request.Form(objItem) <> "" then
CkBoxArray=CkBoxArray & request.Form(objItem) & ","
end if
next
theLen=len(CkBoxArray)
CkBoxArray=left(CkBoxArray,theLen-1)
CkBoxArray=CkBoxArray & ")"
response.Write("Checkboxes with value of cksub - CkBoxArray contents are: " & CkBoxArray & "<br>")

'Results for above code are:
Checkboxes with value of cksub - CkBoxArray contents are: (4402, 4403,4201, 4202, 4203)
written to page

Second attempt:
ReDim CkBoxArray(0)

for each objItem in request.Form()
if left(objItem,5)="cksub" and request.Form(objItem) <> "" then
'Store the value entered by the user in the array
CkBoxArray(UBound(CkBoxArray)) = request.Form(objItem)
'grow the array to be one element greater than its current size
'Preserve its contents
ReDim Preserve CkBoxArray(UBound(CkBoxArray) + 1)
end if
next

The for each loop below results in a display of (based on selection):
4402, 4403
4201, 4202, 4203

Both scenarios assume two checkbox sets, but like I said the sets are built dynamically and are unknown.

When I write out to the web page, I want the type of results shown below:

Value of element in array = 4402
Value of element in array = 4403
Value of element in array = 4201
Value of element in array = 4202
Value of element in array = 4203

Eventually I'll be writing each element to its own record in a db, but am currently debugging code just trying to separate each unknown element on its by writing it to the page. I'm pulling out all of my hair on this one....Any suggestions before its all gone??? Thanks!!!!!

<Added>

Sorry, forgot the code for after:
The for each loop below results in a display of (based on selection):
4402, 4403
4201, 4202, 4203


For Each x in CkBoxArray
response.write(x)
Next

  Re: looping thru an array - Going Crazy!!!  Troy Wolf at 20:45 on Thursday, September 25, 2003
 

I think I answered this in this thread:
http://www.codetoad.com/forum/16_22682.asp
:)
Troy Wolf: site expert
Shiny Solutions


  Problem with one line of code - how to fix?  seala at 18:00 on Saturday, September 27, 2003
 

I've revamped my original code and everything works except for grabbing the registerID.

This is the area I'm having problems with:
'execute SQL statement, to get newly created RegisterId
iRegisterId = 1 '***USE THIS AND THE REST OF THE CODE WORKS, BUT DOESN'T GET FROM FIRST TABLE, JUST HARDCODED
'iRegisterID = connupdate("Register_ID") '*** NEED TO USE THIS AND IT DOESN'T WORK??!


Here is the code that worked, except for being able to grab the Register_ID after the record is inserted into the first table, so that I can use in as part of the insert into the second table:

Here is the entire code for the page:
<% Option Explicit %>

<!-- #include file="adovbs.inc" -->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
</HEAD>

<BODY>

<%
Dim aCatIds, _
aSubCatIds
Dim iGroupId, _
iCntCatId, _
iCntSubCatId, _
iSubCatId, _
iRegisterId
Dim FormNotify
Dim strNotify
Dim sFld, _
sSql
Dim connupdate

sSql = "INSERT INTO Webregister (Company_Name, St_Address, City, State, Zip, Phone_Num, Fax_Num, Web_Site, Contact_Person, Contact_Phone, Contact_Email, Respond_Da, E_Notify) Values ('" & Replace(Request.Form("D_Company"), "'", "") & "','" & Replace(Request.Form("D_Address"), "'", "") & "','" & Replace(Request.Form("D_City"), "'", "") & "','" & Replace(Request.Form("D_State"), "'", "") & "','" & Replace(Request.Form("D_Zip"), "'", "") & "','" & Replace(Request.Form("D_Phone"), "'", "") & "','" & Replace(Request.Form("D_Fax"), "'", "") & "','" & Replace(Request.Form("D_Website"), "'", "") & "','" & Replace(Request.Form("D_Contact"), "'", "") & "','" & Replace(Request.Form("D_CPhone"), "'", "") & "','" & Replace(Request.Form("D_Email"), "'", "") & "','" & Replace(Request.Form("hidDate"), "'", "") & "','" & Request.Form("notify") & "')"

set connupdate = server.createobject("ADODB.Connection")
connupdate.open "Registration"
connupdate.execute(sSql)

'execute SQL statement, to get newly created RegisterId
iRegisterId = 1 '***USE THIS AND THE REST OF THE CODE WORKS, BUT DOESN'T GET FROM FIRST TABLE, JUST HARDCODED
'iRegisterID = connupdate("Register_ID") '*** NEED TO USE THIS AND IT DOESN'T WORK??!

aCatIds = Split(Request.Form("GroupID"), ", ")
For iCntCatId = 0 To UBound(aCatIds)
iGroupId = aCatIds(iCntCatId)

aSubCatIds = Split(Request.Form("SubCatId_" & iGroupId), ", ")
For iCntSubCatId = 0 To UBound(aSubCatIds)
iSubCatId = aSubCatIds(iCntSubCatId)

sSql = "INSERT INTO WebCommodities (Register_ID, GroupID, SubCategory) Values(" & vbCrLf & _
" " & iRegisterId & "," & vbCrLf & _
" " & iGroupId & "," & vbCrLf & _
" " & iSubCatId & vbCrLf & _
")"
'execute SQL statement
connupdate.execute(sSql)
Next
Next

connupdate.close
set connupdate = nothing

%>

</BODY>
</HTML>

I know I need to also use adOpenKeySet to make sure I grab the right id.

In my original code, I used this for connection:
set connupdate = server.createobject("ADODB.Connection")
strProvider="Provider=MSDASQL.1;Persist Security Info=False;Data Source=Registration"
connupdate.Open "WebRegister", strProvider, adOpenKeySet, adLockPessimistic
but for some reason it didn't like my new code - any ideas?











CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums








Recent Forum Threads
•  XML- Passing parameters are Latitude and Longitude.
•  Re: Format Date time issue....
•  Re: How to freeze the web page when displaying progress bar?
•  Re: Help! IFrames!
•  Re: Help: Trouble with z-Index and SELECT lists
•  Re: HOW CREATE LINE CHARTS IN ASP.NET
•  How to store and retrieve document into database
•  Re: text field multiple
•  Re: after updation of a table open another program .. need help


Recent Articles
Communicating with the Database (Using ADO)
MagicGrid
Simple Thumbnail Browsing Solution
Type Anywhere
A Better Moustrap: FmtDate to replace FormatDateTime
ASP.NET Forum Source Code
Internal Search Engine
Javascript Growing Window
Simple date validation
Search engine friendly URLs using ASP.NET (C#.NET)


Site Survey
Help us serve you better. Take a five minute survey. Click here!

© Copyright codetoad.com 2001-2005