datafill.asp
<%@ language="JScript" %>
<%
// On submit, open the database
if(Request.QueryString("Submit")!=""){
conn = Server.CreateObject("ADODB.Connection");
conn.Open("sodanoorg00_vote04");
// Create an array to hold all codes already in the database and any new ones created.
// It will be used to prevent the creation of duplicates.
var currCodes = new Array();
// Fill the array with the codes already in the database.
codesInDB = conn.Execute("select StudentID from ELStudents");
var i = 0;
while(!codesInDB.EOF){
var helper = codesInDB("StudentID");
currCodes[i] = helper;
i++
codesInDB.MoveNext
}
// Create an array that contains all acceptable characters for use in the codes.
// For legibility, this version omits such confusable characters as "O","0","I", etc.
var chars = new Array("2","3","4","5","6","7","8","9","A","B","C","D","E","F","G","H","K","L","M","N","P","Q","R","S","T","X","Y","Z","Z");
// The main loop produces one code per cycle, determined by the "Number of Codes (NOCO)" selection box.
for(x=0;x<parseInt(Request.QueryString("NOCO"));x++){
do{
var code = "";
var flag = "clear";
// The second loop cycles once per character in the code, determined by the "Number of Characters (NOCH)" selection box.
// It selects a random character from the "chars" array and appends it to the "code" variable.
for(y=0;y<parseInt(Request.QueryString("NOCH"));y++){
var rand = Math.round(Math.random() * (chars.length-1));
code += chars[rand];
}
// The code is added to the universal array, but it will only stay survive if it is not found in the rest of the array.
// If it is found to be a duplicate, the do...while loop will repeat, and the index of the currCodes array will not advance.
currCodes[i] = code;
var z;
for(z=0;z<(currCodes.length-1);z++){
var temp = currCodes[z] + "";
if(temp==code){ flag = "error"; break; }
}
}
while(flag=="error")
// The code is inserted into the database.
conn.Execute("insert into ELStudents (StudentID) values ('" + currCodes[i] + "')");
i++;
}
}
%>
<html>
<head>
</head>
<body>
<form name="_form" action="datafill.asp" method="get">
Number of Codes:
<select name="NOCO">
<option value="def">#</option>
<option value="1">1</option>
<option value="10">10</option>
<option value="50">50</option>
<option value="100">100</option>
<option value="200">200</option>
<option value="300">300</option>
</select>
<br/><br/>
Number of Characters:
<select name="NOCH">
<option value="def">#</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
</select> || A-9
<br/><br/>
<input type="submit" value="Write" name="Submit" />
</form>
</body>
</html>