Monday, May 26, 2008

.NET Regex Balanced Grouping

Last time when I tried to understand balanced grouping from MSDN, it hurt my brain badly. Thanks to this guy, though he is not .NET coder, he explained balanced grouping very nicely. A must read for Regex lovers.

Friday, May 23, 2008

Google plays foul

Google hands over user information


Personally, I feel, if you don't like someone, you should not criticize him in filthy language. Being in democracy doesn't mean you strike someone below the belt, specially when someone is top public figure. In 110 crore population, all may not happy with Sonia's government. People should express their view in ethical way.

Whats wrong with Google. I am agree that boy should have not used foul language, but who gives rights to Google to hand over the information. This was not any terrorist activity. If Google thinks, user is using foul language, he should have simple blocked the user. Activity done by Google, simply indicates that they have nothing to do with providing a free social networking platform to public, its all about BUSINESS, its all about getting POWER, its all about MONEY. I read somewhere IBM helped Nazis in holocaust by providing data of Jews population. Google has not done that bad right!!! Well done Google!!.

Friday, February 08, 2008

You have seen Y2K, worse to come!!!

Y2038 Bug!!

According to computer scientists, Unix and Linux system will be massively affected. But of course, still having plenty of time. Surprisingly Perl 10 is Y2038 safe but thats not the reason why I love this language.

Read this:

It is explained that Unix and similar operating systems do not calculate time based on the Gregorian calendar. Instead, they are known to simply count time in seconds from their arbitrary "birthday", that is, GMT 00:00:00, Thursday, January 1, 1970. The accepted practice among software programmers is to use a 32-bit variable for this number (32-bit signed time_t). The largest possible value for the end integer in this calculation is 2**31-1 = 2,147,483,647. So, 2,147,483,647 seconds after Unix's birthday falls on Tuesday, January 19, 2038. And one second later, theoretically Unix systems will revert to their birth date (like an odometer switching back from 999999 to 000000).

See original Post.

Thursday, January 17, 2008

Definitions related with SQL Server

DML

DML is abbreviation of Data Manipulation Language. It is used to retrieve, store, modify, delete, insert and update data in database.

Examples: SELECT, UPDATE, INSERT, DELETE

DDL

DDL is abbreviation of Data Definition Language. It is used to create and modify the structure of database objects in database.

Examples: CREATE, ALTER, DROP statements

DCL

DCL is abbreviation of Data Control Language. It is used to create roles, permissions, and referential integrity as well it is used to control access to database by securing it.

Examples: GRANT, REVOKE statements

TCL

TCL is abbreviation of Transactional Control Language. It is used to manage different transactions occurring within a database.

Monday, January 07, 2008

Fun with WWW::Mechanize

WWW::Mechanize is really a very handy module if you want to automate web page related tasks. Following PERL script downloads the Perl Cook Book pages in a directory. You can put this Perl script in a scheduler and all pages will be collected in few days.

use strict;
use WWW::Mechanize;

my $mech = WWW::Mechanize->new();
my $ouputDir = 'E:\work\documents\PerlCookBook';
my @urls = (
'http://www.perl.com/cookbook/perlckbk2/solution.csp?day=1',
'http://www.perl.com/cookbook/perlckbk2/solution.csp?day=2',
'http://www.perl.com/cookbook/perlckbk2/solution.csp?day=3',
'http://www.perl.com/cookbook/perlckbk2/solution.csp?day=4',
'http://www.perl.com/cookbook/perlckbk2/solution.csp?day=5'
);

foreach my $url (@urls) {
GetPage($url);
}

sub GetPage {
my $url = $_[0];
$mech->get($url);
if ( $mech->success() ) {
my $content = $mech->content();
if ( $content =~ /\<h2 class="head1"\>(.*)?\<\/h2>/ ) {
my $title = $1;

#$title =~ s/\s+//g;
my $filePath = $ouputDir . "\\PCB_" . $title . ".html";
if ( !-e $filePath ) {
open( WR, ">$filePath" ) or die "Can't create file $filePath\n";
print WR $content;
close WR;
print "File was created successfully\n";
}
else {
print "File already exists\n.";
}
}
}
else {
print "Reqeust was not successful\n";
}
}

Friday, January 04, 2008

Recompilation in SQL Server 2005

Before executing SQL statement, SQL server checks for the validity and correctness of the query and generates the execution plan if it's not all already available. No need to say compilation is very CPU intensive process. Before executing the already compiled SQL statement, SQL server perform several other checks and if any of these checks fails, SQL server perform again compilation of SQL statement. Such compilations are known as recompilations. Following are some reasons for recompilation:

  1. Schema change
  2. Statistics change
  3. Deferred compile
  4. SET option change
  5. Temporary table change
  6. Stored procedure created with RECOMPILE query hint or OPTION(RECOMPILE)

In SQL Server 2000, if you need to recompile a portion of stored procedure, you need to compile the whole stored procedure, however in SQL Server 2005, you can perform recompilation at statement level. Less recompilation means less CPU consumption and less contention.

For more information, see Identifying Recompilation

Wednesday, January 02, 2008

Considerations while designing own types

Though Value Types are created on stack and in some situation gives better performance as compare to Reference Types, we should not tempt with choosing Value Type for every situation. General misconception is that if we are not having complex functionality, we should choose Value Type. We should not forget that size of instance also does matter in copy operation. In functions, Value Types are passed by copying the instance as a parameter. Similarly if function is returning Value Type, that is also copied in caller function's memory space. We can make our type as Value Type in following situations:

  1. Type has no members that modify any of the type's instance field. In other words, type is immutable.
  2. Type should not lie in inheritance chain. That means, it should inherit from any other type and also any other type should derived from it.
  3. Instances of the type are small in size (16-20 bytes or less)
  4. Instances of the type are large (greater than 20 bytes) and are not passed as argument or return value in functions.

Most promising technologies in 2008

Welcome 2008. I see following technologies which can take a big leap in 2008:

  1. Microsoft Silverlight framework – facilitates you to run a desktop like application in Internet Explorer.
  2. Ultra Mobile PC (UMPC)- Size of a your hardback book, weight less than a Kg, no hard drive, only flash memory of approx 4 Gb. Visit Asus EEE
  3. ADSL2+: Broadband speed up to 24 Mbps (Of course not in India J)
  4. IPTV: Progress in this field was hampered in past. But now with the increase in broadband speed, this technology is ready to take off.

Monday, December 31, 2007

Bye Bye 2007

2007 was really a vibrant year for me. New Company, new projects, new peoples, new challenges, achievements, awards, anxiety and excitement were some few colors of 2007, but above all, becoming a father will make 2007 is the most memorable year of my life. I am very thankful to my family and friends who gave their support in every up and down phase of my life. I don't believe in resolutions but keeping the tradition, I want to make this resolution and I will my try my best to follow this resolution throughout the year – I will start my sketching again. I am an artist by instinct. I left sketching few years back when I went college for engineering. I know this can't be excuse but really that time I just wanted to concentrate on my career. This unusual blog post is note to me and it will keep reminding me of my New Year resolution.

Sunday, December 30, 2007

RedGate SQL Data Generator

RedGate launched a new tool for generating SQL server for dummy/test data.

Features:
- Full SQL Server 2000 and 2005 support
- All data types supported except CLR types
- Pre & Post Scripts execution
- Command-line access version
- Import data from CSV or SQL tables
- Customizable generator settings that allow configuring the amount of nulls, unique values, minimum and maximum values, etc..
- Diverse range of inbuilt generators

Please visit here for more detail.

By the way, I have already created few PERL scripts to achieve same type functionality to some extent.

Thursday, December 20, 2007

Yet another good article on Cluster and Non cluster index

Take the example of a phone book. The actual data - that is, the name, address and phone number records - is ordered by the name. If you want to look up Joe Bloggs's phone number, you open the book somewhere near the middle, maybe see the names there start with "M", but "Bloggs" is before "M", so you go a bit earlier in the book. You keep narrowing it down until you find the entry labelled Bloggs, and that's it - all the data for that record is right there. That's a bit like a clustered index.

On the other hand, a book might have a table of contents, sorted alphabetically. If you want to find out about llamas, you search the contents for llamas, which probably then gives you a page number, at which point you go to the page, and there's the data about llamas. The difference here is that you've had to do an extra bit of indirection - following the page number pointer - in order to get to the data. You can probably now see that while you can have as many tables of contents, ordered in any way you like, one set of data can only be physically arranged in one way. This means you can have many non-clustered indexes, but only one clustered index on a table.

Full Article

Wednesday, December 19, 2007

Perl 5.10 is out

Perl is no longer teenager. Its Perl's 20th Bithday and its coming in its brand new suite.
Checkout new features of 5.10 in this document.

Of course you have to wait for 5.10 and chances are you never get it but till then Perl 5 is rocking.

Tuesday, December 18, 2007

String - Reference or Value Type

In my interviews, I observed that 90% candidates don't have clear understanding of Reference or Value Types in .NET. I am not going to explain these two things here but want to clear one interesting point here.

Though String in .NET is Reference type object but it works as Value type.
Following code snippet will print "test"

static void Main(string[] args)

{

string str = "test";

ChangeString(str);

Console.WriteLine(str);

Console.ReadLine();

}

static void ChangeString(string str)

{

str += " Changed";

}





Following some line will better explain the things:
although strings are reference types, they work rather more like value types. When one string is set to the value of another, eg
string s1 = "hello";
string s2 = s1;

Then s2 does at this point reference the same string object as s1. However, when the value of s1 is changed, for instance with

s1 = "goodbye";

what happens is that a new string object is created for s1 to point to. Hence, following this piece of code, s1 equals "goodbye", whereas s2 still equals "hello".

The reason for this behaviour is that string objects are 'immutable'. That is, the properties of these objects can't themselves change. So in order to change what a string variable references, a new string object must be created.


Restrictions on TempDB

I found it on internet. Thought you guys like to know:

  • Adding filegroups.
  • Backing up or restoring the database.
  • Changing collation. The default collation is the server collation.
  • Changing the database owner. tempdb is owned by dbo.
  • Creating a database snapshot.
  • Dropping the database.
  • Dropping the guest user from the database.
  • Participating in database mirroring.
  • Removing the primary filegroup, primary data file, or log file.
  • Renaming the database or primary filegroup.
  • Running DBCC CHECKALLOC.
  • Running DBCC CHECKCATALOG.
  • Setting the database to OFFLINE.
  • Setting the database or primary filegroup to READ_ONLY.

Monday, December 17, 2007

SQL Helper - New functionalities (2)

In my last posts, I talked about SQL helper developed by me. Following are two new functionalities added recently to application:

  1. Create database documentation in word or html format
  2. Do Uppercase SQL keywords option in Stored procedures and functions.

Tuesday, November 27, 2007

Part 1: Using XML in SQL Server 2005

Suppose you want to generate following output from stored procedure :

1 <Companies>

2 <Company id="1" name="Company A">

3 <Address>

4 <Street>101 A St.Street>

5 <City>HaysCity>

6 <State>KansasState>

7 <Zip>67601Zip>

8 Address>

9 <Contacts>

10 <Contact id="1" name="Bob Black">

11 <ContactInfo email="bb@companyA.com" phone="(123) 456-7890" />

12 <Login uid="bb" pwd="bb7" />

13 Contact>

14 <Contact id="2" name="Bob Brown">

15 <ContactInfo email="bbn@companyA.com" phone="(123) 456-7891" />

16 <Login uid="bn" pwd="bn1" />

17 Contact>

18 <Contact id="3" name="Bob White">

19 <ContactInfo email="bw@companyA.com" phone="(123) 456-7892" />

20 <Login uid="bw" pwd="bw2" />

21 Contact>

22 Contacts>

23 Company>

24 Companies>



Following is the code snippet to achieve above output from SQL query

select

Company.Cmp_id "@id",

Company.[Name] "@name",

Company.Address "Address/Street",

Company.City "Address/City",

Company.State "Address/State",

Company.Zip "Address/Zip",

(

select

Contact.Cnt_id "@id",

Contact.[Name] "@name",

Contact.Email "ContactInfo/@email",

Contact.Phone "ContactInfo/@phone",

Contact.UserName "Login/@uid",

Contact.Password "Login/@pwd"

from

Contact

where

Contact.Cmp_id = @CompanyId

order by

Contact.[Name]

for xml path ('Contact'), root('Contacts'), type

)

from

Company

where

Company.Cmp_id = @CompanyId

for xml path ('Company'), root('Companies'), type

Monday, November 12, 2007

Showing enum as dropdown or listbox

Following function returns ListItemCollection for given enum type. This ListItemCollection can be bound to dropdown or listbox:

public static System.Web.UI.WebControls.ListItemCollection GetListItemsFromEnum(Type typeOfEnum)
{
System.Web.UI.WebControls.ListItemCollection lstColl
= new System.Web.UI.WebControls.ListItemCollection();
foreach (string name in Enum.GetNames(typeOfEnum))
{
System.Web.UI.WebControls.ListItem li
= new System.Web.UI.WebControls.ListItem();
li.Text = name;
li.Value = ((int)Enum.Parse(typeOfEnum, name)).ToString();
lstColl.Add(li);
}
return lstColl;
}

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