+ All Categories
Home > Documents > ASP Code.doc

ASP Code.doc

Date post: 02-Oct-2015
Category:
Upload: kamesh
View: 21 times
Download: 1 times
Share this document with a friend
Description:
ggg
Popular Tags:
28
<html> <body> <% set conn=Server.CreateObject("ADODB.Connection") conn.Provider="Microsoft.Jet.OLEDB.4.0" conn.Open(Server.Mappath("/db/northwind.mdb")) set rs=Server.CreateObject("ADODB.recordset") sql="SELECT DISTINCT Country FROM Customers ORDER BY Country" rs.Open sql,conn country=request.form("country") %> <form method="post"> Choose Country <select name="country"> <% do until rs.EOF response.write("<option") if rs.fields("country")=country then response.write(" selected") end if response.write(">") response.write(rs.fields("Country")) rs.MoveNext loop rs.Close set rs=Nothing %> </select> <input type="submit" value="Show customers"> </form> <% if country<>"" then sql="SELECT Companyname,Contactname,Country FROM Customers WHERE country='" & country & "'" set rs=Server.CreateObject("ADODB.Recordset") rs.Open sql,conn %> <table width="100%" cellspacing="0" cellpadding="2" border="1"> <tr> <th>Companyname</th> <th>Contactname</th> <th>Country</th> </tr>
Transcript

Choose Country

Companyname Contactname Country

Untitled Document

Classname

Credits:

Grade:

Qualitypoints:

Semester:

A

B

C

D

F

Semester 1

Semester 2

A

B

C

D

F

Semester 1

Semester 2

A

B

C

D

F

Semester 1

Semester 2

jquery.datePicker example: linked datePickers with start and end dates

< date picker homeThe following example displays how you can synchronise two date pickers together. A common scenario for an online date picker is when booking flights or something where you need to input outbound and inbound dates. The start date must be before the end date and vice versa. We can enforce this rule with a very simple little bit of script.

Top of Form

Test date picker form

1. Start date:

HYPERLINK "http://www.kelvinluck.com/assets/jquery/datePicker/v2/demo/datePickerStartEnd.html" \o "Choose date" Choose date

2. End date:

HYPERLINK "http://www.kelvinluck.com/assets/jquery/datePicker/v2/demo/datePickerStartEnd.html" \o "Choose date" Choose date

3. Test select: Bottom of Form

Page sourcecode

$(function()

{

$('.date-pick').datePicker()

$('#start-date').bind(

'dpClosed',

function(e, selectedDates)

{

var d = selectedDates[0];

if (d) {

d = new Date(d);

$('#end-date').dpSetStartDate(d.addDays(1).asString());

}

}

);

$('#end-date').bind(

'dpClosed',

function(e, selectedDates)

{

var d = selectedDates[0];

if (d) {

d = new Date(d);

$('#start-date').dpSetEndDate(d.addDays(-1).asString());

}

}

);

});

Page CSS

/* located in demo.css and creates a little calendar icon

* instead of a text link for "Choose date"

*/

a.dp-choose-date {

float: left;

width: 16px;

height: 16px;

padding: 0;

margin: 5px 3px 0;

display: block;

text-indent: -2000px;

overflow: hidden;

background: url(calendar.png) no-repeat;

}

a.dp-choose-date.dp-disabled {

background-position: 0 -20px;

cursor: default;

}

/* makes the input field shorter once the date picker code

* has run (to allow space for the calendar icon

*/

input.dp-applied {

width: 140px;

float: left;

Adding records to the database with ASPby Faisal Khan.

OverviewNo matter what kind of site you have got, you will need your ASP pages to access databases. ASP makes it easier to work with databases by providing support for ADO ( Activex Data Objects ). You already know how to build DSN ( Data Source Name ) and then access database, if you don't then you might want to check out my tutorial on Accessing Databases via ASP.In this article I will build on that tutorial to show you how to add records to the database. We will begin by exploring the insert statement provided by SQL ( Structured Query Language ). After that we will see the two basic ways we can use to add records to the database.

I will assume here that you have read the Accessing Databases via ASP tutorial and are comfortable creating DSNs and simple Access Databases. I will also assume that you have got access to Microsoft Access Database, PWS / IIS with a notepad to write ASP pages.

SQL Insert StatementSQL is the standard language to deal with databases World wide. It provides us with select, insert, delete and update statements to show, add, delete and update the records in the database respectively. We will only study the insert statement since we are only dealing with adding records to the database here. It will be very helpful to us when we are adding records, as we'll see later.

Syntaxinsert into table_name (field1, field2, field3)

values ('value1', 'value2', 'value3';

The insert statement as you have seen above is very simple to understand. It takes three arguments; table, fields and values. table_name is the name of the table in the database into which you want to add records. fieldn are the names of the columns in that table into which you want to add records. valuen are the values which will be inserted into specific fields. Note field names and values can be one or more than one but the table name will always be one.

Exampleinsert into books (author, title) values

('Faisal Khan', 'Add Records';

When run the above query results in the insertion into two fields of table books, author and title values Faisal Khan and Add Records.

Table Name = books

Fields

Values

author

Faisal Khan

title

Add Records

Now after you are familiar with the insert statement and have seen how it works, it is time to move forward to see the two ways by which we can easily add records to our database via ASP.We manipulate databases in ASP through ADO ( Activex Data Objects ). ADO is a set of pre made data components which makes things a lot easier for us when it comes to accessing data stores. Wondering why did I say data stores and not databases ? well, database is only one of the data stores and ADO can help us access more than that e.g. XML. We'll not go into what ADO can do for us, instead we'll restrain ourselves to the discussion of adding records to database via ASP.

There are two ways to add records to the database. We'll discuss each of them now.

Via Connection ObjectIt is the easiest and fastest way to add records to the database. Here is how we add records with Connection Object :

The above results in the creation of Connection Object which opens the database and inserts the records into specific fields of the table according to the SQL insert statement. See, didn't I say before it was going to be easy.

Via Recordset ObjectRecordset is another very useful Object which allows us to select, add, update and delete records without using SQL statements. Here is how we add records with Recordset Object.

This code will execute as soon as the page loads, which can be useful on occasion, but we usually only want to perfom record modifications as the result of a form being submitted. To do this simply put the command within a condition, for example:

You can also set up multiple commands on a single page in order to insert, update or delete records in different tables in one instance, for example:

You can also use the commands in conjunction with the retrieve inserted record identity code in order to insert the new identity into a second table, which is useful if you need to maintain a relationship between two or more tables, for example:

Updating Multiple Records using SQL Update and the Where/In Clause with Checkboxes

There may be instances where one wants to update a field value for a large, select group of records. Doing this one record at a time is a killer on productivity. However, using checkboxes and a drop-down list, one can select a large group of records and then define a DMX Command to update the records in one fell swoop. This builds upon an article written by Marcellino Bommezijn concerning the deletion of multiple records and uses the SQL WHERE IN clause that he used in that tutorial.

Assumptions:

1. The person using this tutorial is familiar with databases and connection strings/DSNs. These will not be covered in this tutorial. I assume that one can setup or already has setup these conventions to follow the tutorial.

2. This tutorial references Microsoft Access as the database of choice for this tutorial, however, this example can be done in MySQL or SQL databases. I assume one has the knowledge to setup the databases as necessary for whatever server application is being used.

3. The person using this tutorial is familiar with Dreamweaver.

Step 1: Building the Form

In the following example, I will assign students to classroom groups. I will use checkboxes to assign students to a selected classroom group. I am using a simple Microsoft Access database designed like the following:

Setup a table named students with the following field names, types:

student_id, Autonumber - Primary Key

student_name, text

assigned_group, text

Start by saving a new ASP file as frm_assign_group_students.asp. Create a recordset named rs_students that pulls all records from the student table.

SELECT *FROM students

On our form, we will use a list menu to choose which group we are assigning. In this example, we simply add values to the list, though this could also be populated by another recordset using Dyanmic assignments in DMX. We will also setup a repeating region for all records in rs_students. The method of submission will be GET.

The list menu is a simple select tag that looks like this:

MathematiciansWritersScientists

The checkbox on the form will be named "update_group" and we will assign the student_id as a unique value from the recordset. This will look like the following in the HTML code:

The following shows the attributes: Save this file.Step 2: Designing the Update Process

To create the update page, we are going to setup a SQL command using the Command option under the Server Behaviors. Save this file as proc_student_group_update.asp before going any further.

With the Command dialogue box open, we will define the following.

1. The name of the Command will be update_group_assignment

2. From the Connection list, either define your connection or select a pre-defined connection name, in my case cnnUtility.

3. Select the Type Update

4. The table we are updating is students

5. We are setting the field named assigned_group the value of group_option from the form. Since we are using a GET method, we assign the run-time value for variable param_group by using Request.QueryString("group_option"). Since this is a string of text, we need to put param_group variable inside single quotes for the SQL statement.

6. We are updating all records that have been checked in update_group from the form. Because we used the same name on the form for the checkbox, the values will be stored as a comma-delimited list. We assign the run-time value for variable param_students by using Request.QueryString("update_group"). Since we are using IN as part of the WHERE clause, the SQL Update will make changes to all records with a matching student_id value in the recordset. As the student_id values are integers, they do not need to be placed in single quotes.

7. Finally, a little clean up is needed. Just below the section where DMX writes the COMMAND code, add the following to ensure the release of the update_group_assignment command variable:

Step 3: Finishing Up

1. In the Head section place a Refresh tag that redirects to the first form, like so:

2. In the of the HTML, type a message like "Updating Students..."

3. Save this file.

4. We are finished.

Updating Multiple Records using SQL Update and the Where/In Clause with Checkboxes

< !-- Include Files for the pre-header excluding JavaScript and CSS -->< !--#include file="../../Connections/cnn_Utility.asp" -->< !-- Macromedia VB Script and ASP not in the body -->< %

if(Request.QueryString("group_option") "") then update_group_assignment__param_group = Request.QueryString("group_option")

if(Request.QueryString("update_group") "") then update_group_assignment__param_students = Request.QueryString("update_group")

%>< %

set update_group_assignment = Server.CreateObject("ADODB.Command")update_group_assignment.ActiveConnection = MM_cnn_Utility_STRINGupdate_group_assignment.CommandText = "UPDATE students SET assigned_group = '" + Replace(update_group_assignment__param_group, "'", "''") + "' WHERE student_id IN (" + Replace(update_group_assignment__param_students, "'", "''") + ") "update_group_assignment.CommandType = 1update_group_assignment.CommandTimeout = 0update_group_assignment.Prepared = trueupdate_group_assignment.Execute()%>< !-- Nexsys Design VB Script and ASP not in the body -->< %SET update_group_assignment = nothing%>< html>< head>< title>Developed by Nexsys Design< !-- Meta Tags Section in the Head-->< meta name="author" content="[email protected]">< meta name="copyright" content="Copyright 1998 - 2003, Nexsys Design, Inc.">< meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">< meta http-equiv="refresh" content="5;URL=frm_assign_group_students.asp">

< !-- Nexsys Design JavaScript in the Head -->< !-- CSS Links or Style Definitions in the Head -->< script language="JavaScript" type="text/JavaScript">< !--function MM_displayStatusMsg(msgStr) { //v1.0status=msgStr;document.MM_returnValue = true;}//-->< /script>< link href="../../nexsysV2.css" rel="stylesheet" type="text/css">< /head>

< div align="center">

Updating students...

< /div>< /body>< /html>Using a Stored Procedure with a SQL Server database or creating your own custom INSERT command with an Access database it's fairly simple to retrieve the identity of the newly created record, something which is often useful with a record insert. For more information about retrieving record identities with Access and SQL Server see my site: http://www.drdev.net/1. Create your form, including a File Field to browse to the file you wish to upload.2. Now add the Pure ASP Upload server behaviour as you would normally, selecting the options you require. Don't specify a redirect URL, you can add that after your insert command code if required.3. Now create your insert command as you wish, but instead of using Request.Form("formElementName") to collect the values from the form elements, as you would usually, use CStr(UploadFormRequest("formElementName ")).For example, you might normally collect the form elements into variables like this:strFileName = Request.Form("fileUpload")strTitle = Request.Form("txtTitle")strDescription = Request.Form("txtDescription")

With the Pure ASP Upload behaviour, modify the collection of the values from the form as follows:strFileName = CStr(UploadFormRequest("fileUpload"))strTitle = CStr(UploadFormRequest("txtTitle"))strDescription = CStr(UploadFormRequest("txtDescription"))That's it, you can now use the values in the variables with your own insert command whether it uses a Stored Procedure or it is simply your preferred method of inserting a record in an Access or SQL Server database.

Here's an example insert command for an Access database which retrieves the identity of the newly created record and redirects the user to a new page. Notice I've used the Replace single quotes with two single quotes in this example to stop the INSERT command fowling up if they were included in the string values entered in the form:If CStr(UploadFormRequest("Submit")) = "Upload File" ThenstrFileName = Replace(CStr(UploadFormRequest("fileUpload")),"'", "''")strTitle = Replace(CStr(UploadFormRequest("txtTitle")) ,"'", "''")strDescription = Replace(CStr(UploadFormRequest("txtDescription")) ,"'", "''")Set commInsert = Server.CreateObject("ADODB.Connection")commInsert.Open MM_YourConn_STRINGcommInsert.Execute("INSERT INTO tblFiles(strFileName, strTitle, strDescription) VALUES('" & strFileName & "', '" & strTitle & "', '" & strDescription & "');") ' Execute the insert commandSet rsNewID = commInsert.Execute("SELECT @@IDENTITY") ' Create a recordset and SELECT the new IdentityintNewID = rsNewID(0) ' Store the value of the new identity in variable intNewIDrsNewID.CloseSet rsNewID = NothingcommInsert.CloseSet commInsert = Nothing Response.Redirect("FileDetail.asp?NewID=" & intNewID)End If_1337011876.unknownAttribute VB_Name = "ThisDocument"Attribute VB_Base = "1Normal.ThisDocument"Attribute VB_GlobalNameSpace = FalseAttribute VB_Creatable = FalseAttribute VB_PredeclaredId = TrueAttribute VB_Exposed = TrueAttribute VB_TemplateDerived = TrueAttribute VB_Customizable = TrueAttribute VB_Control = "DefaultOcxName2, 0, 0, MSForms, HTMLSelect"Attribute VB_Control = "DefaultOcxName1, 1, 1, MSForms, HTMLText"Attribute VB_Control = "DefaultOcxName, 2, 2, MSForms, HTMLText"

_1337011877.unknownAttribute VB_Name = "ThisDocument"Attribute VB_Base = "1Normal.ThisDocument"Attribute VB_GlobalNameSpace = FalseAttribute VB_Creatable = FalseAttribute VB_PredeclaredId = TrueAttribute VB_Exposed = TrueAttribute VB_TemplateDerived = TrueAttribute VB_Customizable = TrueAttribute VB_Control = "DefaultOcxName2, 0, 0, MSForms, HTMLSelect"Attribute VB_Control = "DefaultOcxName1, 1, 1, MSForms, HTMLText"Attribute VB_Control = "DefaultOcxName, 2, 2, MSForms, HTMLText"

_1337011875.unknownAttribute VB_Name = "ThisDocument"Attribute VB_Base = "1Normal.ThisDocument"Attribute VB_GlobalNameSpace = FalseAttribute VB_Creatable = FalseAttribute VB_PredeclaredId = TrueAttribute VB_Exposed = TrueAttribute VB_TemplateDerived = TrueAttribute VB_Customizable = TrueAttribute VB_Control = "DefaultOcxName2, 0, 0, MSForms, HTMLSelect"Attribute VB_Control = "DefaultOcxName1, 1, 1, MSForms, HTMLText"Attribute VB_Control = "DefaultOcxName, 2, 2, MSForms, HTMLText"

Attribute VB_Name = "ThisDocument"Attribute VB_Base = "1Normal.ThisDocument"Attribute VB_GlobalNameSpace = FalseAttribute VB_Creatable = FalseAttribute VB_PredeclaredId = TrueAttribute VB_Exposed = TrueAttribute VB_TemplateDerived = TrueAttribute VB_Customizable = TrueAttribute VB_Control = "DefaultOcxName2, 0, 0, MSForms, HTMLSelect"Attribute VB_Control = "DefaultOcxName1, 1, 1, MSForms, HTMLText"Attribute VB_Control = "DefaultOcxName, 2, 2, MSForms, HTMLText"


Recommended