Friday, February 27, 2009

What are different types of directives in .NET?

directives allow you to specify page properties and configuration information for the page. The directives are used by ASP.NET as instructions for how to process the page, but they are not rendered as part of the markup that is sent to the browser...
@Page: Defines page-specific attributes used by the ASP.NET page parser and compiler. Can be included only in .aspx files <%@ Page AspCompat="TRUE" language="C#" %>
@Control:Defines control-specific attributes used by the ASP.NET page parser and compiler. Can be included only in .ascx files. <%@ Control Language="VB" EnableViewState="false" %>
@Import: Explicitly imports a namespace into a page or user control. The Import directive cannot have more than one namespace attribute. To import multiple namespaces, use multiple @Import directives. <% @ Import Namespace="System.web" %>
@Implements: Indicates that the current page or user control implements the specified .NET framework interface.<%@ Implements Interface="System.Web.UI.IPostBackEventHandler" %>
@Register: Associates aliases with namespaces and class names for concise notation in custom server control syntax.<%@ Register Tagprefix="Acme" Tagname="AdRotator" Src="AdRotator.ascx" %>
@Assembly: Links an assembly to the current page during compilation, making all the assembly's classes and interfaces available for use on the page. <%@ Assembly Name="MyAssembly" %><%@ Assembly Src="MySource.vb" %>
@OutputCache: Declaratively controls the output caching policies of an ASP.NET page or a user control contained in a page<%@ OutputCache Duration="#ofseconds" Location="Any | Client | Downstream | Server | None" Shared="True | False" VaryByControl="controlname" VaryByCustom="browser | customstring" VaryByHeader="headers" VaryByParam="parametername" %>
@Reference: Declaratively indicates that another user control or page source file should be dynamically compiled and linked against the page in which this directive is declared.

Thursday, February 26, 2009

Asp.net - Dataset vs Datareader

To better improve the performance of a solution, we have to
understand the DataSet and DataReader and they have to be used correctly in the places they needed. Let see how they work...

DataReader
Datareader is like a forward only recordset. It fetches one
row at a time so very less Network Cost compare to DataSet(Fetches all the rows at a time). DataReader is readonly so we cannot do any transaction on them. DataReader will be the best choice where we need to show the data to the user which requires no transaction ie reports. Due to DataReader is forward only we cannot fetch the data randomly. .NET Dataproviders optimizes the datareaders to handle the huge amount of data.

DataSet
DataSet is always a bulky object that requires lot of memory space compare to DataReader. We can say the dataset as a small database coz it stores the schema and data in the application memory area. DataSet fetches all data from the datasource at a time to its memory area. So we can traverse through the object to get required data like qureying database.

The dataset maintains the relationships among the datatables inside
it. We can manipulate the realational data as XML using dataset.We can do transactions (insert/update/delete) on them and finally the modifications can be updated to the actual database. This provides impressive flexibility to the application but with the cost of memory space. DataSet maintains the original data and the modified data seperately which requires more memory space. If the amount of data in the dataset is huge
then it will reduce the applications performance dramatically.

The following points will improve the performane of a dataset...

1. Don't use the commandbuilder to generate the sql statements.
Though it reduces the development time the query generated

by the command builder will not be always as required. For example
To update the details of an author table the command

builder will generate a query like this ...

Query generated by CommandBuilder

UPDATE authors
SET
au_id = ?, au_fname = ?, au_lname = ?, phone = ?,
zip = ?, state = ?, city = ?, address = ?
WHERE
(au_id = ?)
AND (address = ? OR ? IS NULL AND address IS NULL)
AND (au_fname = ?)
AND (au_lname = ?)
AND (city = ? OR ? IS NULL AND city IS NULL)
AND (phone = ?)
AND (state = ? OR ? IS NULL AND state IS NULL)
AND (zip = ? OR ? IS NULL AND zip IS NULL)

But we can write much more efficient query than the above like
this ..
UPDATE authors
SET
au_id = ?, au_fname = ?, au_lname = ?, phone = ?,
zip = ?, state = ?, city = ?, address = ?
WHERE
(au_id = ?)

2. Always select the columns required in the quries. Don't use "select * from ". Take an example of authors table which has the author id , name, address, image. For showning the author name,address if we use select * then the image column also fetched from the database which requires more memory and NetWork Cost will be more due the amount of data. This will reduce the applications performance.

3. Always try to use the parameter collection for the stored
procedures. We can execute the stored proceduers in two ways.
a. we can create a string as a query and can execute it.
Ex "getAuthor(7689)".
b. we can create a command object. Assign the Stored Procedure
name to the commandtext property. Specify the commandtype as stored procedure. Create a parameter for the value passed to the SP and execute.
Ex:
SqlCommand sqlcmd = new SqlCommand();
sqlcmd.CommandText="getAuthors";
sqlcmd.CommandType= CommandType.StoredProcedure;
SqlParameter sqlparam = new SqlParameter
("@au_id",SqlDbType.Int);
sqlparam.Value = 7689;
sqlcmd.Parameters.Add(sqlparam);
sqlcmd.Connection= con;
sqlcmd.ExecuteReader();

There are lot of difference between the way the two quries executed.
The first query will be sent to the database server as a string though we think we are passing the author id as int. The database server will parse the query and will determine the datatype of the parameter. This extra processing will increase the execution time.
But while executing the second query the native datatype of the
parameter is sent to the database server as network packets throuh RPC.

Wednesday, February 18, 2009

Asp.Net Beginners

ASP.NET
ASP.NET is a platform that excels in the realm of Web application development. It also can be used in stand-alone applications. ASP.NET produces HTML content—some static and unchanging, some dynamic and changeable.

The static HTML content that's output using ASP.NET is similar to what you can create with Notepad or FrontPage. When you use an authoring tool such as FrontPage, you typically use the WYSIWYG editor and save the file somewhere—usually to a Web server. The HTML content is then served up when a client machine makes a request to the Web server.

Many times, though, the content needs to adapt itself to the situation. A particular user might need additional menu choices in the Web application. Or if your business offers specials each Tuesday, you might need special images to appear next to those items in you product catalog each Tuesday. As a technology for dynamically creating HTML, ASP.NET works in instances such as these.

Many of you have developed ASP applications in which the files all have .asp extensions. ASP.NET application files have an .aspx extension to differentiate them from ASP files. Because their extensions are different, both classic ASP and ASP.NET can function side-by-side in the same Web site.

ASP.NET files have a major performance advantage over ASP files. The first time they are requested, they are compiled into native code, which makes them execute much faster than the interpreted ASP files.

In classic ASP, you didn't have many language choices. Anything you could do in HTML, such as JScript and JavaScript, were available. For server-side programming, VBScript was one choice, and for ISAPI programming, VB and C++. Now, with ASP.NET, you get VB, C#, and JScript out of the box. And other languages—Cobol, RPG, Pascal, and many others—are under development by third-party vendors. ASP.NET was developed from the ground up to be language agnostic—in other words, any language should give you similar final results.

In terms of extra effort required, this multiple-language support and runtime compilation to native code doesn't come at any price to developers. To save an ASP.NET file, all you have to do is save it to disk—no compile button and no other steps.

ASP.NET has a new control-based, event-driven execution model. You can hook a Page_Load() method, an event that a server-side control fires off such as an OnItemCommand() method, or any other event that is available. The naming of Page_Load() in C# is the default behavior. The event handler could be named iRool and not affect whether the event was called. By comparison, in VB.NET, similar methods end with handles Page.Load.

In all, the new model that ASP.NET follows offers these benefits: It requires that you write less code, it encapsulates functionality into easy-to-use classes, and it reduces the amount of spaghetti code you'll be inclined to write.

Many of the applications we'll write in this book use Visual Studio .Net as the development tool, with ASP.NET as the deployment and runtime environment. We chose this combination because of the heavy emphasis that Microsoft is placing on distributed applications, and the rising need for you to develop enterprise and distributed applications.

About Me

Hyderabad, Andhra Pradesh, India
I'm a MCA graduate working as a Web Developer.