Thursday, September 30, 2010

ASP.net Page Life Cycle

ASP.NET determines that the page request by a user requires to be parsing and compiling or to render cached copy of the page to be send. Thus it comes very much before of the beginning of the page life cycle. After this, it is also checked that request is a normal request, postback, cross-page postback or callback. The page constructor creates a tree of controls as soon as the HTTP runtime instantiates the page class to perform the request.

Events

PreInit

This event is the beginning of the page life cycle. Every page controls are initialized and the properties are set according to aspx source code.

  • Possible to Change or set Master page, Themes
  • Creates or re-creates dynamic controls.
  • Reads or sets Profile property values.


Init

First, the Init event for the Page object occurs, then Init event occurs for each control on the Page. Viewstate information is not available at this stage.

  • Controls have been initialized zed
  • Theme skins applied if any.
  • Initialize control properties.


InitComplete


This event is used to processing tasks that require all initialization be complete.

PreLoad

This event is used before Perform any processing that should occur before Load. Use this event if you need to perform processing on your page or control before the Load event. Before the Page instance raises this event, it loads view state for itself and all controls, and then processes any postback data included with the Request instance.

Load

Set properties in controls and establish database connections

Control Events

These are control specific events such as – Button Click, DropDownIndexChanged etc.

Load Complete

This event is used for performing those tasks which require load has been completed.

PreRender

In this event page insures that all child controls are created. Page calls EnsureChildControls for all controls, including itself. Every controls whose datasource/databind property is set calls for its databind method.

SaveStateComplete

This event occurs after viewstate encoded and saved for the page and for all controls.

Render

Every ASP.NET control has render method and the page instance calls this method to output the control’s markup, after this event any changes to the page or controls are ignored.

Unload

Unload event used to do cleanup task like closing the database connections, closing of the open files, completing logging or other requested tasks. Unload events occurs for each control on page control tree first and after that page.

Wednesday, September 29, 2010

Working with SQL Database - part 2 [Stored Procedures]

Using Stored Procedures:

A Stored Procedure is a set of Structured Query Language (SQL) statements with an assigned name that's stored in the database in compiled form so that it can be shared by a number of programs. The use of stored procedures can be helpful in controlling access to data (end-users may enter or change data but do not write procedures), preserving data integrity (information is entered in a consistent manner), and improving productivity (statements in a stored procedure only need to be written one time).


Syntax:

CREATE PROCEDURE [Procedure_Name]
(
[@Param1] [type],
[@Param2] [type]......
)
AS
BEGIN
[Your SQL Stataments]
END
GO

Example:

CREATE PROCEDURE setData
(
@Name VARCHAR(30),
@EMAIL VARCHAR(30),
@MOBILE VARCHAR(30)
)
AS
BEGIN
INSERT INTO testdata(name,email,mobile)
VALUES(@Name,@EMAIL,@MOBILE);
END

Implementation:

cmd = new SqlCommand("
setData
",con); //cmd- Command, con- Connection Variables.
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = txtName.Text;
cmd.Parameters.Add("@Email", SqlDbType.
VarChar
).Value = txtEmail.Text;
cmd.Parameters.Add("@Mobile", SqlDbType.BigInt).Value = txtMobile.Text;
cmd.ExecuteNonQuery();

The above statements when executed inserts the records in to the database table.

Tuesday, September 28, 2010

Working with SQL Database - part 1

1. Add a connection string in your application's web.config file.

In the connectionStrings tag, add a key and value for publishing the connection.

2.Include the following namespaces.

using System.Data.SqlClient;
using System.Data;
using System.Configuration;

3.Declare the basic variables for database interaction in your application.

SqlConnection con;
SqlCommand cmd;
SqlDataReader rd;

4.Establish connection with your database.

con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ConnectionString.ToString());
con.Open();

5.After establishing connection. It is possible perform all the operations on the database. First we look into entering data into the database.

cmd = new SqlCommand("insert into [tablename] values(@arg1,@arg2,...)",con);
cmd.Parameters.Add("@rangeFrom", SqlDbType.Int).Value = [yourvalue];
cmd.Parameters.Add("@rangeTo", SqlDbType.VarChar).Value = [yourvalue];
cmd.ExecuteNonQuery(); // this executes the above query and the data is stored.

6.So we stored the data. Now let us focus on retrieving the data.

cmd = new SqlCommand("select * from [tablename]", con);
rd = cmd.ExecuteReader(); // used for select statements.
Grid.DataSource = rd; // Assigning the data to a GridView Control
Grid.DataBind();

7. Once you run your application, the data from the table will displayed in the Grid.

What is ASP?

Active Server Pages (ASP) is a server side scripting language that lets a webmaster transform the plain, static web site into a dazzling, dynamic solution. With Microsoft's server side scripting language you can gather data from your site's visitors, use sessions, cookies, application variables, and more.