Forums

ASP

This topic is locked

Can I do this?

Posted 29 Jul 2005 03:57:03
1
has voted
29 Jul 2005 03:57:03 Linda Farruggia posted:
Situation: Have an asp search page (note we are a public school). Have students tied to classes. Search page brings up class names and id in results click on ID - Results links to a page that lists all students in that class. Works great - now I need to take that list and tie it to an insert into attendance table. Can I have a dynamic page that returns student name, id, class name, and then inserts into a table for attendance? (Data base is sql)

I then want to be able to have (insert fields next to those names like attendance code and auto date.
Click on insert and voila it takes the list and inserts it into the attendance table!
I have tried several things and everytime I try to enter an insert form along in the results page - bingo error error? I have thought about session variables, but I really need to bring up a class list and just have users enter attendance code. I'm trying to keep it simple for teachers without having to go to several pages or worse yet bring up each student at a time.
Any help would be so greatly appreciated. It's probably something I should be writing in sql ??? Any suggestions in that area? I'm not real great yet with sql, but I have tried tieing tables in view, but that isn't help either.


Replies

Replied 02 Aug 2005 01:31:05
02 Aug 2005 01:31:05 Raafat Abdul replied:
It looks like you need to do a multiple insert and for that, you will need to write the code because Dreamweaver will not do it for you.

Now, in the page which returns all the students in a particular class:
1- Add hidden fields next to each field being returned by the record set and give each hidden field a name and the value of the record field.
2- Add a text box at the end of the dynamic table and call it "ATTENDANCE". This is the box where you will be writing the attendance mark next to each student. Assuming when you run the result page and it returned 5 student records, you will have 5 empty text boxes next to each student respectively.
3- Add a hidden field to store the number of records you have on the result page and you can use a small JavaScript to store that number. The number returned is used when doing the insertion loop.
4- Look at the code marked "The insertion code" in the page "students.asp"

In my example, I created 2 tables. A STUDENT table and ATTENDANCE table.
The STUDENTS table contains the fields:
ID as Number
NAME as string
CLASSNAME as string

The ATTENDANCE table contains the fields:
ID as Number
NAME as string
CLASSNAME as string
ATTENDANCE as string
DATE as system date

<b> Students Page "students.asp" </b>

<pre id=code><font face=courier size=2 id=code>
&lt;%@LANGUAGE="JAVASCRIPT" CODEPAGE="1252"%&gt;
&lt;!--#include file="../../Connections/DBConnection.asp" --&gt;

&lt;%
var rsSTUDENTS = Server.CreateObject("ADODB.Recordset";
rsSTUDENTS.ActiveConnection = MM_DBConnection_STRING;
rsSTUDENTS.Source = "SELECT ID, NAME, CLASSNAME FROM STUDENTS ORDER BY ID";
rsSTUDENTS.CursorType = 0;
rsSTUDENTS.CursorLocation = 2;
rsSTUDENTS.LockType = 1;
rsSTUDENTS.Open();
var rsSTUDENTS_numRows = 0;
%&gt;
&lt;%
var Repeat1__numRows = -1;
var Repeat1__index = 0;
rsSTUDENTS_numRows += Repeat1__numRows;
%&gt;

&lt;%
// The insertion code
if (String(Request("MM_insert") == "form1"
{
var i;
var totalStudents=Request.Form("totalStudents"; // How namy students we have on the page
for(i=0;i&lt;totalStudents;i++)
{
// We have to split the ID because if we have 5 students for example, we will
// get the IDs as 1,2,3,4,5 and the separator is the comma.
intID = "" + Request("ID" + ""
myID=intID.split(",";

// We do the same for the STUDENT NAMES as we did for the ID
charNAME = "" + Request("NAME" + ""
myNAME=charNAME.split(",";

// We do the same for the CLASS NAMES as we did for the ID
charCLASSNAME = "" + Request("CLASSNAME" + ""
myCLASSNAME=charCLASSNAME.split(",";

// We do the same for the ATTENDANCE as we did for the ID
charATTENDANCE = "" + Request("ATTENDANCE" + ""
myATTENDANCE=charATTENDANCE.split(",";

// Do the insert
var Command1 = Server.CreateObject("ADODB.Command";
Command1.ActiveConnection = MM_DBConnection_STRING;
Command1.CommandText = "INSERT INTO ATTENDANCE (ID, NAME,CLASSNAME,ATTENDANCE,TIMESTAMP) VALUES (" + myID[i] + ",'" + myNAME[i] +"','" + myCLASSNAME[i] +"','" + myATTENDANCE[i] +"' , SYSDATE)";
Command1.CommandType = 1;
Command1.CommandTimeout = 0;
Command1.Prepared = true;
Command1.Execute();
}

// Once the loop is over, we direct to the attendance page
var MM_editRedirectUrl = "attendance.asp";
if (MM_editRedirectUrl) {Response.Redirect(MM_editRedirectUrl);}

}
%&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"&gt;
&lt;title&gt;Untitled Document&lt;/title&gt;

&lt;/head&gt;

&lt;body&gt;
&lt;form name="form1" method="post" action=""&gt;
&lt;table border="1"&gt;
&lt;tr bgcolor="#CCCCCC"&gt;
&lt;td&gt;&lt;div align="center"&gt;Student ID&lt;/div&gt;&lt;/td&gt;
&lt;td&gt;&lt;div align="center"&gt;Student Name &lt;/div&gt;&lt;/td&gt;
&lt;td&gt;&lt;div align="center"&gt;Class Name &lt;/div&gt;&lt;/td&gt;
&lt;td&gt;&lt;div align="center"&gt;Attendance&lt;/div&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;% while ((Repeat1__numRows-- != 0) && (!rsSTUDENTS.EOF)) { %&gt;
&lt;tr&gt;
&lt;td&gt;&lt;%=(rsSTUDENTS.Fields.Item("ID".Value)%&gt;
&lt;input name="ID" type="hidden" id="ID" value="&lt;%=(rsSTUDENTS.Fields.Item("ID".Value)%&gt;"&gt; &lt;/td&gt;
&lt;td&gt;&lt;%=(rsSTUDENTS.Fields.Item("NAME".Value)%&gt;
&lt;input name="NAME" type="hidden" id="NAME" value="&lt;%=(rsSTUDENTS.Fields.Item("NAME".Value)%&gt;"&gt;&lt;/td&gt;
&lt;td&gt;&lt;%=(rsSTUDENTS.Fields.Item("CLASSNAME".Value)%&gt;
&lt;input name="CLASSNAME" type="hidden" id="CLASSNAME" value="&lt;%=(rsSTUDENTS.Fields.Item("CLASSNAME".Value)%&gt;"&gt;&lt;/td&gt;
&lt;td&gt;&lt;input name="ATTENDANCE" type="text" id="ATTENDANCE"&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;%
Repeat1__index++;
rsSTUDENTS.MoveNext();
}
%&gt;
&lt;/table&gt;
&lt;p&gt;&lt;input type="hidden" name="totalStudents" value="form1"&gt;
&lt;!-- A small script to tell us how many rows we have in the result table because our
insertion loop depends on that number. --&gt;
&lt;SCRIPT LANGUAGE="JavaScript1.2"&gt;document.form1.totalStudents.value=document.form1.ATTENDANCE.length&lt;/script&gt;
&lt;input type="hidden" name="MM_insert" value="form1"&gt;
&lt;input type="submit" name="Submit" value="Submit"&gt;

&lt;/p&gt;
&lt;/form&gt;

&lt;br&gt;
&lt;/body&gt;
&lt;/html&gt;
&lt;%
rsSTUDENTS.Close();
%&gt;

</font id=code></pre id=code>

<b> Attendance Page "attendance.asp" </b>

<pre id=code><font face=courier size=2 id=code>

&lt;%@LANGUAGE="JAVASCRIPT" CODEPAGE="1252"%&gt;
&lt;!--#include file="../../Connections/DBConnection.asp" --&gt;
&lt;%
var rsAttendance = Server.CreateObject("ADODB.Recordset";
rsAttendance.ActiveConnection = MM_DBConnection_STRING;
rsAttendance.Source = "SELECT ID, NAME, CLASSNAME, ATTENDANCE,TIMESTAMP FROM ATTENDANCE ORDER BY ID";
rsAttendance.CursorType = 0;
rsAttendance.CursorLocation = 2;
rsAttendance.LockType = 1;
rsAttendance.Open();
var rsAttendance_numRows = 0;
%&gt;
&lt;%
var Repeat1__numRows = -1;
var Repeat1__index = 0;
rsAttendance_numRows += Repeat1__numRows;
%&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"&gt;
&lt;title&gt;Untitled Document&lt;/title&gt;
&lt;/head&gt;

&lt;body&gt;
&lt;table border="1"&gt;
&lt;tr&gt;
&lt;td&gt;&lt;div align="center"&gt;ID&lt;/div&gt;&lt;/td&gt;
&lt;td&gt;&lt;div align="center"&gt;NAME&lt;/div&gt;&lt;/td&gt;
&lt;td&gt;&lt;div align="center"&gt;CLASSNAME&lt;/div&gt;&lt;/td&gt;
&lt;td&gt;&lt;div align="center"&gt;ATTENDANCE&lt;/div&gt;&lt;/td&gt;
&lt;td&gt;&lt;div align="center"&gt;DATE&lt;/div&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;% while ((Repeat1__numRows-- != 0) && (!rsAttendance.EOF)) { %&gt;
&lt;tr&gt;
&lt;td&gt;&lt;%=(rsAttendance.Fields.Item("ID".Value)%&gt;&lt;/td&gt;
&lt;td&gt;&lt;%=(rsAttendance.Fields.Item("NAME".Value)%&gt;&lt;/td&gt;
&lt;td&gt;&lt;%=(rsAttendance.Fields.Item("CLASSNAME".Value)%&gt;&lt;/td&gt;
&lt;td&gt;&lt;%=(rsAttendance.Fields.Item("ATTENDANCE".Value)%&gt;&lt;/td&gt;
&lt;td&gt;&lt;%=(rsAttendance.Fields.Item("TIMESTAMP".Value)%&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;%
Repeat1__index++;
rsAttendance.MoveNext();
}
%&gt;
&lt;/table&gt;
&lt;p&gt;&lt;a href="students.asp"&gt;Add More&lt;/a&gt; &lt;/p&gt;
&lt;/body&gt;
&lt;/html&gt;
&lt;%
rsAttendance.Close();
%&gt;

</font id=code></pre id=code>

I hope that will help.

Reply to this topic