Total Pageviews
Monday, May 17, 2010
ASP.NET basic page life cycle
1. PreInit
2. Init
3. InitComplete
4. PreLoad
5. Load
6. LoadComplete
7. PreRender
8. PreRenderComplete
9. SaveStateComplete
10. Unload
Capturing Key stroke and Do the post back
Well the first step in the process is to capture the Key press event that has happened when the user has pressed some key while he is using you web page.
The way to do this is something like this.
The KeyID will provide you with a number that indicates which key was pressed. You can build a logic based on the value of KeyID. You can also use the key combinations and like “cntrl+S” etc.
The below code is written and tested to do post back for various keys press events.
First add this JavaScript to your page.
This script does a post back and passes the name of the key pressed as the value of the “Request["__EVENTARGUMENT"]” key in the Request object.
For making the “__doPostBack” to work we will have to do some register the postback event reference during page load. The code below explains how it is done
protected void Page_Load(object sender, EventArgs e)
{
Page.ClientScript.GetPostBackEventReference(this, "");
string eventArgs = Request["__EVENTARGUMENT"];if(!string.IsNullOrEmpty(eventArgs))
{
switch (eventArgs)
{
case "F7":
DoF7();
break;
case "F8":
DoF8();
break;case "F9":
DoF8();
break;
case "F10":
DoF8();
break;
}
}
}
Based on the value received in the Request["__EVENTARGUMENT"] we can take appropriate action during postback.
Load Balancing
In one of our project the site usage of site was very heavy and we need to migrate it to load balancing server. I have never configured the sites in the load balancing server but it was quite interspersing experience Here are the some points which we need to take care while we move asp.net sites into the load balancing environments. So first we will see what is load balancing.
Following is a load balancing definition from the Google.
In computer networking, load balancing is a technique to distribute workload evenly across two or more computers, network links, CPUs, hard drives, or other resources, in order to get optimal resource utilization, maximize throughput, minimize response time, and avoid overload.
Following are the points which you need to take care when you are deploying your asp.net sites into load balancing server environments.
Machine Key Should be same for both servers: View state and session both are depends on the machine key. If you machine key is not same then you will have problems related to session and view state you may loose your session and view state in between request during post backs. If machine key will not be same then its possible that you can get strange result in Ajax requests. There is a machine key section in web.config where you can specify machine key.
- <machineKey validationKey='C44B8B7C521CB5BC7E602BAE6118AA44CD690C7304817129DA27C17E800132A
- 1BD946C6D9AD12F0A5B342840C7D130564195428160B7466146938CA9E3A62686' decryptionKey='0E9DF2DA7F210B84087690FF0BF25C905182AD81E16A5FA9' validation='SHA1'/>
Following is a good link to learn how you can configure sessions state in asp.net application.
And here is a good link to configure session on SQL Server.
http://support.microsoft.com/kb/317604
- <sessionState mode="SQLServer" StateConnectionString="tcpip=127.0.0.1:42424"
-
- SqlConnectionString = "data source=SERVERNAME; user id=sa; password=sa"
-
- cookieless="false" timeout="20" />
Caspol Utility: You can use caspol utility to share some resources on between load balancing servers. Like we have file base cache dependency in our application so we have created a central location(A shared folder for cache files) for the both the server and used Caspol utility to give full trust share on that folder for load balancing servers. Following are some of the good links to which will teach how you can use CasPol utility to for asp.net application
http://blogs.msdn.com/shawnfa/archive/2004/12/30/344554.aspx
http://forums.asp.net/p/1119925/1881331.aspx#1881331
File Replication:File Replication is also an important features of load balancing you should have replication enabled on the folders of web application so if you upload anything on one server it should replicated to other sites. Following is good link to understand file replication.
Sticky Sessions: In some scenario Sticky session is very useful. In one page our application we have used extensive Ajax and we need that for each request and partial post back it should stay on one server till request completes.To achieve that we can used sticky session. Following are some good links to know about sticky sessions.
http://dev.fyicenter.com/Interview-Questions/JavaScript/What_does_the_term_sticky_session_mean_in_a_web_.html
http://blogs.msdn.com/drnick/archive/2007/07/13/sticky-sessions.aspx
Hope this will help you on deploying your asp.net on load balancing sites. Following are some good links for understanding load balancing in more details
http://technet.microsoft.com/en-us/library/bb742455.aspx
http://support.microsoft.com/kb/323437
http://technet.microsoft.com/en-us/library/cc754833%28WS.10%29.aspx.
http://edge.technet.com/Media/Network-Load-Balancing-NLB-in-Windows-Server-2008/
Wednesday, May 12, 2010
What is Boxing & Unboxing in C#?
Boxing means converting Value type to a Reference type.
Ex :
int i=100;
object obj=(object) i;
Unboxing means converting Reference type to a Value type.
Ex:
object obj=100;
int i=(int)obj;Usage of SQL SELECT statements
1) Select * from tblTest (Returns all columns/Rows)
2) Select * from tblTest Where TestID=2 (Returns the row/s which TestID has value 2)
3) Select * from tblTest where TestID Between 10 and 20 (Return all rows between 10 and 20, this result includes 10 and 20)
4) Select * from tblTest Where TestCity in ('New York','Washington','California') (Returns all rows which city is NewYork, Washington, california)
5) Select * from tblTest Where TestName Like 'A%' (Return all rows where the name starts letter A)
6) Select * from tblTest Where TestName Like '%A' (Return all rows where the name ends letter A)
7) Select * from tblTest Where TestName Like '[ABC]%' (Return all rows of name start with A / B / C)
8) Select * from tblTest Where TestName Like '[^ABC]%' (Return all rows of name not start with A and B and C)
9) Select (TestName+space(1)+TestCity) as Address from tblTest (Returns single column address, name and city added together with a space)
10) Select * from tblTest Where TestName IS NULL (Return all rows which TestNane has null values)
11) Select * from tblTest Where TestName IS NOT NULL (Return all rows which TestNane has not null values)
12) Select * from tblTest Order By TestID Desc (Sort the result set descending order, Asc or not using any sort Ascending order)
13) Select 'Visual Studio' as IDE, '2010' as Version (Creating memory resident result set with two columns[IDE and Version])
14) Select Distinct TestID from tblTest (Returns unique rows based on TestID)
15) Select Top 10 * from tblTest (Return 10 customers randomly)
16) Select getdate() (Shows the current date)
17) Select db_name() (shows the database name which you are working on)
18) Select @@Servername (Shows name of the server)
19) Select serverproperty ('Edition') (You can pass following ServerName, Edition, EngineEdition, ProductLevel to get current information about the server)
20) Select user_name() (Get current user)
21) Select * into #test from tblTest (Create temporary table #test and insert all records from tblTest)
22) Select Max(TestID) from tblTest (Returns Maximum TestID from tblTest)
23) Select * from tblTest Compute Max(TestID) (Returns two result sets - getting all rows and maximum value of TestID)
24) Select FirstName, LastName, Salary, DOB,
Case Gender
When 'M' Then 'Male'
When 'F' Then 'Female'
End
From Employees
(This Change Gender fields as if M then prints Male and if F then prints Female)
VS Shortcuts
Copy a single line - CTRL+C [Move the insertion point at the begin or end and press space bar, then press the shortcut key combination]
Cut a single line - CTRL+X [Move the insertion point at the begin or end and press space bar, then press the shortcut key combination]
Comment a line - CTRL+K, CTRL+C [Keep the insertion point any where on line and press ]
UnComment a line - CTRL+K, CTRL+U [Keep the insertion point any where on line and press ]
Put/Remove a break point - F9
Remove all break points as once - CTRL+SHFT+F9
Expand/Collapse a region- CTRL+M, CTRL+M
Intellisense - Ctrl+Space [Press CTRL again make the drop down transparent]
Build Solution - Ctrl+Shft+B
Expand All regions - Ctrl+M+P
Collapse All regions - Ctrl+M+O
New Break Point - Ctrl+B