Sunday, August 24, 2008

C#: Handling Strings

Often, in the real time application development, we come across Strings that need to be handled differently based on the need.

String reverse:
One such function is the String reverse. To accomplish this in C#, we need to add reference to the Microsoft.VisualBasic assembly and you can write the code to reverse the string as follows -

String str = "Welcome";
str = Microsoft.VisualBasic.Strings.StrReverse(str);
Response.Write(str);

Word Count:
String str = "Welcome to ASP.NET with C#.";
System.Text.RegularExpressions.MatchCollection wordCountColl
= System.Text.RegularExpressions.Regex.Matches(str, @"[\S]+");
Response.Write(wordCountColl.Count.ToString());

Character Count:
String str = "Welcome to ASP.NET with C#. Welcome to ASP.NET with C#.";
System.Text.RegularExpressions.MatchCollection charCountColl
= System.Text.RegularExpressions.Regex.Matches(str, @".");
Response.Write(charCountColl.Count.ToString());

Thursday, August 21, 2008

JS: Simple Regular expressions

Regex to validate Alpha-Numeric with whitespace:

var reg = /^[a-zA-Z0-9\s]+$/;
var val = document.getElementById("t1").value;
if(reg.test(val))
alert("Contains only Alphanumeric.");
else
alert("Contains other than alpha numeric.");

here "t1" is the Text control



Regex to trim the white spaces before and after a content:

function trim ( strToTrim )
{
return strToTrim.replace(/^\s+|\s+$/g,"");
}

var val = document.getElementById("t1").value;
alert("-"+trim(val)+"-");

Monday, August 11, 2008

There may be times to access the parent window’s control in the child window. Here is the sample code which does this -

Assume that you have a control named 'txt1' of text type in the Parent.html, and you open a new window on a button click as,

window.open("Child.html","Child","height=400px,width=500px,resizable=yes,scrollbar=no,status=no");

and you can access the control 'txt1' in the Child.html as,
var parentWinCtrl = window.opener.document.getElementById('txt1');

and you can assign or retrieve the value from the parentWinCtrl variable,
to assign - parentWinCtrl.value = "something";
to access - var parentVal = parentWinCtrl.value;

Saturday, August 9, 2008

JavaScript: Closing all child windows when parent window is closed

Consider that if you have opened multiple new windows thru window.open() method of JS and if you want all of the child windows to be closed here is the sample code for that -

Here, when you open a new window assume that you are opening it thru NewWin(..) function.

var childWin=new Array();
var childCnt = 0;
function NewWin(obj,name)
{
childWin[childCnt++] = window.open(obj,name,’height=200,width=400′);
}

function CloseAllChildWin()
{
for( cnt=0; cnt < childWin.length;cnt++)
{
if(childWin[cnt])
childWin[cnt].close();
}
}


Now register the onunload event to the BODY tag with the function CloseAllChildWin(). So, if you have opened any windows thru the NewWin() function, all the opened windows will be closed when you press the F5 key / Refresh button / Close button.

There is another way to handle, the child windows should be closed only when the 'X' / Close button is clicked. To achieve this just modify the CloseAllChildWin() as follows -


function CloseAllChildWin()
{
if( event.clientX < 0 && event.clientY < 0 )
{
for( cnt=0; cnt < childWin.length;cnt++)
{
if(childWin[cnt])
childWin[cnt].close();
}
}
}

MS SQL : Using Temp table between Stored Procedures

Suppose we want the Temporary tables to be shared across stored procedures, it can be achieved in the following way -

Create an SP which is called by other SP:
CREATE PROC InsertRecords
AS
BEGIN
INSERT INTO #GlobalTemp values(1,’Akash’)
INSERT INTO #GlobalTemp values(2,’Arun’)
INSERT INTO #GlobalTemp values(3,’Chitra’)
END

Create an SP which calls the above SP:
CREATE PROC CallingSP
AS
BEGIN
CREATE TABLE #GlobalTemp(EmpId INT, EmpName VARCHAR(20))
EXEC InsertRecords
SELECT * FROM #GlobalTemp
END

In the first SP, we are just inserting the values in the temp table ‘#GlobalTemp’, but it is not declared there, instead. The ‘#GlobalTemp’ table is declared in the SP ‘CallingSP’ and is used by the SP ‘InsertRecords’.

Now if you execute -
EXEC CallingSP

you will get the output as follows -
1 Akash
2 Arun
3 Chitra