Wednesday, December 26, 2007

ASP Summary

ASP Summary

This tutorial has taught you how to add server-side scripts to your web site, to make your web site more dynamic and interactive.

You have learned how to dynamically edit, change or add any content of a web page, respond to data submitted from HTML forms, access any data or databases and return the results to a browser, customize a web page to make it more useful for individual users.

For more information on ASP, please look at our ASP examples.


Now You Know ASP, What's Next?

The next step is to learn SQL and ADO.

SQL

SQL is a standard computer language for accessing and manipulating database systems.

SQL statements are used to retrieve and update data in a database. SQL works with database programs like MS Access, DB2, Informix, MS SQL Server, Oracle, Sybase, and other database systems.

If you want to learn more about SQL, please visit our SQL tutorial.

ADO

ADO is a programming interface to access data in a database from a web site.

ADO uses SQL to query data in a database.

If you want to learn more about ADO, please visit our ADO tutorial.

ASP Coding Starting

An ASP file can contain text, HTML tags and scripts. Scripts in an ASP file are executed on the server


What you should already know

Before you continue you should have some basic understanding of the following:

  • HTML / XHTML
  • A scripting language like JavaScript or VBScript

If you want to study these subjects first, find the tutorials on our Home page.


What is ASP?

  • ASP stands for Active Server Pages
  • ASP is a program that runs inside IIS
  • IIS stands for Internet Information Services
  • IIS comes as a free component with Windows 2000
  • IIS is also a part of the Windows NT 4.0 Option Pack
  • The Option Pack can be downloaded from Microsoft
  • PWS is a smaller - but fully functional - version of IIS
  • PWS can be found on your Windows 95/98 CD

ASP Compatibility

  • ASP is a Microsoft Technology
  • To run IIS you must have Windows NT 4.0 or later
  • To run PWS you must have Windows 95 or later
  • ChiliASP is a technology that runs ASP without Windows OS
  • InstantASP is another technology that runs ASP without Windows

What is an ASP File?

  • An ASP file is just the same as an HTML file
  • An ASP file can contain text, HTML, XML, and scripts
  • Scripts in an ASP file are executed on the server
  • An ASP file has the file extension ".asp"

How Does ASP Differ from HTML?

  • When a browser requests an HTML file, the server returns the file
  • When a browser requests an ASP file, IIS passes the request to the ASP engine. The ASP engine reads the ASP file, line by line, and executes the scripts in the file. Finally, the ASP file is returned to the browser as plain HTML

What can ASP do for you?

  • Dynamically edit, change or add any content of a Web page
  • Respond to user queries or data submitted from HTML forms
  • Access any data or databases and return the results to a browser
  • Customize a Web page to make it more useful for individual users
  • The advantages of using ASP instead of CGI and Perl, are those of simplicity and speed
  • Provide security since your ASP code can not be viewed from the browser
  • Clever ASP programming can minimize the network traffic

Important: Because the scripts are executed on the server, the browser that displays the ASP file does not need to support scripting at all!

HTML Forms

HTML Forms are used to select different kinds of user input.



Examples

Text fields
This example demonstrates how to create text fields on an HTML page. A user can write text in a text field.

Password fields
This example demonstrates how to create a password field on an HTML page.

(You can find more examples at the bottom of this page)


Forms

A form is an area that can contain form elements.Form elements are elements that allow the user to enter information (like text fields, textarea fields, drop-down menus, radio buttons, checkboxes, etc.) in a form.A form is defined with the <form> tag.





<form> 
<input><input>
</form>


Input

The most used form tag is the <input> tag. The type of input is specified with the type attribute. The most commonly used input types are explained below.

Text Fields

Text fields are used when you want the user to type letters, numbers, etc. in a form.







<form>
First name: <input type="text" name="firstname">
<br> Last name: <input type="text" name="lastname">
</form>


How it looks in a browser:
First name:



Last name:

Note that the form itself is not visible. Also note that in most browsers, the width of the text field is 20 characters by default.

Radio Buttons

Radio Buttons are used when you want the user to select one of a limited number of choices.







<form>
<input type="radio" name="sex" value="male">
Male <br> <input type="radio" name="sex" value="female">
Female
</form>


How it looks in a browser:



Male


Female

Note that only one option can be chosen.

Checkboxes
Checkboxes are used when you want the user to select one or more options of a limited number of choices.






<form>  I have a bike:
<input type="checkbox" name="vehicle" value="Bike"> <br />
I have a car: <input type="checkbox" name="vehicle" value="Car">
<br /> I have an airplane:
<input type="checkbox" name="vehicle" value="Airplane">
</form>


How it looks in a browser:

I have a bike:

I have a car:

I have an airplane:


The Form's Action Attribute and the Submit Button

When the user clicks on the "Submit" button, the content of the form is sent to another file. The form's action attribute defines the name of the file to send the content to. The file defined in the action attribute usually does something with the received input.







<form name="input" action="html_form_action.asp"  method="get">
Username: <input type="text" name="user">
<input type="submit" value="Submit"> </form>


How it looks in a browser:
Username:



If you type some characters in the text field above, and click the "Submit" button, you will send your input to a page called "html_form_action.asp". That page will show you the received input.


More Examples

Checkboxes
This example demonstrates how to create check-boxes on an HTML page. A user can select or unselect a checkbox.

Radio buttons

This example demonstrates how to create radio-buttons on an HTML page.

Simple drop down box

This example demonstrates how to create a simple drop-down box on an HTML page. A drop-down box is a selectable list.

Another drop down box

This example demonstrates how to create a simple drop-down box with a pre-selected value.

Textarea

This example demonstrates how to create a text-area (a multi-line text input control). A user can write text in the text-area. In a text-area you can write an unlimited number of characters.

Create a button

This example demonstrates how to create a button. On the button you can define your own text.

Fieldset around data

This example demonstrates how to draw a border with a caption around your data.

Form Examples

Form with input fields and a submit button

This example demonstrates how to add a form to a page. The form contains two input fields and a submit button.

Form with checkboxes

This form contains two checkboxes, and a submit button.

Form with radio buttons

This form contains two radio buttons, and a submit button.

Send e-mail from a form

This example demonstrates how to send e-mail from a form.


Form Tags




















































Tag Description
<form> Defines a form for user input
<input> Defines an input field
<textarea> Defines a text-area (a multi-line text input control)
<label> Defines a label to a control
<fieldset> Defines a fieldset
<legend> Defines a caption for a fieldset
<select> Defines a selectable list (a drop-down box)
<optgroup> Defines an option group
<option> Defines an option in the drop-down box
<button> Defines a push button
<isindex> Deprecated. Use <input> instead

HTML Tags

HTML documents are text files made up of HTML elements.
HTML elements are defined using HTML tags.


HTML Tags

  • HTML tags are used to mark-up HTML elements
  • HTML tags are surrounded by the two characters < and >
  • The surrounding characters are called angle brackets
  • HTML tags normally come in pairs like <b> and </b>
  • The first tag in a pair is the start tag, the second tag is the end tag
  • The text between the start and end tags is the element content
  • HTML tags are not case sensitive, <b> means the same as <B>


HTML Elements

Remember the HTML example from the previous page:





<html>
<head>
<title>Title of page</title>
</head>
<body>
This is my first homepage.
<b>This text is bold</b>
</body>
</html>
This is an HTML element:


<b>This text is bold</b>

The HTML element starts with a start tag: <b>

The content of the HTML element is: This text is bold
The HTML element ends with an end tag: </b>

The purpose of the <b> tag is to define an HTML element that should be displayed as bold.
This is also an HTML element:





<body>
This is my first homepage.
<b>This text is bold</b>
</body>
This HTML element starts with the start tag <body>, and ends with the end tag </body>.The purpose of the <body> tag is to define the HTML element that contains the body of the HTML document.

Why do We Use Lowercase Tags?

We have just said that HTML tags are not case sensitive: <B> means the same as <b>. If you surf the Web, you will notice that plenty of web sites use uppercase HTML tags in their source code. We always use lowercase tags. Why?

If you want to follow the latest web standards, you should always use lowercase tags. The World Wide Web Consortium (W3C) recommends lowercase tags in their HTML 4 recommendation, and XHTML (the next generation HTML) demands lowercase tags.


Tag Attributes

Tags can have attributes. Attributes provide additional information to an HTML element.
The following tag defines an HTML table: <table>. With an added border attribute, you can tell the browser that the table should have no borders: <table border="0">Attributes always come in name/value pairs like this: name="value".
Attributes are always specified in the start tag of an HTML element.Attributes and attribute values are also case-insensitive. However, the World Wide Web Consortium (W3C) recommends lowercase attributes/attribute values in their HTML 4 recommendation, and XHTML demands lowercase attributes/attribute values.

Always Quote Attribute Values

Attribute values should always be enclosed in quotes. Double style quotes are the most common, but single style quotes are also allowed.In some rare situations, like when the attribute value itself contains quotes, it is necessary to use single quotes:

name='John "ShotGun" Nelson'

Start Web Design

What is an HTML File?

  • HTML stands for Hyper Text Markup Language
  • An HTML file is a text file containing small markup tags
  • The markup tags tell the Web browser how to display the page
  • An HTML file must have an htm or html file extension
  • An HTML file can be created using a simple text editor

Do You Want to Try It?

If you are running Windows, start Notepad.

If you are on a Mac, start SimpleText.

In OSX start TextEdit and change the following preferences: Open the the "Format" menu and select "Plain text" instead of "Rich text". Then open the "Preferences" window under the "Text Edit" menu and select "Ignore rich text commands in HTML files". Your HTML code will probably not work if you do not change the preferences above!

Type in the following text:



<html>
<head>
<title>Title of page</title>
</head>
<body>
This is my first homepage.
<b>This text is bold</b>
</body>
</html>


Save the file as "mypage.htm".

Start your Internet browser. Select "Open" (or "Open Page") in the File menu of your browser. A dialog box will appear. Select "Browse" (or "Choose File") and locate the HTML file you just created - "mypage.htm" - select it and click "Open". Now you should see an address in the dialog box, for example "C:\MyDocuments\mypage.htm". Click OK, and the browser will display the page.


Example Explained

The first tag in your HTML document is . This tag tells your browser that this is the start of an HTML document. The last tag in your document is . This tag tells your browser that this is the end of the HTML document.

The text between the tag and the tag is header information. Header information is not displayed in the browser window.

tags is the title of your document. The title is displayed in your browser's caption.The text between the

The text between the tags is the text that will be displayed in your browser.

The text between the and tags will be displayed in a bold font.


HTM or HTML Extension?

When you save an HTML file, you can use either the .htm or the .html extension. We have used .htm in our examples. It might be a bad habit inherited from the past when some of the commonly used software only allowed three letter extensions.

With newer software we think it will be perfectly safe to use .html.


Note on HTML Editors:

You can easily edit HTML files using a WYSIWYG (what you see is what you get) editor like FrontPage or Dreamweaver, instead of writing your markup tags in a plain text file.

However, if you want to be a skillful Web developer, we strongly recommend that you use a plain text editor to learn your primer HTML.


Frequently Asked Questions

Q: After I have edited an HTML file, I cannot view the result in my browser. Why?
A:
Make sure that you have saved the file with a proper name and extension like "c:\mypage.htm". Also make sure that you use the same name when you open the file in your browser.

Q: I have edited an HTML file, but the changes don't show in the browser. Why?
A:
A browser caches pages so it doesn't have to read the same page twice. When you have modified a page, the browser doesn't know that. Use the browser's refresh/reload button to force the browser to reload the page.

Q: What browser should I use?
A:
You can do all the training with all of the well-known browsers, like Internet Explorer, Firefox, Netscape, or Opera. However, some of the examples in our advanced classes require the latest versions of the browsers.

Q: Does my computer have to run Windows? What about a Mac?
A:
You can do all your training on a non-Windows computer like a Mac.

Saturday, December 22, 2007

SQL Server Tips

1) How to start SQLServer in Single User Mode

At times you might want to start SQL server in a single user mode to perform some maintenance work on the server or to change server configurations or to recover a damaged database. You can accomplish this in any of the three ways given below :-

a) From Command Prompt :-

» sqlservr -m

b) From Startup Options :-

» Go to SQL Server Properties by right-clicking on the Server name in the Enterprise manager.
» Under the 'General' tab, click on 'Startup Parameters'.
» Enter a value of -m in the Parameter.

c) From Registry :-

» Go to HKEY_LOCAL_MACHINE\Software\Microsoft\MSSQLServer\MSSQLServer\Parameters.
» Add new string value.
» Specify the 'Name' as SQLArg(n) & 'Data' as -m.
Where n is the argument number in the list of arguments.

Caution: Be careful while editing the Registry. Incorrectly setting up Registry values can cause unpredictable behavior.


2) How to start SQL Server in Minimal Configuration Mode

Sometimes a bad configuration value can prevent SQL Server from starting. Then it won't even let you connect to SQL Server using Enterprise Manager and correct the configuration values. The only option is to start SQL Server in a minimum configuration mode and then correct the configuration values and restart the SQL Server in normal mode. Here's how you can start the SQL Server in a minimal configuration mode :-

a) From Command Prompt :-

» sqlservr -f

b) From Startup Options :-

» Go to SQL Server Properties by right-clicking on the Server name in the Enterprise manager.
» Under the 'General' tab, click on 'Startup Parameters'.
» Enter a value of -f in the Parameter.

c) From Registry :-

» Go to HKEY_LOCAL_MACHINE\Software\Microsoft\MSSQLServer\MSSQLServer\Parameters.
» Add new string value.
» Specify the 'Name' as SQLArg(n) & 'Data' as -f.
Where n is the argument number in the list of arguments.

Caution: Be careful while editing the Registry. Incorrectly setting up Registry values can cause unpredictable behavior.

3) How to get Output parameter value from Dynamic execution of a Query

Sometimes you have to execute a Query dynamically using Exec(@Sql). This method works fine as long as you don't want any output values from the @Sql query. But there's another method (sp_Executesql) that allows you to execute queries dynamically as well as get their output values.

The syntax :-
EXECUTE sp_executesql
@SQLString,
@ParmDefinition,
@parm=@parmIN,
@parm1OUT=@parmRET1 OUTPUT,@parm2OUT=@parmRET2 OUTPUT
Example :-
DECLARE @IntVariable INT
DECLARE @SQLString nVARCHAR(500)
DECLARE @ParmDefinition nVARCHAR(500)
DECLARE @Lastlname nVARCHAR(30)

SET @SQLString = 'SELECT @LastlnameOUT = max(lname) FROM
pubs.dbo.employee WHERE job_lvl = @level'

SET @ParmDefinition = '@level tinyint, @LastlnameOUT
varchar(30) OUTPUT'
SET @IntVariable = 35
EXECUTE sp_executesql @SQLString, @ParmDefinition, @level
= @IntVariable, @LastlnameOUT=@Lastlname OUTPUT
SELECT @Lastlname

Note: sp_Executesql is a Extended Stored Procedure.


4) How to move Database/Transaction Log files

You can move Database & Transaction log files to a different location in 2 ways.

a) sp_detach_db & then sp_attach_db

» Make sure that no user is using the database.
» Exec sp_detach_db northwind
» Move the Data & log files to a different location
» EXEC sp_attach_db 'Northwind', 'c:\mssql7\northwnd.mdf', 'c:\mssql7\northwnd.ldf'

b) BACKUP and RESTORE using WITH MOVE

» Backup :-
Backup Database Northwind To Disk = 'C:\mssql7\backup\nwind.bak'
Go

» Restore :-
USE Master
Go

RESTORE Database northwind from DISK = 'c:\mssql7\backup\nwind.bak'
WITH MOVE 'Northwind' TO 'c:\mssql7\Northwnd.mdf',
MOVE 'Northwind_log' TO 'c:\mssql7\Northwnd.ldf'
Go

c) Can be used only for moving Tempdb files.

» Use ALTER Database statement to specify a different Path for the filename.

ALTER DATABASE Tempdb MODIFY FILE (NAME = Tempdev, FILENAME = 'c:\mssql7\tempdb.mdf')

ALTER DATABASE Tempdb MODIFY FILE (NAME = Templog, FILENAME = 'c:\mssql7\templog.ldf')

» Restart SQL Server and delete the old files.

5) How to Rename a Database

a) Using sp_rename

» Make sure that no user is using the database.
» Make the database in the single user mode. You can do this by using sp_dboption.

sp_dboption 'Pubs', 'single user', true

» sp_rename Pubs, Library, Database

Note : For renaming the database, you can also use sp_renamedb.

sp_renamedb Pubs, Library

» Bring back the database in multiuser mode

sp_dboption 'Library', 'single user', false

b) Using Detach & Attach

sp_detach_db @dbname = N'Pubs'
sp_attach_db @dbname = N'Library',
@filename1 = N'd:\programfiles\MSSQL7\data\Pubs_Data.MDF',
@filename2 = N'd:\program files\MSSQL7\data\Pubs_log.ldf'

Deleting table rows using Javascript

<html>
<head>
<script type="text/javascript">
function deleteRow(i){

document.getElementById('myTable').deleteRow(i)

}
</script>
</head>

<body>
<table id="myTable" border="1">
<tr>
<td>Row 1</td>
<td><input type="button" value="Delete" onclick="deleteRow(this.parentNode.parentNode.rowIndex)"></td>
</tr>
<tr>
<td>Row 2</td>
<td><input type="button" value="Delete" onclick="deleteRow(this.parentNode.parentNode.rowIndex)"></td>
</tr>
<tr>
<td>Row 3</td>
<td><input type="button" value="Delete" onclick="deleteRow(this.parentNode.parentNode.rowIndex)"></td>
</tr>
</table>
</body>
</html>

Adding table rows

<html>
<head>
<script type="text/javascript">
function insRow(){
var x=document.getElementById('myTable').insertRow(2)
var y=x.insertCell(0)
var z=x.insertCell(1)
y.innerHTML="NEW CELL1"
z.innerHTML="NEW CELL2"
}
</script>
</head>
<body>
<table id="myTable" border="1">
<tr>
<td>d</td>
<td>d</td>
</tr>
<tr>
<td>d</td>
<td>d</td>
</tr>
<tr>
<td>Row3 cell1</td>
<td>Row3 cell2</td>
</tr>
<tr>
<td>Row4 cell1</td>
<td>Row4 cell2</td>
</tr>
<tr>
<td>Row5 cell1</td>
<td>Row5 cell2</td>
</tr>
</table>
<form>
<input type="button" onclick="insRow()" value="Insert row">
</form>
</body>
</html>