Docstoc

ASPLecture Transparencies

Document Sample
ASPLecture Transparencies Powered By Docstoc
					     Michael Hershberg
          Programmer
    Quicksilver Software, Inc.

Email: mhershbe@quicksilver.com
VBScript Examples:

   Function SquareNum(num)
      SquareNum = num * num
   End Function

   Sub Foo
      Dim x, x_2

      x = 5
      x_2 = SquareNum(x)

      Response.Write "x = " & x & vbNewLine
      Response.Write "x^2 = " & x_2
   End Sub


   Sub ObjectDemo
      Dim objDict, i, numDays
      Set objDict = CreateObject("Scripting.Dictionary")

      ' Add months and the number of days in that month
      For i = 1 To 12
         If i Mod 2 = 0 Then
            If i = 2 Then
                numDays = 28
            Else
                numDays = 30
            End If
         Else
            numDays = 31
         End If

         objDict.Add MonthName(i), numDays
      Next

      ' Now let's test it out
      Response.Write "January has "
      Response.Write objDict("January") & " days" & vbNewLine
      Response.Write "June only has "
      Response.Write objDict("June") & " days"

      ' Some unnecessary clean up
      Set objDict = Nothing
   End Sub
ASP Examples:

Source Code

    <% @Language = VBScript %>
    <% Option Explicit %>
    <html>
    <head>
      <title>Simple ASP Example</title>
    </head>

    <body bgcolor="#FFFFFF">

    <p>Let's count to 10 in ASP!</p>

    <p>
    <%
          Dim i
          For i = 1 To 10
             Response.Write "<b>" & i & "</b> " & vbNewLine
          Next
    %>
    </p>

    <p>Now let's count to 5!</p>
    <% For i = 1 To 5 %>

    <p><b><%=i%></b></p>

    <% Next %>
    </body>
    </html>

Page

Let's count to 10 in ASP!
1 2 3 4 5 6 7 8 9 10
Now let's count to 5!
1
2
3
4
5
More ASP Examples

Forms

(form.htm)

   <html>
   <head>
     <title>Please say Hello</title>
   </head>
   <body bgcolor="#FFFFFF">

   <form action="sayhello.asp" method="POST">
   <p><b>Please enter your name:</b>
   <input type="text" name="YourName" id="YourName"></p>
   <p><input type="submit" value="Say Hello!"></p>
   </form>

   </body>
   </html>

(sayhello.asp)

   <% @Language = VBScript %>
   <% Option Explicit %>
   <%
       Dim YourName
       YourName = Request.Form("YourName")
   %>
   <html>
   <head>
      <title>Hello <%=YourName%>!</title>
   </head>
   <body bgcolor="FFFFFF">

   <h2>Hello <%=YourName%>!</h2>

   <p>Nice to meet you!</p>

   </body>
   </html>
Add A Student

(add_student.asp)
   <% @Language = VBScript %>
   <% Option Explicit %>
   <!--#include virtual="db.inc" -->

   <html>
   <head>
     <title>Added Student</title>
   </head>

   <body bgcolor="#FFFFFF">

   <%
        ' Read the data from the form
        Dim FirstName, LastName
        FirstName = Request.Form("FirstName")
        LastName = Request.Form("LastName")

        ' Connect to the database
        Dim conn, query
        Set conn = OpenAccessDB("school.mdb")

        ' Set   up our query
        query   = "INSERT INTO Students (FirstName, LastName) VALUES("
        query   = query & "'" & FirstName & "',"
        query   = query & "'" & LastName & "')"

        ' Execute our query
        conn.Execute query

        conn.Close
   %>

   <h2>Student added to database successfully!</h2>

   </body>
   </html>

Adding a professor is similar except for the table name.
Add A Course

(add_course.asp)
   <% @Language = VBScript %>
   <% Option Explicit %>
   <!--#include virtual="db.inc" -->

   <html>
   <head>
     <title>Added Course</title>
   </head>

   <body bgcolor="#FFFFFF">

   <%
        ' Read the data from the form
        Dim Title, ProfID
        Title = Request.Form("Title")
        ProfID = Request.Form("ProfID")

        ' Connect to the database
        Dim conn, query
        Set conn = OpenAccessDB("school.mdb")

        ' Set   up our query
        query   = "INSERT INTO Courses (Course, Professor) VALUES("
        query   = query & "'" & Title & "',"
        query   = query & ProfID & ")"

        ' Execute our query
        conn.Execute query

        conn.Close
   %>

   <h2>Course added to database successfully!</h2>

   </body>
   </html>
Add A Student To A Course

(enroll_student.asp)
   <% @Language = VBScript %>
   <% Option Explicit %>
   <!--#include virtual="db.inc" -->

   <html>
   <head>
     <title>Student Enrolled in Course</title>
   </head>

   <body bgcolor="#FFFFFF">

   <%
        ' Read the data from the form
        Dim StudentID, CourseID
        Title = Request.Form("StudentID")
        ProfID = Request.Form("CourseID")

        ' Connect to the database
        Dim conn, query
        Set conn = OpenAccessDB("school.mdb")

        ' Set   up our query
        query   = "INSERT INTO Rosters (StudentID, CourseID) VALUES("
        query   = query & StudentID & ","
        query   = query & CourseID & ")"

        ' Execute our query
        conn.Execute query

        conn.Close
   %>

   <h2>Student enrolled in course successfully!</h2>

   </body>
   </html>
List All Students In A Course

(list_roster.asp)
    <% @Language = VBScript %>
    <% Option Explicit %>
    <%
       Response.Buffer = True

         ' Read the course name from the database
         Dim conn, query, rs, CourseName

         Set conn = Server.CreateObject("ADODB.Connection")
         conn.Open "DSN=MyDSN;Database=MyData;UID=sa;PWD=l33t"

         query = "SELECT Course FROM Courses WHERE CourseID = "
         query = query & Request.Form("CourseID")

         Set rs = conn.Execute(query)

         If rs.EOF Then
            Response.Redirect "roster_error.htm"
         End If

         CourseName = rs("Course")
         rs.Close
    %>

    <html>
    <head>
      <title>Roster For Course <%=CourseName%></title>
    </head>

    <body bgcolor="#FFFFFF">

    <h2>Roster For Course <%=CourseName%></h2>

    <%
         ' Read the roster
         query = "SELECT FirstName, LastName, StudentID FROM "
         query = query & "Students S, Rosters R WHERE "
         query = query & "S.StudentID = R.StudentID AND R.CourseID = "
         query = query & Request.Form("CourseID") & " ORDER BY LastName"

         Set rs = conn.Execute(query)
    %>

Continued on next page…
Continued from last page…

   <table border=0 cellspacing=5>
   <tr>
      <td><b>Student</b></td>
      <td><b>ID Number</b></td>
   </tr>
   <%
       While Not rs.EOF
   %>
   <tr>
      <td><%=rs("LastName")%>, <%=rs("FirstName")%></td>
      <td><%=rs("StudentID")%></td>
   </tr>
   <%
          rs.MoveNext    ' VERY IMPORTANT! DON'T FORGET THIS!
       Wend

      rs.Close
      conn.Close
   %>
   </table>

   </body>
   </html>
VBScript
Basic Syntax (no pun intended)

   •   Simple syntax (no semicolons, statements restricted to one line)
   •   Apostrophy ' used for comments
   •   Variables do not have to be declared before using them
   •   Subroutines and functions: subroutines are equivalent to functions that return void
       in C/C++
   •   Private, Public allow you to give variables, subroutines, and functions
       application- or page-scope.
   •   Case-insensitive

Language Features:

   •   Only one variable data type: the Variant
   •   Variants can be null or store any of the following types of data: integer, single
       (float), double, byte, Boolean, currency, date/time, or string
   •   Additionally, variants can store references to objects
           o When used as a reference to an object, you must assign using the Set
                keyword.
           o You can “disconnect” a reference from an object by assigning the variable
                to Nothing (using the Set keyword, of course!)
   •   Arrays can be resized using the ReDim statement

Things to Watch Out For

   •   Be sure you know what type of data you are working with
           o Helpful to use data-conversion functions such as CBool, CSng, CStr, CInt,
              etc. to explicitly force variants to the desired data type.
           o Use the & operator and not the + operator to concatenate strings.
   •   Parameters passed to a subroutine are not enclosed in parentheses
   •   Parameters passed to a function are required to be enclosed in parentheses
   •   Two kinds of division
           o Integer division operator \               5\2=2
           o Floating point division operator /        5 / 2 = 2.5
   •   Some subroutines and functions have default values for certain parameters. To use
       the default simply omit the corresponding argument. If the default is at the
       beginning or the middle of the parameter list just use commas without anything in
       between: rs.Open "Students", conn, , , adCmdTable
Helpful Hints

   •   Type Option Explicit on one line near the start of your scripts to force variable
       declarations. This helps you prevent bugs caused by mistyping variable names.
   •   IsNumeric function returns True if a string represents a valid number
   •   IsDate function returns True if a string represents a valid date
   •   Dictionary object allows you to create a hash table
          o Accessing the values is as easy as using an array (same notation)
          o http://msdn. microsoft.com/library/default.asp?url=/library/en-
               us/script56/html/jsobjDictionary.asp?frame=true for more info

Debugging

   •   Set up IIS to allow ASP sever-side debugging
   •   Download the Windows Script Debugger
           o Allows you to set breakpoints, step through code, and examine variables
           o Also allows you to debug client-side scripts from Internet Explorer
Sample Database




  •   This database keeps track of students, professors and their classes.
  •   It has 4 tables: Students, Professors, Courses, and Rosters
  •   Students stores information about students and uses a StudentID field as a unique
      id for each record in the table.
  •   Professors stores information about professors and uses a ProfessorID field as its
      unique id.
  •   Courses stores the name of the class and the id of the professor teaching it. This is
      a relationship between the Professors table and the Courses table.
  •   Rosters defines the relationship between the Students table and the Courses table.
      It stores both the id of the class and the id of the student taking the class.

				
About if any file u wil find copyright contact me it will be remove in 3 to 4 buisnees days. add me on sanjaydudeja007@gmail.com or visit http://www.ohotech.com/