How To: Enable Microsoft ASP.NET Ajax Extension V1.0 on an existing site

The fastest way to add ajax fuctionality to your site -
Here is a good explanation of the process of enabling ajax in an asp.net page.
There is also integeration of CTP on an existing site.

How to delete row in a DataTable with C# or VB.NET

When you use a DataRow.Delete() method the row is not actually deleted. It is only marked to be deleted after calling DataTable.AcceptChanges(). It is not good idea to remove row with DataTable.Rows.Remove or RemoveAt methods. They forces row removing and causes by iterating the following exception:
'Collection was modified; enumeration operation may not execute'
One more thing to be sure that the iterating will be performed succesfully is to iterate through returned by DataTable.Select() rows (not DataTable.Rows).

For Each row As DataRow In dt.Select()
   If (bool_statement) Then
     row.Delete()
   End If
Next

dt.AcceptChanges

If adapter is used changes can be commited by Update method. All rows with DataRow.RowState of 'Deleted' will be deleted.

How to use SQL SERVER 2005 with SQL EXPRESS at the same time.

Error message received by Visual Studio 2005:
"Connections to sql server files (*.mdf) require sql server express 2005 to function properly"
I had installed Visual Studio 2005 and SQL Server 2005 Developer Edition when I received it. To solve the porblem I installed other instance of SQL Server - Express Edition. But to get VS to work with it I had to change its instance name in the VS options. Tools -> Options - > Database Tools -> Data Connections. Now I can work with the two sql server instances - Developer Edition and Express Edition.


To change an existing instance of Microsoft SQL Server 2005 to a different edition of SQL Server 2005, you must run SQL Server 2005 Setup from the command prompt and include the SKUUPGRADE=1 parameter.

Checking for existing Javascript variable

Somethemes it is necessary to know if the variable is already defined. For example when postback methods which register client script block are used.

function isSet( v )
{
return( typeof( v ) != 'undefined' );
}

Default Web Site on IIS doesn't work until changing the default port.

It is a very strange problem that occurs sometimes.
"IS Unexpected error 0x8ffe2740"
Today I have just found the reason - Skype is using the default port 80 for alterantive connection.
Resolve that by:
Skype File, Options, Advanced, Connection
Uncheck "Use Port 80 as an alternatives for incoming connections" and restart default website or IIS

How to get random number or generate random password with VB.NET or C#

The simplest way to get random number is to get instance of the Random class and use the "Next" method.
Dim rand As Random=new Random()
rand.Next() gives you a random nonnegative number
rand.Next(23) gives you a random nonnegative number less than integer value (23 for the example)
rand.Next(23, 89) gives you a random nonnegative number in the specified range

With this class you can generate also double values and bytes.

Here is an way for generating random password using guid.
So simple, so effective.

Public Function GetRandomPassword(ByVal length As Integer) As String
Dim rand As String = System.Guid.NewGuid().ToString()
rand = rand.Replace("-", String.Empty)

If length <= 0 OrElse length > rand.Length Then
Throw New ArgumentException("Length must be between 1 and " & rand.Length)
End If

Return rand.Substring(0, length)
End Function

Fire Default button on enter on an Asp.NET page

I found two ways to get this to work. By Default the first button in the form is the default button. When you push Enter an fire event of this button occur. Current solution is enough to get Login control on asp.net form to work. Here is the code:


Dim ctl As Control = Login1.FindControl("LoginLinkButton")
If Not (ctl Is Nothing) Then
Login1.Attributes.Add("onKeyPress", "javascript:if (event.keyCode == 13) {__doPostBack('" + ctl.UniqueID + "',''); return false;}")
End If

Fix: return false will break the firing of other buttons. Just to ensure right behaviour.

It works fine for all browsers.
There is one more way to do this but unfortinately it doesn't work with firefox.

Login1.Attributes.Add("onkeypress", String.Format("javascript:return WebForm_FireDefaultButton(event, '{0}')", ctl.ClientID))

How to install PHP on IIS

PHP installation on IIS 7.0 (Windows VISTA)
I will not explain how to do it, just see this good article:
http://blondr.blogspot.com/2006/11/set-up-iis-7-w-mysql-and-php-5.html
It includes also the settings for MySQL and php session' permission.


PHP installation on IIS 5.1

1) add to path c:\PHP
2) rename php.ini-recomended to php.ini

doc_root = c:\inetpub\wwwroot // for IIS/PWS
doc_root = c:\apache\htdocs // for Apache
extension_dir = "c:\php\ext"
cgi.force_redirect = 0
set browscap to c:\windows\system32\inetsrv\browscap.ini

If you want to use the CGI binary, do the following:

* Under 'Home Directory', 'Virtual Directory', or 'Directory', do the following:
* Change the Execute Permissions to 'Scripts only'
* Click on the 'Configuration' button, and choose the Application Mappings tab. Click Add and set the Executable path to the appropriate CGI file. An example PHP 5 value is: C:\php\php-cgi.exe Supply .php as the extension. Leave 'Method exclusions' blank, and check the 'Script engine' checkbox. Now, click OK a few times.
*

Set up the appropriate security. (This is done in Internet Service Manager), and if your NT Server uses NTFS file system, add execute rights for I_USR_ to the directory that contains php.exe / php-cgi.exe.

To use the ISAPI module, do the following:

* If you don't want to perform HTTP Authentication using PHP, you can (and should) skip this step. Under ISAPI Filters, add a new ISAPI filter. Use PHP as the filter name, and supply a path to the php4isapi.dll / php5isapi.dll.
* Under 'Home Directory', 'Virtual Directory', or 'Directory', do the following:
* Change the Execute Permissions to 'Scripts only'
* Click on the 'Configuration' button, and choose the Application Mappings tab. Click Add and set the Executable path to the appropriate ISAPI DLL. An example PHP 5 value is: C:\php\php5isapi.dll Supply .php as the extension. Leave 'Method exclusions' blank, and check the 'Script engine' checkbox. Now, click OK a few times.
* Stop IIS completely (NET STOP iisadmin)
* Start IIS again (NET START w3svc)
3) add user rights to php directory (IIS user - modify, IUSR_MASNINENAME - read)

How to get window size and scroll by javascript

Here is a nice explanation of the browser incompatibility with getting window size:
http://www.howtocreate.co.uk/tutorials/javascript/browserwindow

var myWidth = 0, myHeight = 0;
if( typeof( window.innerWidth ) == 'number' ) {
//Non-IE
myWidth = window.innerWidth;
myHeight = window.innerHeight;
} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
//IE 6+ in 'standards compliant mode'
myWidth = document.documentElement.clientWidth;
myHeight = document.documentElement.clientHeight;
} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
//IE 4 compatible
myWidth = document.body.clientWidth;
myHeight = document.body.clientHeight;
}

It works fine for all browsers.