Ajax Error Message:The state information is invalid for this page and might be corrupted

The message is shown when a page with partial postback regions (update panels) is refreshed by F5 or browser's refresh button and then a new partial postback request is performed. In this case the information stored in the page has been changed and causes not to be possible to perform partial postback. For pages like these the page parameter EnableEventValidation must be set to false:

<%@ Page EnableEventValidation="false" ....

Another case is described here: http://support.microsoft.com/kb/323744

How to execute string (custom built sql query ) in a stored procedure

sp_executesql executes string in a stored procedure. It is used in more complex queries which we need to build concatenating strings. The string is run as a batch. The following example gets the TOP records from a table matching predefined conditions.

CREATE PROCEDURE [GetTOPRecords]
@count varchar(6),
@conditions nvarchar(300)
AS
BEGIN

DECLARE @SQL nvarchar(1000)

SET
@SQL='SELECT
TOP '+ @count + ' * FROM RecordsTable
WHERE ' + @conditions

EXEC sp_executesql @SQL

END


The @count and @conditions are passed as parameters. The execution string is built run-time and executed. If you don't need any conditions here just pass the string ' 1=1 ' as @conditions.

How to build unsafe C# code with Visual Studio

Unsafe code may only appear if compiling with /unsafe

This appears as an error when you try to compile assembly which contains unsafe code in release mode.
You must change the compiler options using project's Properties page -> Build -> and check Allow Unsafe Code. Consider if you want to use unsafe code :)))

Javascript: How to check if the variable is a number?

There are some ways for doing this but I think the following is the most easiest and convenient for use:

if (! isNaN(variable)) {
//code here
}


isNaN comes from is Not A Number and can be used for understanding if the variable is a number.

How to maintain the view state of dynamically added control in an asp.net page

First of all you must read these articles:

The viewstate of a dynamically added control can be saved for maintaining the state of the control after postback.
This can be achieved by adding the controls before the Load Viewstate stage (see the page stages in the articles above). Therefore the viewstate information ( control value) from the request will be applied to already added controls as they are defined already in the page.
The right place of adding dynamic controls is Page_Init. Use Page_Init also for defining dynamically master pages and themes.

Understanding AutoEventWireup attribute in Asp.net page. Event handlers.

The AutoEventWireup attribute indicates if the page events are auto-wired up. If the attribute is set to true .Net framework searches automatically for methods which names are created in the way Page_xxxxx (Page_Load, Page_Init etc.) and connects them to the events. Otherwise the developer or the environment must do that using delegates:

this.Load += new System.EventHandler(this.Page_Load);

for all handled page events. Setting AutoEventWireup to false is the better way because of the performance -> the framework uses only the methods with code!

The attribute can be set on several places: in machine.config, web.config and for every page.

Note:
The note is not connected directly to the page events, but to events of postback controls like button, linkbutton etc.
In VB.NET the event handlers can be attached to the events using the keyword "Handles".

Protected Sub LinkButton1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles LinkButton1.Click

This method will handle the event Click and will work like adding the attribute
OnClick="LinkButton1_Click" to the LinkButton tag. But NEVER USE both of the ways at the same because this will invoke the method twice!!
Consider using keyword "Handles" OR OnEvent="", but never use them together.

How to covert System.Drawing.Color to a HTML/CSS color value

The System.Drawing.Color represenation of the color including the alpha color element:
alpha, red, green, blue.
The process is simple - convert the decimal color representation to a hexadecimal, then remove the alpha hexadecimal value from the string and concatenate to '#'.

Dim HTMLColor As String = Hex(color.ToArgb())
HTMLColor = "#" + HTMLColor.Substring(2)

where "color" is an instance of System.Drawing.Color

There is a difference between VB.NET and C# in getting the hexadecimal value as string. The C# fans can use the folloing code:

string HTMLColor = color.ToArgb().ToString("X8");
HTMLColor = "#" + HTMLColor.Substring(2);


The opposite process can be accomplished by ColorTranslator' services:
Dim color As System.Drawing.Color = System.Drawing.ColorTranslator.FromHtml("#e5e5e5")

Javascript string functions

Let's see how to use replace() function for replacing string with another one. The specialty here is the usage of regular expressions to replace the string if it occurs more than once in the source string.

replace('aa','a'); is gonna replace only the first occurrence of the 'aa'.
replace(/aa/g, 'a') will replace all occurrences of the 'aa' string.
replace(/A/i, 'b') will replace the occurrences of 'A' or 'a' with 'b' - case insensitive.

Very common problems are the special character replacement. These characters should be escaped using \.

replace(/\./g, '') will replace all occurrences . with an empty ('') string.