Thursday, July 10, 2008
Assembly Directive
Assembly Directive used to link assembly in to our application.
To Make our ASP.NET page aware of external components.
It has following attibutes/property.
a. Name: Enables you specify the name of an assembly you want to attach to the page.
Here you should mention the filename without the extension.
b. Src: represents the name of a source code file
<%@ Assembly Name="YourAssemblyName" %>
Thursday, July 3, 2008
Asp.net Directives
We have eleven directives to enrich and nurture our asp.net pages or user control.
Directives are opened <%@ and close with %>
Developers have tendacy to decalre it on top of the page, It does not matter if you declare them at any part of the page.
List of directives in alphabatical order.
Assembly :- Used to link assembly.Control :- For User Control.Implements:- For implement .Net interface.Import:- For Importing Namespace in to page or user control.
Master :- For Using Master page , .Master Page required.MasterType:-Associate a class name to get strongly typed refrences within the specfied master page.
OutputCache:-Controls the Caching Mechanism.
Page:- Allow users to specify a page level attributes,Only can be used with Asp.net Page.
PreviousPageType:-Enables in asp.net Page to handles few things about previous page.
Reference:-Links page or user control to the current page or user control.
Register:-Associate aliase with namespaces and class name for notation in the custom server control.
I will try to explain each useful directive one by one on my forthcoming
posts.......
Saturday, June 28, 2008
Web Hosting and Domain
Let me share few information regarding Web hosting and DNS.
Web Host -Means Server where our website resides.This always up and connected to internet to make our web site active.
Shared Hosting:- Most of the host provides Shared Hosting , as it is meaningless to use whole server for small website which does not have very heavy traffic or not a big application.
At that time on a on server more then one website is hosted and manged.shared hosting is widely used , it is very cost effective too.Shared hosting may not provide full access to server(it is depends upon service provider also).
Dedicated Server:- Dedicated Server means only one particuler website or particular user owns a server.Where Owner(Person who purchase Dedicated server from host ,yes your own pc also can be a dedicated server).Owner have full access of server ,freedom of installing new s/w ,uninstall s/w.
DNS:-Domain Name Server
As we all knows on back side web request is made through IPs.
DNS :- Map the IP(where our website hosted) and Domain Name (like www.google.com).
As defination you can say
DNS is the service which translates between Internet names and Internet addresses.
If you are working on a Windows machine, you can view your current IP address with the command WINIPCFG.EXE (IPCONFIG.EXE for Windows 2000/XP). On a UNIX machine, type nslookup along with a machine name (such as "nslookup www.howstuffworks.com") to display the IP address of the machine (use the command hostname to learn the name of your machine).
DNS is such a big thing so it can not be explained in a single post I will add more information about it.
Monday, June 23, 2008
Create Error log in Asp.net2.0
To maintain error log its good to have log for each day for a better review .
so here we will create log file everyday , and when file already exist will append error entry in to it.
For that create one class and write following method....
C#
public static void WriteError(string errorMessage)
{
try
{
//Specify path where log file to be created
string logfilePath = "~/Error/" + DateTime.Today.ToString("dd-mm-yy") + ".txt";
if (!File.Exists(System.Web.HttpContext.Current.Server.MapPath(logfilePath)))
{
File.Create(System.Web.HttpContext.Current.Server.MapPath(logfilePath)).Close();
}
using (StreamWriter wrtr = File.AppendText(System.Web.HttpContext.Current.Server.MapPath(logfilePath)))
{
wrtr.WriteLine("\r\nLog Entry : ");
wrtr.WriteLine("{0}", DateTime.Now.ToString(CultureInfo.InvariantCulture));
string err = "Error in: " + System.Web.HttpContext.Current.Request.Url.ToString() +
". Error Message:" + errorMessage;
wrtr.WriteLine(err);
wrtr.WriteLine("__________________________");
wrtr.Flush();
wrtr.Close();
}
}
catch (Exception ex)
{
WriteError(ex.Message);
}
}
Wednesday, June 18, 2008
Caching Concept
· Output Caching
· Fragment Caching
· Data Caching
· Cache Configuration
Output Caching
Performance is improved in an ASP.NET application by caching the rendered markup and serving the cached version of the markup until the cache expires. For example, if you have a page that displays user information from a database, caching will help improve performance by serving the page from memory instead of making a connection to the database on each page request.
You can cache a page by using the OutputCache API or simply by using the @outputcache directive. <%@ Page Language=”C#” %><%@ OutputCache Duration=”15” VaryByParam=”none” %>
The above cache directive will cache the page for 15 minutes.
So output caching is great for when you want to cache an entire page.
Fragment Caching
In many situations caching the entire page just isn’t going to cut it, for some reason or another you require specific sections of the page to display live information. One way to improve performance is to analyze your page and identify objects that require a substantial overhead to run. You can build a list of these objects that are expensive to run, and then cache them for a period of time using fragment caching.
For example, say your page default.aspx consists of three user controls. After looking over the code, you identified that you can cache one of them. You can simply add the caching directive to the top of the user control: <%@ Control %><%@ OutputCache Duration=”5” VaryByParam=”none” %>
Now keep in mind that the actual page that contains the control is not cached, only the user control. This means that the default.aspx page will be rendered each and every page request, but the user control is only ‘run’ every 15 minutes.
Data Caching
So we know that we can cache an entire page, or a fragment of a page by caching down to the user control level. Wouldn’t it be great if we could cache down to the object level? The good news is you can with ASP.NET Data caching.
The cache consists of a dictionary collection that is private to each application in memory. To insert items in the cache simply provide the collection with a unique name: Cache[“someKey”] = myObject;
Retrieving the object form the cache: myObject = (MyObject)Cache[“someKey”];
It is a good time to point out that you should always remember to check for null, and be sure to caste to your datatype.
Cache Configuration
If you are familiar with how caching worked in ASP.NET 1.0, you realize that managing all the cache directions for all your pages could potentially get out of hand. ASP.NET 2.0 introduces Cache Profiles that helps you centrally manage your cache. Cache settings can be inherited by your pages, and overridden if required by using the OutputCache directive.
The page directive looks pretty much the same, expect this time it references a cache profile that you defined in your web.config file.
Disable auto id’s when copying ASP.NET controls
For example, look at this code:
I took the first line, copied it and pasted it, and got the second line. It’s rare that I need to reference a validator in my CodeBehind.
Here is the option to disable this functionality in the Visual Studio options
(Tools->Options):

Simply uncheck the “Auto ID elements on paste in Source view” option.
Tuesday, June 17, 2008
Date/Time Conversions Using SQL Server
1 | select convert(varchar, getdate(), 1) | 01/07/08 |
2 | select convert(varchar, getdate(), 2) | 08.01.07 |
3 | select convert(varchar, getdate(), 3) | 07/01/08 |
4 | select convert(varchar, getdate(), 4) | 07.01.08 |
5 | select convert(varchar, getdate(), 5) | 07-01-08 |
6 | select convert(varchar, getdate(), 6) | 07 Jan 08 |
7 | select convert(varchar, getdate(), 7) | Jan 07, 08 |
10 | select convert(varchar, getdate(), 10) | 01-07-08 |
11 | select convert(varchar, getdate(), 11) | 08/01/07 |
101 | select convert(varchar, getdate(), 101) | 01/07/2008 |
102 | select convert(varchar, getdate(), 102) | 2008.01.07 |
103 | select convert(varchar, getdate(), 103) | 07/01/2008 |
104 | select convert(varchar, getdate(), 104) | 07.01.2008 |
105 | select convert(varchar, getdate(), 105) | 07-01-2008 |
106 | select convert(varchar, getdate(), 106) | 07 Jan 2008 |
107 | select convert(varchar, getdate(), 107) | Jan 07, 2008 |
110 | select convert(varchar, getdate(), 110) | 01-07-2008 |
111 | select convert(varchar, getdate(), 111) | 2008/01/07 |
TIME FORMATS | ||
8 or 108 | select convert(varchar, getdate(), 8) | 18:12:39 |
9 or 109 | select convert(varchar, getdate(), 9) | Jan 7 2008 6:13:02:140PM |
14 or 114 | select convert(varchar, getdate(), 14) | 18:13:21:093 |
You can also format the date or time without dividing characters, as well as concatenate the
date and time string:
Sample statement | Output |
select replace(convert(varchar, getdate(),101),'/','') | 01072008 |
select replace(convert(varchar, getdate(),101),'/','') + replace(convert(varchar, getdate(),108),':','') | 01072008181409 |
