Monday, November 15, 2010

Visual Studio 2010 and SQL Server 2008

When creating ASP.NET 4.0 application using VS 2010 with SQL Server 2008, by default, the ASP.Net 4.0 users does not have the right permission to access SQL server database. Following the below step to assign the proper user.
1. In IIS Manager, go to the website and in the IIS section, select the Authentication icon and open it. You should have a list of authentications. Right-click on Anonymous Authentication and select Edit. this should be set to Application pool identity.
2. Go to the Application Pools and select the pool your website is in. In the listing, you should see the Identity column. It should be set to LocalSystem. If not, then right-click on the pool and select Advanced Settings. Under the Process Model section, set the Identity to NetworkService by clicking the button on the right-side of the text box and in the dialog, using the Built-in account dropdownlist

In addition, the sqlDatasource wizard may get an invalid object error. Apply the following steps to correct the error.
SQL server 2005 uses a "Schema.Table" naming convention that VS2010 SQLDataSource Wizard doesn't pickup. To workaround the issue, instead of "Specify columns from table or view", select "Specify a custom SQL statement...". On the next screen, use the "Query Builder" to generate the SQL statement. It will pickup the schema name.

Javascript: Passing local variable to setTimeout callback as parameter

In javascript, setTimeout is the only way to make asynchronous method call, however there is a limitation to use setTimeout method - the local variable can not be used as parameter in the callback method. The reason is once the callback is invoked by timer, the original method that calls setTimeout already returns and all the local variable are invalid.
A workaround for this issue is using javascript inner function, the outer functions local variable is visible to the inner function, even if the outer function returns.

Function function1( param )
{
var localVar = param + " is a local variable";
Function innerFunc()
{
alert( localVar );
}
window.setTimeout( innerFunc, 1000);
}

Sunday, November 14, 2010

Recursive FindControl for ASP.Net page

Page.FindControl method only checks the input ID from the immedidate child controls, while in most case the caller wants to get the control from multiple level of child control. It is more useful use the following method to find a control from a page.

public Control FindControlIterative(Control root, string id)
{

if (root.ID == id)
return root;
foreach (Control child in root.Controls)
{
Control childCtl = FindControlIterative(child, id);
if (childCtl != null)
return childCtl;
}


return null;
}

Saturday, November 13, 2010

Html server control or ASP.Net server control

When choosing between html server control or ASP.Net server control, one consideration is whether the appliation needs to support browsers with differetn rendering ablity. If so, it is better to choose ASP.Net server controls over html server controls. By doing so, ASP.Net framework can automatically convert any unsupported ASP.Net server controls into compatible html controls based on the target browser's rendering ability.

On the contrary, if html server controls are used, ASP.Net will not make this conversion based on browser's rendering ability.

This is more important when creating web pages targeting mobile browsers.