Google Search

Google
 

Wednesday, August 22, 2007

ThumbNail Creation in ASP.Net

Creation of thumbnails from image in ASP.Net is very easy task.

Following are Steps to create thumbnails image in ASP.Net :

1)Read the Image using the method Image.FromStream() as,

Dim imInputImage, imThumbNail As Image

imInputImage = System.Drawing.Image.FromStream(imgUpload.PostedFile.InputStream)


here,imgUploadis the id of FileUpload Control.

2)Get the ThumbNail Image using the method GetThumbnailImage.

imThumbNail = imInputImage.GetThumbnailImage(100, 100, Nothing, System.IntPtr.Zero)

We can also specify the format of the Thumbnail image we want to save by,

imThumbNail.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg)

Some of the Formats can be,
Jpeg- System.Drawing.Imaging.ImageFormat.Jpeg
Gif - System.Drawing.Imaging.ImageFormat.Gif
Tiff- System.Drawing.Imaging.ImageFormat.Tiff
Png - System.Drawing.Imaging.ImageFormat.Png

Thursday, August 2, 2007

Page Directives in ASP.NET 2.0

Page directives configure the runtime environment that will execute the page. The complete list of directives is as follows:

@ Assembly - Links an assembly to the current page or user control declaratively.

@ Control - Defines control-specific attributes used by the ASP.NET page parser and compiler and can be included only in .ascx files (user controls).

@ Implements - Indicates that a page or user control implements a specified .NET Framework interface declaratively.

@ Import - Imports a namespace into a page or user control explicitly.

@ Master - Identifies a page as a master page and defines attributes used by the ASP.NET page parser and compiler and can be included only in .master files.

@ MasterType - Defines the class or virtual path used to type the Master property of a page.

@ OutputCache - Controls the output caching policies of a page or user control declaratively.

@ Page - Defines page-specific attributes used by the ASP.NET page parser and compiler and can be included only in .aspx files.

@ PreviousPageType - Creates a strongly typed reference to the source page from the target of a cross-page posting.

@ Reference - Links a page, user control, or COM control to the current page or user control declaratively.

@ Register - Associates aliases with namespaces and classes, which allow user controls and custom server

Folders in ASP.NET 2.0

There are seven new folders introduced in ASP.NET 2.0 :

\App_Browsers folder – Holds browser definitions(.brower) files which identify the browser and their capabilities.

\App_Code folder – Contains source code (.cs, .vb) files which are automatically compiled when placed in this folder. Additionally placing web service files generates a proxy class(out of .wsdl) and a typed dataset (out of .xsd).

\App_Data folder – Contains data store files like .mdf (Sql Express files), .mdb, XML files etc. This folder also stores the local db to maintain membership and role information.

\App_GlobalResources folder – Contains assembly resource files (.resx) which when placed in this folder are compiled automatically. In earlier versions, we were required to manually use the resgen.exe tool to compile resource files. These files can be accessed globally in the application.

\App_LocalResources folder – Contains assembly resource files (.resx) which can be used by a specific page or control.

\App_Themes folder – This folder contains .css and .skin files that define the appearance of web pages and controls.

\App_WebReferences folder – Replaces the previously used Web References folder. This folder contains the .disco, .wsdl, .xsd files that get generated when accessing remote web services.

Tuesday, July 31, 2007

Event Life cycle of ASP.NET 2.0

The events occur in the following sequence. Its best to turn on tracing() and track the flow of events :

PreInit – This event represents the entry point of the page life cycle. If you need to change the Master page or theme programmatically, then this would be the event to do so. Dynamic controls are created in this event.

Init – Each control in the control collection is initialized.

Init Complete* - Page is initialized and the process is completed.

PreLoad* - This event is called before the loading of the page is completed.

Load – This event is raised for the Page and then all child controls. The controls properties and view state can be accessed at this stage. This event indicates that the controls have been fully loaded.

LoadComplete* - This event signals indicates that the page has been loaded in the memory. It also marks the beginning of the rendering stage.

PreRender – If you need to make any final updates to the contents of the controls or the page, then use this event. It first fires for the page and then for all the controls.

PreRenderComplete* - Is called to explicitly state that the PreRender phase is completed.

SaveStateComplete* - In this event, the current state of the control is completely saved to the ViewState.

Unload – This event is typically used for closing files and database connections. At times, it is also used for logging some wrap-up tasks.

The events marked with * have been introduced in ASP.NET 2.0.

Monday, June 18, 2007

IDENT_CURRENT,SCOPE_IDENTITY and @@IDENTITY

SCOPE_IDENTITY, IDENT_CURRENT, and @@IDENTITY are similar functions in that they return values inserted into IDENTITY columns.



IDENT_CURRENT is not limited by scope and session; it is limited to a specified table. IDENT_CURRENT returns the value generated for a specific table in any session and any scope.



SCOPE_IDENTITY and @@IDENTITY will return last identity values generated in any table in the current session.



However, SCOPE_IDENTITY returns values inserted only within the current scope; @@IDENTITY is not limited to a specific scope.

Thursday, June 14, 2007

Stored Procedures Optimization Tips

Use stored procedures instead of heavy-duty queries.

This can reduce network traffic, because your client will send to server only stored procedure name (perhaps with some parameters) instead of large heavy-duty queries text. Stored procedures can be used to enhance security and conceal underlying data objects also. For example, you can give the users permission to execute the stored procedure to work with the restricted set of the columns and data.


*****

Include the SET NOCOUNT ON statement into your stored procedures to stop the message indicating the number of rows affected by a Transact-SQL statement.

This can reduce network traffic, because your client will not receive the message indicating the number of rows affected by a Transact-SQL statement.

*****

Call stored procedure using its fully qualified name.

The complete name of an object consists of four identifiers: the server name, database name, owner name, and object name. An object name that specifies all four parts is known as a fully qualified name. Using fully qualified names eliminates any confusion about which stored procedure you want to run and can boost performance because SQL Server has a better chance to reuse the stored procedures execution plans if they were executed using fully qualified names.

*****

Consider returning the integer value as an RETURN statement instead of an integer value as part of a recordset.

The RETURN statement exits unconditionally from a stored procedure, so the statements following RETURN are not executed. Though the RETURN statement is generally used for error checking, you can use this statement to return an integer value for any other reason. Using RETURN statement can boost performance because SQL Server will not create a recordset.

*****

Don't use the prefix "sp_" in the stored procedure name if you need to create a stored procedure to run in a database other than the master database.

The prefix "sp_" is used in the system stored procedures names. Microsoft does not recommend to use the prefix "sp_" in the user-created stored procedure name, because SQL Server always looks for a stored procedure beginning with "sp_" in the following order: the master database, the stored procedure based on the fully qualified name provided, the stored procedure using dbo as the owner, if one is not specified. So, when you have the stored procedure with the prefix "sp_" in the database other than master, the master database is always checked first, and if the user-created stored procedure has the same name as a system stored procedure, the user-created stored procedure will never be executed.

*****
Use the sp_executesql stored procedure instead of the EXECUTE statement.

The sp_executesql stored procedure supports parameters. So, using the sp_executesql stored procedure instead of the EXECUTE statement improve readability of your code when there are many parameters are used. When you use the sp_executesql stored procedure to executes a Transact-SQL statements that will be reused many times, the SQL Server query optimizer will reuse the execution plan it generates for the first execution when the change in parameter values to the statement is the only variation.

*****
Use sp_executesql stored procedure instead of temporary stored procedures.

Microsoft recommends to use the temporary stored procedures when connecting to earlier versions of SQL Server that do not support the reuse of execution plans. Applications connecting to SQL Server 7.0 or SQL Server 2000 should use the sp_executesql system stored procedure instead of temporary stored procedures to have a better chance to reuse the execution plans.

*****
If you have a very large stored procedure, try to break down this stored procedure into several sub-procedures, and call them from a controlling stored procedure.

The stored procedure will be recompiled when any structural changes were made to a table or view referenced by the stored procedure (for example, ALTER TABLE statement), or when a large number of INSERTS, UPDATES or DELETES are made to a table referenced by a stored procedure. So, if you break down a very large stored procedure into several sub-procedures, you get chance that only a single sub-procedure will be recompiled, but other sub-procedures will not.

*****
Try to avoid using temporary tables inside your stored procedure.

Using temporary tables inside stored procedure reduces the chance to reuse the execution plan.

*****
Try to avoid using DDL (Data Definition Language) statements inside your stored procedure.

Using DDL statements inside stored procedure reduces the chance to reuse the execution plan.

*****
Add the WITH RECOMPILE option to the CREATE PROCEDURE statement if you know that your query will vary each time it is run from the stored procedure.

The WITH RECOMPILE option prevents reusing the stored procedure execution plan, so SQL Server does not cache a plan for this procedure and the procedure is recompiled at run time. Using the WITH RECOMPILE option can boost performance if your query will vary each time it is run from the stored procedure because in this case the wrong execution plan will not be used.

*****
Use SQL Server Profiler to determine which stored procedures has been recompiled too often.

To check the stored procedure has been recompiled, run SQL Server Profiler and choose to trace the event in the "Stored Procedures" category called "SP:Recompile". You can also trace the event "SP:StmtStarting" to see at what point in the procedure it is being recompiled. When you identify these stored procedures, you can take some correction actions to reduce or eliminate the excessive recompilations.

*****

Friday, June 1, 2007

Date Time

Dim dtNow As DateTime = DateTime.Now

'Date OutPut : 6/1/2007 2:53:10 PM
Response.Write("
Date Time :" + dtNow)

'Long Date : Friday, June 01, 2007
Response.Write("
Long Date :" & dtNow.ToLongDateString())

'Short Date : 6/1/2007
Response.Write("
Short Date :" & dtNow.ToShortDateString())

'Long Time : 2:53:10 PM
Response.Write("
Long Time :" & dtNow.ToLongTimeString())

'Short Time : 2:53 PM
Response.Write("
Short Time :" & dtNow.ToShortTimeString())

'Day Of Week : Friday
Response.Write("
Day Of Week : " & dtNow.DayOfWeek.ToString)

'Day Of Year : 152
Response.Write("
Day Of Week : " & dtNow.DayOfYear.ToString)


Dim nowdayofweek As Integer = dtNow.DayOfWeek
Dim weekStartDate, weekEndDate As DateTime

weekStartDate = DateAdd("d", 0 - dtNow.DayOfWeek, dtNow)
weekEndDate = DateAdd("d", 6 - dtNow.DayOfWeek, dtNow)

'Displays first day of the week : 03/18/2007
Response.Write("
Start Date-->" & weekStartDate.ToString("MM/dd/yyyy"))

'Displays last day of the week : 03/24/2007
Response.Write("
End Date-->" & weekEndDate.ToString("MM/dd/yyyy"))