Friday, October 12, 2007

ASP.NET - Performing long running process asynchronously with out duplication

Sometimes in our web pages we need to perform some long running process. You must be agree that instead of watching wait cursor for long running process, user can do some other activity in same web application with out blocking. Those who do not know AJAX or want to do some quick fix in their ASP.NET code, following is the code which targets following objectives:
  1. User should not be blocked on web page, he can perform some other activity. That means after post back, process should be run in background and user instantly gets control after starting the process.
  2. Web server connection time out for long running process.
  3. Even instantly getting control after starting the process, user should be able to start the same process only after completion of previous process started by the same user. I used mutex for it.
Following is the code:
using System.Threading;

// Button click event
protected void btnLoad_Click(object sender, EventArgs e)
{
try
{
ThreadPool.QueueUserWorkItem(new WaitCallback(LoadLog));
}
catch (Exception ex)
{
lblMessage.Text = "Your job could not be submittited due to following error:
" + ex.Message;
lblMessage.CssClass = "Error";
}
lblMessage.Visible = true;
}

// In above code LoadLog is the function which starts long running process
// Following is the code for LoadLog
private void LoadLog(object state)
{

string mutexKey =Thread.CurrentPrincipal.Identity.Name;
mutexKey = mutexKey.Replace(@"\", "_"); // Required otherwise mutex will consider its a file system path
Mutex mMutex = new Mutex(false, mutexKey + "_LoadLogs");
mMutex.WaitOne();
try
{
//Here your long running process
Thread.Sleep(1000000); // Don't try this line, just for example.
}
finally
{
mMutex.ReleaseMutex();
}
}

Thursday, October 11, 2007

Sproc Vs UDF

I got this from SQL server magazine. Yeah.. I know this is very basic thing but honestly speaking, I did not know about point 3, so this is note for myself:
  1. A UDF must return a value-a single result set. A stored procedure can return a value-or even multiple result sets-but doesn't have to.
  2. You can use a UDF directly in a SELECT statement as well as in ORDER BY, WHERE, and FROM clauses, but you can't use a stored procedure in a SELECT statement.
  3. A UDF can't use a nondeterministic function such as GETDATE(), NEWID(), or RAND(), whereas a stored procedure can use such functions. A nondeterministic function is one that can return a different result given the same input parameters.
  4. A UDF can't change server environment variables; a stored procedure can.
  5. A UDF always stops execution of T-SQL code when an error occurs, whereas a stored procedure continues to the next instruction if you've used proper error handling code.
  6. A UDF can not execute DDL statements. It can perform insert, update and delete operations on only temporary tables not on permanent tables.
  7. You can't have trannsactions in UDF
  8. You can call stored procedures with in a stored procedure but not with in a function. Ofcourse you can call functions with in a function.

Friday, October 05, 2007

New version of SandCastle is available

The latest version of SandcastleGUI is available for download at http://www.inchl.nl. This release of SandcastleGUI targets the September CTP build of Microsoft's Sandcastle.

Microsoft did a great job fixing over 50 issues that were reported by customers, also they fixed over 500 work items to support the Orcas (Visual Studio 2008) RTM.

Microsoft's Sandcastle September 2007 CTP build does no longer contain the .NET framework reflection data. Because this data can be generated in runtime it didn't make sense to include over 500 MB of reflection data into an installation file.

Also for each version of the .NET framework and the chosen Sandcastle template this reflection data is different.

SandcastleGUI introduces a new feature that allows caching of this data because the generation of it is very time consuming.

Thursday, October 04, 2007

Implementing timer operation in window service

Following code snippet shows how can we perform a operation on timer event in window service:


public partial class MyService : ServiceBase
{
private System.Timers.Timer timer2;
private bool IsProcessRunning = false;

public MyService()
{
InitializeComponent();
this.ServiceName = "MyService";
timer2 = new System.Timers.Timer();
timer2.Enabled = false;
timer2.Interval = "10000";
timer2.Elapsed += new System.Timers.ElapsedEventHandler(timer2_Elapsed);
}

protected override void OnStart(string[] args)
{
timer2.Enabled = true;
timer2.Start();
}

protected override void OnStop()
{
// TODO: Add code here to perform any tear-down necessary to stop your service.
}

void timer2_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
if (!IsProcessRunning)
{
try
{
IsProcessRunning = true;
// Add process here
InsertRecord();
}
catch (Exception ex)
{
this.EventLog.WriteEntry(ex.Message, EventLogEntryType.Error, 1000);
}
finally
{
IsProcessRunning = false;
}
}
}
}

Wednesday, October 03, 2007

Javascript debugging in Visual Studio

In order to debug javascript code in ASP.NET application you need to following thing:
  1. Enable script debugging option for Internet Explorer.
  2. Open inetmgr and right click on the web application project. Set true to option "Enable ASP client side debugging".
  3. There is a quick and dirty way - Add "debugger;" command as the first line of the javascript function which you want to debug.

Monday, October 01, 2007

Performing case sensitive comparison in SQL 2005

Using following query, you can perform case sensitive comparison for strings in SQL 2005 even if your database was configured for case insensitive comparison:

SELECT UserId, email
FROM aspnet_membership
WHERE email = 'test@test.com' COLLATE SQL_Latin1_General_CP1_CS_AS