0% found this document useful (0 votes)
16 views19 pages

Asp Programming Lab - 250412 - 232338

The ASP Programming Lab Manual provides a series of exercises aimed at teaching ASP programming with SQL, including displaying messages based on the day of the week, creating forms for user input, managing session timeouts, and manipulating databases. Each exercise includes a specific aim, procedure, and expected results, covering topics like cookie management, user authentication, and course registration. The manual serves as a practical guide for hands-on learning in web development using ASP and SQL.

Uploaded by

kirubaaveena
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
16 views19 pages

Asp Programming Lab - 250412 - 232338

The ASP Programming Lab Manual provides a series of exercises aimed at teaching ASP programming with SQL, including displaying messages based on the day of the week, creating forms for user input, managing session timeouts, and manipulating databases. Each exercise includes a specific aim, procedure, and expected results, covering topics like cookie management, user authentication, and course registration. The manual serves as a practical guide for hands-on learning in web development using ASP and SQL.

Uploaded by

kirubaaveena
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 19
ASP Programming Lab Manual with SQL. 1, Create an ASP file to display the message "Have a Good Weekend’ if it is a Saturday, otherwise "Hang in there, the week will get better’ Aim: To display a message based on the day of the week. Procedure: © Use the Weekday function in ASP to determine the current day. © Display a message accordingly using If..Else statements, Code: Result: The message displayed will vary depending on whether it is Saturday or not. 2. Write an ASP program to get the name and favorite ice cream flavor. Respond with the price of the corresponding ice cream. Ait To create a simple form and return the price based on the selected ice cream flavor. Procedure: © Create an HTML form to get user input. © Process the form in ASP to display the price of the selected ice cream flavor. Code: Result: The ASP script takes the name and favorite flavor from the form and displays the corresponding price. 3. Create a login form that expires if the user does not type the password within 100 seconds. Aim: To create a login form with a timer expiration using JavaScript and ASP Procedure: © Use a JavaScript setTimeout() function to check the 100-second limit. © Redirect the page or show an error message when the time is exceeded Code: Result: If the user doesn't submit the form within 100 seconds, the session will expire and redirect them to a timeout page. 4, Create an advertisement for a bookshop using Ad Rotator component Aim: To use the Ad Rotator component to display rotating advertisements on a webpage. Procedure: © Use the AdRotator ASP component to create rotating ads. © Configure an XML file for the AdRotator component. Code Result: The AdRotator will display rotating bookshop ads from the XML file 5. Create a course registration form with name, address, and list of available courses. Respond with the corresponding course fees on selection of a single course or a collection of courses. Aim: To create a registration form and respond with the course fees based on the selected courses. Procedure: © Create a form to get student details and courses selection. © Display the fee based on the courses selected Code: Result: The form collects student data and displays the total course fee based on the selected courses, 6. Write an ASP program to manipulate cookies with the information between HTTP sessions such as: Aim: To store and retrieve user session information using cookies. Procedure: © Set cookies for the last visit date and time, number of visits. © Retrieve and display this information. Code: Result: Displays the last visit date and time and the number of visits made by the user. 7. Create a student database and manipulate the records using the connection object in ASP Aim: To interact with a database to create and manipulate student records. Procedure: © _Use the ADODB.Connection object to connect to a database. © Use SQL queries to insert, update, and display student records. Code: Result: Displays a list of students from the database. 8, Create an employee database and manipulate the records using the command object in ASP Aim: To interact with a database and manipulate employee records using the Command object. Procedure: © Create and use the ADODB.Command object to insert, update, and delete records. Code: Result: This will insert a new employee into the employee database. This lab manual covers several core topics in ASP with SQL and provides hands-on exercises for working with form inputs, database interactions, and cookies, all commonly used in web development with ASP. <% Dim visitCount, lastVisitDate, lastVisitTime If Request. Cookies(‘visitCount’) <> " Then visitCount = Cint(Request.Cookies( visitCount’)) +1 lastVisitDate = Request. Cookies("lastVisitDate') lastVisitTime = Request Cookies(‘lastVisitTime’) Else visitCount = 1 lastVisitDate = "Never lastVisitTime = "Never" End If Dim currentDate, currentTime Response.Cookies(' Response.Cookies(" {tCount’) = visitCount itCount’) Expires = DateAdat’ 30, Now()) Response.Cookies('lastVisitDate’) = currentDate Response.Cookies("lastVisitDate’) Expires = DateAdd(‘d’, 30, Now()) Response.Cookies(‘lastVisitTime’) Response.Cookies(‘lastVisitTime’).Expires = DateAdd("d’, 30, Now()) Response. Write(

Welcome back!

") Response Write(

Last visit date: " & lastVisitDate &

") Response. Write("

Last visit time: " & lastVisitTime & "

") Response. Write('

Total number of visits: *& visitCount & "«/p>") If visitCount = 1 Then Response.Write('

Welcome to your first visit!

") End if > % " Create a connection to the database Dim conn, rs, sql, userName, userFlavor, iceCreamPrice "Initialize connection Set conn = Server.CreateObject(‘ADODB.Connection") conn.Open “Your_Connection_String” ' Replace with your actual database connection string * Check ifthe form is submitted If Request. ServerVariables(REQUEST_METHOD") "Get the values from the form userName = Request. Form(name") userFlavor = Request.Form(“flavor") POST" Then "Query to get the price of the selected flavor sql = "SELECT price FROM IceCreams WHERE flavor Set rs = conn.Execute(sql) &userFlavor &™ * Check if the flavor is found in the database If Not rs.EOF Then iceCreamPrice = rs(‘price") Response. Write("

Hello, " & userName & "I The price of *& userFlavor & ice cream is $" & iceCreamPrice & "

") Else Response. Write("

Sorry, we donit have ” & userFlavor &" ice cream available.

") End if "Close the recordset rs.Close Set rs = Nothing Else "Ifthe form hasn't been submitted, display the form Response.Write("

Ice Cream Flavor Pricing

") Response.Write("
") Response. Write("
") End if * Clean up database connection conn.Close Set conn = Nothing > 3. CREATE TABLE Users ( username VARCHAR(50) PRIMARY KEY, password VARCHAR(255) NOT NULL — Store hashed passwords for security i ~~ Example data: INSERT INTO Users (username, password) VALUES (john_doe’, hashed _password here), (jane_smith; hashed password here); <% "Initialize session timeout (optional, session will expire after a certain time of inactivity) Session.Timeout = 2 Session timeout in minutes (for extra security) " Check if the form is submitted If Request. ServerVariables("REQUEST_METHOD’) = "POST" Then Dim conn, rs, sql, username, password, dbPassword, loginTimestamp Set conn = Server.CreateObject(‘ADODB. Connection’) conn.Open "Your_Connection_String” "Replace with your actual connection string "Get the input from the form. usemame = Request Form("username") password = Request. Form("password") " Query to get the hashed password from the database sql = "SELECT password FROM Users WHERE username =" & username & ™ Set rs = conn.Execute(sql) "Validate user credentials If Not rs.E0F Then dbPassword = rs(‘password") " Check if passwords match (assuming plain text for simplicity, but hashing is recommended) If password = dbPassword Then " Store the current timestamp in the session to track login time Session(‘username’) = username Session(‘login_time’) = Now( ' Store the login time Response. Write("

Welcome, " & username & "

") Response. Write("

Login successful. You have 100 seconds to interact with this page.

") Else Response. Write("

Incorrect password. Please try again.

") End if Else Response. Write("

Usemame not found. Please register or try again.

") EndIf "Clean up rs.Close Set rs = Nothing conn.Close Set conn = Nothing Else * Check if the user is logged in and if the session has expired If Not IsEmpty(Session(‘username")) Then " Check the login time loginTimestamp = Ses \("login_time") " Calculate the difference in seconds between now and the login time If DateDiff('s’, loginTimestamp, Now()) > 100 Then * Expired session, redirect to login page Response.Redirect(‘login.asp) End if End if “Display the login form ifthe user is not logged in or ifthe session expired If IsEmpty(Session('username')) Then Response. Write("

Login

") Response.Write("
*) jext’ name='username' required>
") ‘password’ name=/password' required>
") Login") 4 % " Hardcoded courses and corresponding fees Dim courses, courseFees courses = Array("Web Development’, ‘Data Science’, "Machine Learning’, "Digital Marketing’) courseFees = Array(200.00, 300.00, 350.00, 150.00) "Initialize variables Dim name, address, selectedCourses, totalFees totalFees = 0 " Check if the form has been submitted If Request ServerVariables(‘REQUEST_METHOD’) = "POST’ Then "Get form inputs name = Request.Form("name") address = Request Form(‘address") selectedCourses = Request Form(‘courses') Array of selected courses "Initialize an empty string to store selected course names Dim selectedCourseslist selectedCoursesList =" * Loop through selected courses and calculate total fees If Not IsArray(selectedCourses) Then selectedCourses = Array(selectedCourses) ‘If only one course is selected End if For Each courseld In selectedCourses " Add the course name and fee to the total fees totalFees = totalFees + courseFees(courseld- 1)' Subtract 1 for array indexing selectedCoursesList = selectedCoursesList & courses(courseld - 1) & "
" Next "Display the result Response Write("

Course Registration Successfull

") Response. Write("

Name: * & name & "

") Response.Write('

Address: " & address & *

") Response Write('

Selected Courses:
" & selectedCoursesList &

") Response.Write("

Total Fees: $" & totalFees & "

") Else "Display the registration form if it has not been submitted >

Course Registration

«label for="name">Full Name:
«


<% End If %> 5 <% "Initialize hardcoded course data (course name and fee) Dim courses, courseFees courses = Array("Web Development’, ‘Data Science’, "Machine Learnin courseFees = Array(200.00, 300.00, 350.00, 150.00) igital Marketing’) " Check if the form has been submitted If Request. ServerVariables(’REQUEST_METHOD’) = “POST” Then "Get form inputs Dim name, address, selectedCourses, totalFees name = Request.Form("name") address = Request Form(‘address") selectedCourses = Request.Form("courses") ' This is an array of selected courses totalFees = 0 "Initialize an empty string to store the selected courses Dim selectedCoursesList selectedCoursesList =" "Loop through selected courses and caloulate total fees If Not IsArray(selectedCourses) Then selectedCourses = Array(selectedCourses) If only one course is selected End if For Each courseld In selectedCourses " Add the course name and fee to the total fees. totalFees = totalFees + courseFees(courseld- 1) ' Subtract 1 for array indexit selectedCoursesList = selectedCoursesList & courses(courseld-- 1) & “
” Next "Display the result ‘

Course Registration Successfull

") '

Name: " & name & "

") ‘

Address: " & address & "

") '

Selected Courses:
" & selectedCoursesList &

") '

Total Fees: $" & totalFees & "

") Else "Display the form if not submitted ood
“name'>Full Name:
«

«label for="address’>Address:
«textarea name="address" id="address’ required>

«label for="courses’»Select Courses:


<% End If %> 6 <% Dim visitCount, lastVisitDate, lastVisitTime If Request.Cookies("visitCount") <>" Then visitCount = Cint(Request.Cookies(“visitCount’)) + 1 lastVisitDate = Request.Cookies("lastVisitDate") lastVisitTime = Request.Cookies(‘lastVisitTime") Else visitCount = 1 lastVisitDate = "Never" lastVisitTime = "Never" End if Dim currentDate, currentTime currentDate = Date() currentTime = Time() Response.Cookies('visitCount’) = visitCount Response.Cookies('visitCount’).Expires = DateAdd('d", 30, Now() Response.Cookies("lastVisitDate") = currentDate Response.Cookies("lastVisitDate").Expires = DateAdd("d", 30, Now()) Response.Cookies(‘lastVisitTime") = currentTime Response.Cookies("lastVisitTime").Expires = DateAdd("d", 30, Now()) Response.Write("

Welcome back!

") Response .Write("

Last visit date: " & lastVisitDate & "

") Response .Write("

Last visit time: " & lastVisitTime & "

") Response.Write("

Total number of visits: " & visitCount & "

") If visitCount = 1 Then Response.Write("

Welcome to your first visit!

") End If > 7 <% "Initialize an array to store student data (simulating a database) Dim students) ReDim students(1)' Initial size (2D array with ID, FirstName, LastName, Age, Gender) " Action handling (Add, View, Update, Delete) Dim action, studentlD, firstName, lastName, age, gender actior Request. QueryString(“action’) " Add anew student If action = ‘add’ Then firstName = Request Form(‘firstName") lastName = Request Form(lastName’) age = Request Form('age") gender = Request. Form("gender’) "Resize array to accommodate new student ReDim Preserve students(UBound (students) + 1) "Add student data (simulating a database record) students(UBound(students)) = Array(UBound(students), firstName, lastName, age, gendet) Response.Write("

Student added successfully!

") * Update an existing student Elself action = “update” Then studentiD = Cint(Request.QueryString(‘studentiD’)) firstName = Request Form(‘firstName") lastName = Request Form("lastName") age = Request.Form(‘age") gender = Request.Form("gender’) "Update the student in the array (by studentiD) If studentiD >= 0 And studentiD <= UBound(students) Then students(studentID) = Array(studentiD, firstName, lastName, age, gender) Response. Write("

Student record updated successfully!

") Else Response. Write("

invalid student ID!

") EndIf "Delete a student Elself action = “delete” Then studentiD = Cint(Request.QueryString(‘studentiD’)) "Delete the student by ID If studentiD >= 0 And studentiD <= UBound(students) Then " Shift students to remove the entry For i = studentID To UBound(students) - 1 students(i) = students(i+ 1) Next ReDim Preserve students(UBound(students) - 1) Response. Write("

Student deleted successfully!

") Else Response. Write("

invalid student ID!

") EndIf "View all students Else Response Write('

Student Records

") Response.Write("") Response.Write("") Response.Write("") Response. Write("std>" & students()(2) & "") Response.Write("") Response Write("") Response. Write("") Response. Write("s/tr>") Next Response Write("
IDFirst NameLast NameAgeGenderActions" & students(i)(0) & "" & students()(1) & "" & students(i)(3) & "" & students(i)(4) & "Update |" &_ "Delete
") End if "HTML Form for adding or updating a student record If action = 'add” Or action = "update’ Then Dim studentToEdit If action = "update’ Then studentiD = Cint(Request. QueryString('studentiD")) If studentiD >= 0 And studentiD <= UBound(students) Then studentToEdit = students(studentiD) End if End if %

<%=IIf(action = “add’, “Add New Student’, “Update Student”) %>

«8studentID=<%= IIf(action = update’, studentID, ") %>">
" required>

Age:
" required>

" required>
If(action = “update”, studentToEdit(4), ") %>" required>
"Add Student’, “Update Student) %>"> 8 <% "Initialize an array to simulate an employee database (array of arrays) Dim employees() ReDim employees(1) Initial size, this will store EmployeeID, Name, Age, Department, Salary " Variables for action and data handling Dim action, employeelD, name, age, department, salary action = Request.QueryString(‘action’) "Add a new employee record If action = "add Then name = Request. Form(‘name') age = Request.Form("age") department = Request.Form(‘department’) salary = Request.Form(salary') "Resize the array to add a new employee ReDim Preserve employees(UBound(employees) + 1) " Add the new employee data (simulating the insert into a database) employees(UBound(employees)) = Array(UBound(employees), name, age, department, salary) Response.Write("

Employee added successfully!

") " Update an existing employee record Elsetf action = “update” Then employeelD = Cint(Request.QueryString("employeeID")) name = Request.Form(‘name") age = Request. Form('age") department = Request.Form(‘department’) salary = Request Form(‘salary’) " Check if employeeID is valid If employeeID >= 0 And employeelD <= UBound(employees) Then " Update the employee record in the array employees(employeetD) = Array(employeelD, name, age, department, salary) Response. Write("

Employee record updated successfully!

") Else Response. Write("

invalid Employee IDI

") EndIf "Delete an employee record Elself action = “delete” Then employeelD = Cint(Request. QueryString(‘employeeID")) "Check if employeelD is valid If employeelD >= 0 And employeelD <= UBound(employees) Then " Shift records to delete the employee entry For i = employeelD To UBound(employees) -1 employees(i) = employees(i + 1) Next ReDim Preserve employees(UBound(employees) - 1) Response. Write("

Employee record deleted successfully!

") Else Response. Write("

invalid Employee IDI

") End if " View all employees Else Response.Write(

Employee Records

") Response. Write("") * Loop through the array of employees and display the records For i= 0 To UBound(employees) Response.Write("") Response. Write("") Response. Write("") Response. Write("") Response.Write("") Response. Write("") Response. Write("") Response. Write(" he) Next Response Write("
IDNameAges/th>Departmenté/th>SalaryActions
" & employees(i)(0) & "" & employees(i)(1) & "" & employees(i)(2) & " & employees(i)(3) & " & employees(i)(4) & "Update |" &_ "ea hi * & employees(i)(0) & ">Delete
") End if "HTML form for adding or updating an employee record If action = ‘add’ Or action = “update’ Then Dim employeeToEdit If action = "update" Then employeelD = Cint(Request.QueryString("employeel0")) If employeeID >= 0 And employeeID <= UBound(employees) Then employeeToEdit = employees(employee!D) End If End if % ‘

<%= IIf(action = "add", “Add New Employee’, "Update Employee") %>

«">
" required>

" required>

" required>
f(action = "update", employee ToEdit(4),") %>" required>
"Add Employee", "Update Employee’) %>">
<% End If %> % Dim conn, rs, sql Set conn = Server.CreateObject(‘ADODB.Conn conn.Open “Provider=SQLOLEDB; Data Source=YOUR SERVER_NAME; I Catalog YOUR DATABASE_NAME; User ID=YOUR_USER; Password=YOUR_PASSWORD;” ‘sql = "SELECT DATENAME(weekday, GETDATE()) AS DayName” Set rs = conn.Execute(sq)) Dim today today=rs("DayName") If today "Saturday" Then Response.Write "

Have a Good Weekend«/h2>" Else Response.Write “

Hang in there, the week will get better

" End If rs.Close Set rs = Nothing conn.Close Set conn = Nothing % <% Dim visitCount, lastVisitDate, lastVisitTime If Request. Cookies("visitCount’) <>" Then visitCount = Cint(Request.Cookies('visitCount’)) + 1 lastVisitDate = Request. Cookies("lastVisitDate') lastVisitTime = Request Cookies(‘lastVisitTime’) Else visitCount = 1 lastVisitDate = "Never lastVisitTime = "Never" End if Dim currentDate, currentTime Response.Cookies("visitCount’) = visitCount Response.Cookies('visitCount’).Expires = DateAdd(’ Response.Cookies(’ Response.Cookies("lastVisitDate’) Expires = DateAdd(‘d’, 30, Now()) Response Cookies(‘lastVisitTim Response.Cookies(‘lastVisitTime’).Expires = DateAdd("d’, 30, Now()) Response. Write(

Welcome back!

") Response Write(

Last visit date: " & lastVisitDate &

") Response. Write(

Last visit time: "& lastVisitTime & "Total number of visits: *& visitCount & "«/p>") If visitCount = 1 Then Response.Write('

Welcome to your first visit!

") End if >

You might also like