ASP Insertion Problem

He has: 183 posts

Joined: Nov 2003

Hi,

I'm trying to add a large amount of data in to a table.I can view all the data in a table using a repeat region, but i can only submit one row at a time OR all the data, but it adds it in to just 1 row in the database. I would like it to add all the rows at the same time in to diffurant rows in the database. Here is what i have so far:

<%
While ((Repeat1__numRows <> 0) AND (NOT NewUsernames.EOF))
%>
        <table width="100%"  border="0">
          <tr>
            <td><table width="90%"  border="0" onMouseOver="this.bgColor = '#CCCCCC';this.style.cursor='hand'" onMouseOut ="this.bgColor = 'white'" bgcolor="white" bordercolor="#FFFFFF">
                <tr>
                  <td width="19%"><input name="Username" type="text" class="NoCaps" id="Username" value="<%=(NewUsernames.Fields.Item("FirstName").Value)%>.<%=(NewUsernames.Fields.Item("SurnameName").Value)%>" size="30"></td>
                  <td width="15%"><input name="Year" type="text" class="NoCaps" id="Year" value="<%=(NewUsernames.Fields.Item("Year").Value)%>"></td>
                  <td width="15%"><input name="Form" type="text" class="NoCaps" id="Form" value="<%=(NewUsernames.Fields.Item("Form").Value)%>"></td>
                  <td width="15%"><input name="DOB" type="text" class="NoCaps" id="DOB" value="<%=(NewUsernames.Fields.Item("DOB").Value)%>"></td>
                  <td width="19%"><input name="Password" type="hidden" id="Password" value="<%=(NewUsernames.Fields.Item("FirstName").Value)%>.<%=(NewUsernames.Fields.Item("SurnameName").Value)%>">
                    <input name="School" type="hidden" id="School" value="Ripon College">
                    <input name="SchoolID" type="hidden" id="SchoolID" value="11SL">
                    <input name="AccessPageAdmin" type="hidden" id="AccessPageAdmin" value="/StudentLogin/Student.asp">
                    <input name="Done_" type="hidden" id="Done_" value="Done">
                  </td>
                  <td width="17%"><input type="submit" name="Submit" value="Submit">
                  </td>
                </tr>
              </table></td>
          </tr>
                </table>
        <%
  Repeat1__index=Repeat1__index+1
  Repeat1__numRows=Repeat1__numRows-1
  NewUsernames.MoveNext()
Wend
%>

        <input type="hidden" name="MM_insert" value="form2">
'

And here is my recordsets:

<!--#include file="../../Connections/SchoolDatabase01.asp" -->
<%
' *** Edit Operations: declare variables

Dim MM_editAction
Dim MM_abortEdit
Dim MM_editQuery
Dim MM_editCmd

Dim MM_editConnection
Dim MM_editTable
Dim MM_editRedirectUrl
Dim MM_editColumn
Dim MM_recordId

Dim MM_fieldsStr
Dim MM_columnsStr
Dim MM_fields
Dim MM_columns
Dim MM_typeArray
Dim MM_formVal
Dim MM_delim
Dim MM_altVal
Dim MM_emptyVal
Dim MM_i

MM_editAction = CStr(Request.ServerVariables("SCRIPT_NAME"))
If (Request.QueryString <> "") Then
  MM_editAction = MM_editAction & "?" & Server.HTMLEncode(Request.QueryString)
End If

' boolean to abort record edit
MM_abortEdit = false

' query string to execute
MM_editQuery = ""
%>
<%
' *** Insert Record: set variables

If (CStr(Request("MM_insert")) = "form2") Then

  MM_editConnection = MM_SchoolDatabase01_STRING
  MM_editTable = "AllUsers"
  MM_editRedirectUrl = "NewList.asp"
  MM_fieldsStr  = "Username|value|Year|value|Form|value|DOB|value|Password|value|School|value|SchoolID|value|AccessPageAdmin|value|Done_|value"
  MM_columnsStr = "Username|',none,''|Year|',none,''|Form|',none,''|DOB|',none,''|Password|',none,''|School|',none,''|SchoolID|',none,''|AccessPageAdmin|',none,''|Done_|',none,''"

  ' create the MM_fields and MM_columns arrays
  MM_fields = Split(MM_fieldsStr, "|")
  MM_columns = Split(MM_columnsStr, "|")
 
  ' set the form values
  For MM_i = LBound(MM_fields) To UBound(MM_fields) Step 2
    MM_fields(MM_i+1) = CStr(Request.Form(MM_fields(MM_i)))
  Next

  ' append the query string to the redirect URL
  If (MM_editRedirectUrl <> "" And Request.QueryString <> "") Then
    If (InStr(1, MM_editRedirectUrl, "?", vbTextCompare) = 0 And Request.QueryString <> "") Then
      MM_editRedirectUrl = MM_editRedirectUrl & "?" & Request.QueryString
    Else
      MM_editRedirectUrl = MM_editRedirectUrl & "&" & Request.QueryString
    End If
  End If

End If
%>
<%
' *** Insert Record: construct a sql insert statement and execute it

Dim MM_tableValues
Dim MM_dbValues

If (CStr(Request("MM_insert")) <> "") Then

  ' create the sql insert statement
  MM_tableValues = ""
  MM_dbValues = ""
  For MM_i = LBound(MM_fields) To UBound(MM_fields) Step 2
    MM_formVal = MM_fields(MM_i+1)
    MM_typeArray = Split(MM_columns(MM_i+1),",")
    MM_delim = MM_typeArray(0)
    If (MM_delim = "none") Then MM_delim = ""
    MM_altVal = MM_typeArray(1)
    If (MM_altVal = "none") Then MM_altVal = ""
    MM_emptyVal = MM_typeArray(2)
    If (MM_emptyVal = "none") Then MM_emptyVal = ""
    If (MM_formVal = "") Then
      MM_formVal = MM_emptyVal
    Else
      If (MM_altVal <> "") Then
        MM_formVal = MM_altVal
      ElseIf (MM_delim = "'") Then  ' escape quotes
        MM_formVal = "'" & Replace(MM_formVal,"'","''") & "'"
      Else
        MM_formVal = MM_delim + MM_formVal + MM_delim
      End If
    End If
    If (MM_i <> LBound(MM_fields)) Then
      MM_tableValues = MM_tableValues & ","
      MM_dbValues = MM_dbValues & ","
    End If
    MM_tableValues = MM_tableValues & MM_columns(MM_i)
    MM_dbValues = MM_dbValues & MM_formVal
  Next
  MM_editQuery = "insert into " & MM_editTable & " (" & MM_tableValues & ") values (" & MM_dbValues & ")"

  If (Not MM_abortEdit) Then
    ' execute the insert
    Set MM_editCmd = Server.CreateObject("ADODB.Command")
    MM_editCmd.ActiveConnection = MM_editConnection
    MM_editCmd.CommandText = MM_editQuery
    MM_editCmd.Execute
    MM_editCmd.ActiveConnection.Close

    If (MM_editRedirectUrl <> "") Then
      Response.Redirect(MM_editRedirectUrl)
    End If
  End If

End If
%>
<%
Dim NewUsernames
Dim NewUsernames_numRows

Set NewUsernames = Server.CreateObject("ADODB.Recordset")
NewUsernames.ActiveConnection = MM_SchoolDatabase01_STRING
NewUsernames.Source = "SELECT * FROM NewUserNames ORDER BY SurnameName ASC"
NewUsernames.CursorType = 0
NewUsernames.CursorLocation = 2
NewUsernames.LockType = 1
NewUsernames.Open()

NewUsernames_numRows = 0
%>
<%
Dim Repeat1__numRows
Dim Repeat1__index

Repeat1__numRows = -1
Repeat1__index = 0
NewUsernames_numRows = NewUsernames_numRows + Repeat1__numRows
%>
'

If anyone who knows how to solve this it would be a great help to me. Thanks

They have: 5,633 posts

Joined: Jan 1970

That was some what confuseing but here is what I got... you want a online form that you can fill out and it will add a few rows in a access db?

He has: 183 posts

Joined: Nov 2003

Sorry.

Yes. The code above is what i have so far. I can add just a single row at a time, or all the rows in the table in to a single row in a access database. But what i would like is to add each row on the table in to its own row in the access database.

I hope this helps.

They have: 5,633 posts

Joined: Jan 1970

I'm not going to write the whole code for you but I can point you in the right direction...
- Your first proublem is letting macromedia write the code for you.
- Second your writeing in VB the only real purpose is VB is for asp classic.
- 3rd, your writeing in ASP classic useing a access db. There are better technologies out there.

But if you still wish to stick with what u have now....
See this...

  MM_editQuery = "insert into " & MM_editTable & " (" & MM_tableValues & ") values (" & MM_dbValues & ")"
'
"MM_editQuery" is a SQL string. The "insert into" keywords in a SQL string tell the database to add a new row.
  If (Not MM_abortEdit) Then
    ' execute the insert
    Set MM_editCmd = Server.CreateObject("ADODB.Command")
    MM_editCmd.ActiveConnection = MM_editConnection
    MM_editCmd.CommandText = MM_editQuery
    MM_editCmd.Execute
    MM_editCmd.ActiveConnection.Close
'
The row really isnt added untill this section is ran. Write the page so it runs the SQL sting for each row you want to add.

He has: 183 posts

Joined: Nov 2003

Cheers for that.

For future notes, what would you say be the best langauge to learn? I find ASP and Access easy to understand the basics so i thought i would stick to it.

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.