Tuesday, September 16, 2008

ASP.NET: Diff between Session's Abandon() & Remove() & Clear()

We all might know the essentials of Session object which is available in the namespace 'System.Web.HttpContext.Current'. But one may or may not know about the difference between the static methods like -
Abandon(), Remove(key) and Clear()

Abandon() - removes current user from the Session and Global.asax's Session_End() will be fired.
Remove(arg) - clears the particular key's value from the session, but the Session for that user will exists.
Clear() - clears all the entries in the Session object of the particular user, but the Session for that user will exists.

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

Sunday, March 23, 2008

An Introduction to .NET(Part-I)

An Introduction to .NET(Part-I):

.NET is a platform for creating several applications like desktop, distributed, web applications, etc. The main adavntage of .NET is that support for writing an application in different languages like(C#, VB,J#,etc) and it is also an platform independent means it is like Java and can run on any OS.....

For more on this visit the following links

http://www.mono-project.com/Main_Page

http://www.gnu.org/software/dotgnu/

.NET Framework Architecture:.NET Architecture

.NET Languages: you can select any one of the languages you desire for developing the applications like(C#, VB.NET, J# or any other).

.NET Common Language System(CLS): It is the component which enables language interoperability. Common Type System(CTS) is a sub-component of CLS. So to make langauge interoperability, it should adhere to the specification of CLS.

.NET Framework Class Library(FCL) (.NET Base class Library): This is the library which contains objects that enable you to communicate with the system. For eg, to perform i/o with the harddisk, you include the library(System.IO). FCL is the library which is used by all the languages.

.NET CLR: This is the runtime environment for your .net applications. It takes care of executing your application and providing services behind the scenes like multi-threading, garbage collection, security, etc.

Applications developed using .NET:

Web Applications: ASP.NET is used to create web applications. It provides you a lot of predefined tools using which you can create a quick and robust applications. You can choose any one language(C#, VB, JScript,..) for the server-side scripting.

Windows Applications: You can use any one of the languages provided. These type of applications are desktop applications which is used by a single computer or user. This applications may further be classified as Console Application, GUI application.

Distributed Applications: Web Services, .NET remoting can be used to create these type of applications. But allpications created using .NET remoting cannot communicate with non-.net applications, Web Services on the other hand communicate with heterogeneous systems(other than .NET applications). Web services achieves this by using SOAP message protocol which uses XML for defining the messages. XML(eXtensible Markup Language) which is platform and language independent stores data in a plain text format rather than using any internal representation.

Hope this is useful for an beginner, and we will see more about it in the next section.