Sunday, January 20, 2008

Once in America a plane crashed,
only a monkey who was traveling in the plane was left alive.
Fortunately the monkey was intelligent enough to understand our language and reply in actions.
The officials went to see the monkey in the hospital and had a talk with the monkey.

Officer: "When the plane took off what were the travelers doing?"
Monkey: "Tying their belts"
Officer: "What were the airhostesses doing?"
Monkey: "Saying Hello! Good morning!"
Officer: "What were the pilots doing?"
Monkey: "Checking the system"
Officer: "What were you doing?"
Monkey: "Looking for my people"
Officer: "After 10' minutes what were the travelers doing?"
Monkey: "Having beverages and snacks"
Officer: "What were the airhostesses doing?"
Monkey: "Serving the travelers"
Officer: "What were the Pilots doing?"
Monkey: "Handling the steering"
Officer: "What were you doing?"
Monkey: "Eating & throwing"
Officer: "After 30 minutes what were the travelers doing?"
Monkey: "Some were sleeping and some were reading"
Officer: "What were the airhostesses doing?"
Monkey: "Make up"
Officer: "What were the pilots doing?"
Monkey: "Handling the steering"
Officer: "What were you doing?"
Monkey: "Nothing"
Officer: "Just before plane crash what were the travelers doing?"Monkey: "All were sleeping"

Officer: "What were the pilots doing?"
Monkey: "Handling the air hostess"
Officer: What were you doing?
Monkey: Handling the steering!!!! !
No more Questions!!! !!!!!!!!! !!!!!!!! ( to monkey)

IT Guys will never change.....

Two IT guys were chatting in a pub after work. "Guess what, mate," says the first IT guy, "yesterday, I met this gorgeous blonde girl in a bar." "What did you do?" says the other IT guy. "Well, I invited her over to my place, we had a couple of drinks, we got into the mood and then she suddenly asked me to make her feel special" "You're kidding me!" says the second IT guy. "I then I lifted her and put her on my desk next to my new laptop." "Really? You got a new laptop? What configuration?" "It's a 1 GB RAM and mobile internet connectivity card and 180GB HARDDISK..Wi-Fi..DVD-Writer......................"

Windows 100 Keyboard Shortcuts

Microsoft windows keyboard shortcuts. You might know most of these shortcuts, but probably not all of them. If you have more keyboard shortcuts, please let me know :)
CTRL+C (Copy)
CTRL+X (Cut)
CTRL+V (Paste)
CTRL+Z (Undo)
DELETE (Delete)
SHIFT+DELETE (Delete the selected item permanently without placing the item in the Recycle Bin)
CTRL while dragging an item (Copy the selected item)
CTRL+SHIFT while dragging an item (Create a shortcut to the selected item)
F2 key (Rename the selected item)
CTRL+RIGHT ARROW (Move the insertion point to the beginning of the next word)
CTRL+LEFT ARROW (Move the insertion point to the beginning of the previous word)
CTRL+DOWN ARROW (Move the insertion point to the beginning of the next paragraph)
CTRL+UP ARROW (Move the insertion point to the beginning of the previous paragraph)
CTRL+SHIFT with any of the arrow keys (Highlight a block of text)
SHIFT with any of the arrow keys (Select more than one item in a window or on the desktop, or select text in a document)
CTRL+A (Select all)
F3 key (Search for a file or a folder)
ALT+ENTER (View the properties for the selected item)
ALT+F4 (Close the active item, or quit the active program)
ALT+ENTER (Display the properties of the selected object)
ALT+SPACEBAR (Open the shortcut menu for the active window)
CTRL+F4 (Close the active document in programs that enable you to have multiple documents open simultaneously)
ALT+TAB (Switch between the open items)
ALT+ESC (Cycle through items in the order that they had been opened)
F6 key (Cycle through the screen elements in a window or on the desktop)
F4 key (Display the Address bar list in My Computer or Windows Explorer)
SHIFT+F10 (Display the shortcut menu for the selected item)
ALT+SPACEBAR (Display the System menu for the active window)
CTRL+ESC (Display the Start menu)
ALT+Underlined letter in a menu name (Display the corresponding menu)
Underlined letter in a command name on an open menu (Perform the corresponding command)
F10 key (Activate the menu bar in the active program)
RIGHT ARROW(Open the next menu to the right, or open a submenu)
LEFT ARROW (Open the next menu to the left, or close a submenu)
F5 key (Update the active window)
BACKSPACE (View the folder one level up in My Computer or Windows Explorer)
ESC (Cancel the current task)
SHIFT when you insert a CD-ROM into the CD-ROM drive (Prevent the CD-ROM from automatically playing)
Dialog Box Keyboard Shortcuts
CTRL+TAB (Move forward through the tabs)
CTRL+SHIFT+TAB (Move backward through the tabs)
TAB (Move forward through the options)
SHIFT+TAB (Move backward through the options)
ALT+Underlined letter (Perform the corresponding command or select the corresponding option)
ENTER (Perform the command for the active option or button)
SPACEBAR (Select or clear the check box if the active option is a check box)
Arrow keys (Select a button if the active option is a group of option buttons)
F1 key (Display Help)
F4 key (Display the items in the active list)
BACKSPACE (Open a folder one level up if a folder is selected in the Save As or Open dialog box)
Microsoft Natural Keyboard Shortcuts
Windows Logo (Display or hide the Start menu)
Windows Logo+BREAK (Display the System Properties dialog box)
Windows Logo+D (Display the desktop)
Windows Logo+M (Minimize all of the windows)
Windows Logo+SHIFT+M (Restore the minimized windows)
Windows Logo+E (Open My Computer)
Windows Logo+F (Search for a file or a folder)
CTRL+Windows Logo+F (Search for computers)
Windows Logo+F1 (Display Windows Help)
Windows Logo+ L (Lock the keyboard)
Windows Logo+R (Open the Run dialog box)
Windows Logo+U (Open Utility Manager)
Accessibility Keyboard Shortcuts
Right SHIFT for eight seconds (Switch FilterKeys either on or off)
Left ALT+left SHIFT+PRINT SCREEN (Switch High Contrast either on or off)
Left ALT+left SHIFT+NUM LOCK (Switch the MouseKeys either on or off)
SHIFT five times (Switch the StickyKeys either on or off)
NUM LOCK for five seconds (Switch the ToggleKeys either on or off)
Windows Logo +U (Open Utility Manager)
Windows Explorer Keyboard Shortcuts
END (Display the bottom of the active window)
HOME (Display the top of the active window)
NUM LOCK+Asterisk sign (*) (Display all of the subfolders that are under the selected folder)
NUM LOCK+Plus sign (+) (Display the contents of the selected folder)
NUM LOCK+Minus sign (-) (Collapse the selected folder)
LEFT ARROW (Collapse the current selection if it is expanded, or select the parent folder)
RIGHT ARROW (Display the current selection if it is collapsed, or select the first subfolder)
Shortcut Keys for Character Map
After you double-click a character on the grid of characters, you can move through the grid by using the keyboard shortcuts:
RIGHT ARROW (Move to the right or to the beginning of the next line)
LEFT ARROW (Move to the left or to the end of the previous line)
UP ARROW (Move up one row)
DOWN ARROW (Move down one row)
PAGE UP (Move up one screen at a time)
PAGE DOWN (Move down one screen at a time)
HOME (Move to the beginning of the line)
END (Move to the end of the line)
CTRL+HOME (Move to the first character)
CTRL+END (Move to the last character)
SPACEBAR (Switch between Enlarged and Nor mal mode when a character is selected)
Microsoft Management Console (MMC) Main Window Keyboard Shortcuts
CTRL+O (Open a saved console)
CTRL+N (Open a new console)
CTRL+S (Save the open console)
CTRL+M (Add or remove a console item)
CTRL+W (Open a new window)
F5 key (Update the content of all console windows)
ALT+SPACEBAR (Display the MMC window menu)
ALT+F4 (Close the console)
ALT+A (Display the Action menu)
ALT+V (Display the View menu)
ALT+F (Display the File menu)
ALT+O (Display the Favorites menu)
MMC Console Window Keyboard Shortcuts
CTRL+P (Print the current page or active pane)
ALT+Minus sign (-) (Display the window menu for the active console window)
SHIFT+F10 (Display the Action shortcut menu for the selected item)
F1 key (Open the Help topic, if any, for the selected item)
F5 key (Update the content of all console windows)
CTRL+F10 (Maximize the active console window)
CTRL+F5 (Restore the active console window)
ALT+ENTER (Display the Properties dialog box, if any, for the selected item)
F2 key (Rename the selected item)
CTRL+F4 (Close the active console window. When a console has only one console window, this shortcut closes the console)
Remote Desktop Connection Navigation

CTRL+ALT+END (Open the m*cro$oft Windows NT Security dialog box)
ALT+PAGE UP (Switch between programs from left to right)
ALT+PAGE DOWN (Switch between programs from right to left)
ALT+INSERT (Cycle through the programs in most recently used order)
ALT+HOME (Display the Start menu)
CTRL+ALT+BREAK (Switch the client computer between a window and a full screen)
ALT+DELETE (Display the Windows menu)
CTRL+ALT+Minus sign (-) (Place a snapshot of the active window in the client on the Terminal server clipboard and provide the same functionality as pressing PRINT SCREEN on a local computer.)
CTRL+ALT+Plus sign (+) (Place a snapshot of the entire client window area on the Terminal server clipboard and provide the same functionality as pressing ALT+PRINT SCREEN on a local computer.)

Internet Explorer navigation
CTRL+B (Open the Organize Favorites dialog box)
CTRL+E (Open the Search bar)
CTRL+F (Start the Find utility)
CTRL+H (Open the History bar)
CTRL+I (Open the Favorites bar)
CTRL+L (Open the Open dialog box)
CTRL+N (Start another instance of the browser with the same Web address)
CTRL+O (Open the Open dialog box, the same as CTRL+L)
CTRL+P (Open the Print dialog box)
CTRL+R (Update the current Web page)
CTRL+W (Close the current window)

Monday, June 25, 2007

Viewstate Cleared..!!

One of the most interesting features of ASP.NET 2.0 is the open way it allows the programmer to manipulate a control's viewstate. The viewstate should be the first choice to save data to access across postbacks


If the information is to be shared across different controls, it can be placed in the viewstate of the page, if it is to be accessed within the scope of a single control, then it can be placed within the viewstate of the control. The type of info one can save here may be simple data types or even complex user defined objects.



Storing Simple Data Types in the ViewState
Like most types of state management in ASP.NET, view state relies on a dictionary collection, where each item is indexed with a unique string name. For example, consider this code:

ViewState["ViewStateVariableName"] = 1;

This places the value 1 (or rather, an integer that contains the value 1) into the ViewState collection and gives it the descriptive name ViewStateVariable. If there is currently no item with the name ViewStateVariable, a new item will be added automatically. If there is already an item indexed under this name, it will be replaced.

You can access this variable anywhere within the page/control where the viewstate variable has been added. When retrieving a value, you use the key name.

int number = (int) ViewState["ViewStateVariable"];

You also need to cast the retrieved value to the appropriate data type. This is because the ViewState collection stores all items as generic objects which also give you the flexibility to store any type of basic data types in it. In fact, you can even store your custom objects in the view state.


Storing Objects in View State



You can store your own objects in view state just as easily as you store numeric and string types. However, to store an item in view state, ASP.NET must be able to convert it into a stream of bytes so that it can be added to the hidden input field in the page. This process is called serialization. If your objects aren't serializable (and by default they aren't), you'll receive an error message when you attempt to place them in view state.

To make your objects serializable, all you need to do is to add the 'Serializable' attribute before your class declaration. For example, here's an exceedingly simple Student class:

[Serializable]
public class Student
{
public string firstName;
public string lastName;

public Student(string fName, string lName)
{
firstName = fName;
lastName = lName;
}
}

Because the Student class is marked as serializable, it can be stored in view state:

// Storing a student in view state.
Student stud = new Student("John", "Doe");
ViewState["CurrentStudent"] = stud;

Remember, when using custom objects, you'll need to cast your data when you retrieve it from view state.

// Retrieve a student from view state.
Student stud = (Student) ViewState["CurrentCustomer"];

To be serializable, your classes you must meet these requirements:

• Your class must have the Serializable attribute.

• Any classes it derives from must have the Serializable attribute.

• All the private variables of the class must be serializable data types. Any nonserializable data type must be decorated with the NonSerialized attribute (which means it is simply ignored during the serialization process).

Example 1: Making a PostBackCounter
Let's say you want to keep track of the number of times a page is posted back to the server. This info can be kept inside the session, or even inside a hidden field control in the page. However, keeping it in the session forces you to clear up or reset this variable every time the user moves to a new page. Keeping it in a hidden filed or an invisible text box on the page might clutter your .aspx file with controls that can be changed by malicious users. Using the view state in this scenario is an elegant way to persist info across postbacks that will be accessible only to the programmer and the code.

Suppose our .aspx page control contains one button and one label inside the form.




On the code behind file we have the page load event and the event handler for the click event of button1.

protected void Page_Load(object sender, System.EventArgs e)
{
//First we check if the page is loaded for the first time,
if (!IsPostBack)
{
ViewState["PostBackCounter"] = 0;
}
//Now, we assign the variable value in the text box
int counter = (int)ViewState["PostBackCounter"];
Label1.Text = counter.ToString();
}

Now, the label will always display the post back count!

Let's say we increment the post back count only when our button is clicked. In this case, we need to write some code on the button click event handler.

protected void Button1_Click(object sender, System.EventArgs e)
{
int oldCount = (int)ViewState["PostBackCounter"];
int newCount = oldCount + 1;
//First, we assign this new value to the label
Label1.Text = newCount.ToString();
//Secondly, we replace the old value of the view state so that the new value is read the next //time
ViewState["PostBackCounter"] = newCount;
}

Now, every time the user will click on this button, the text will show an incremented number!

Example 2: Tracking Dynamic Controls
One of the main challenges with working with dynamically added controls is that these controls must be programmatically added on each postback. That is, you can't just load these controls on the first page load, and then not reload them on subsequent postbacks. Failure to explicitly add the controls on each postback will cause the controls to literally disappear on postbacks.

Let's build on our previous example. Only this time, instead of an integer, we'll keep a List in the view state. Instead of one label, we are going to dynamically add a new Label every time the button is clicked. And, the value of the new label will be given by the user inside a text box.

So, out .aspx page will look like this:





The placeholder will act as the container on which we shall load out Labels.

On the code behind cs file, we have page load event, which initializes the controls:

protected void Page_Load(object sender, System.EventArgs e)
{
//First we check if the page is loaded for the first time,
if(!IsPostBack)
{
List temp = new List();
ViewState["allTextData"] = temp;
}
List myTextData = (List) ViewState["allTextData"];
//Loading the dynamic controls
foreach(string data in myTextData)
{
AddDynamicLabel (data);
}
}

The AddDynamicLabel() function dynamically adds a label in the placeholder.

private void AddDynamicLabel (string data)
{
//Making the new label and giving it the appropriate value
Label l = new Label();
l.Text = data + "
";
//Adding the label into the page
PlaceHolder1.Controls.Add(l);
}

And finally we have the Button1 click event handler, where we add a brand new label in our placeholder.

protected void Button1_Click(object sender, System.EventArgs e) { //Adding the new text as a label inside the placeholder AddDynamicLabel (TextBox1.Text); //Adding the new text inside the viewstate List, for retreival on subsequent postbacks List myTextData = (List) ViewState["allTextData"]; myTextData.Add(TextBox1.Text); ViewState["allTextData"] = myTextData; }

When to use the ViewState
View state is a good way to store data in between postbacks, because it doesn't take up any memory on the server side and it doesn't impose any arbitrary usage limits (such as a timeout). So, what might force you to abandon view state for another type of state management? Here are three possible reasons:

• You need to store mission-critical data that the user cannot be allowed to tamper with. (An ingenious user could modify the view state information in a postback request.) In this case, consider session state.

• You need to store information that will be used by multiple pages. In this case, consider session state, cookies, or the query string.

• You need to store an extremely large amount of information, and you don't want to slow down page transmission times. In this case, consider using a database, or possibly session state.



Remember, any data that you put in the view state will be part of the data stream that goes to the client and then comes back to the server, so be very careful while putting large chunks of data in the ViewState!

Wednesday, April 18, 2007

Play & Enjoy SQL Joins

The SQL JOIN clause is used whenever we have to select data from 2 or more tables.

To be able to use SQL JOIN clause to extract data from 2 (or more) tables, we need a relationship between certain columns in these tables.

We are going to illustrate our SQL JOIN example with the following 2 tables:

Customers:

CustomerID FirstName LastName Email DOB Phone
1 John Smith John.Smith@yahoo.com 2/4/1968 626 222-2222
2 Steven Goldfish goldfish@fishhere.net 4/4/1974 323 455-4545
3 Paula Brown pb@herowndomain.org 5/24/1978 416 323-3232
4 James Smith jim@supergig.co.uk 20/10/1980 416 323-8888


Sales:

CustomerID Date SaleAmount
2 5/6/2004 $100.22
1 5/7/2004 $99.95
3 5/7/2004 $122.95
3 5/13/2004 $100.00
4 5/22/2004 $555.55


As you can see those 2 tables have common field called CustomerID and thanks to that we can extract information from both tables by matching their CustomerID columns.

Consider the following SQL statement:


SELECT Customers.FirstName, Customers.LastName, SUM(Sales.SaleAmount) AS SalesPerCustomer
FROM Customers, Sales
WHERE Customers.CustomerID = Sales.CustomerID
GROUP BY Customers.FirstName, Customers.LastName




The SQL expression above will select all distinct customers (their first and last names) and the total respective amount of dollars they have spent.
The SQL JOIN condition has been specified after the SQL WHERE clause and says that the 2 tables have to be matched by their respective CustomerID columns.

Here is the result of this SQL statement:

FirstName LastName SalesPerCustomers
John Smith $99.95
Steven Goldfish $100.22
Paula Brown $222.95
James Smith $555.55


The SQL statement above can be re-written using the SQL JOIN clause like this:


SELECT Customers.FirstName, Customers.LastName, SUM(Sales.SaleAmount) AS SalesPerCustomer
FROM Customers JOIN Sales
ON Customers.CustomerID = Sales.CustomerID
GROUP BY Customers.FirstName, Customers.LastName




There are 2 types of SQL JOINS – INNER JOINS and OUTER JOINS. If you don't put INNER or OUTER keywords in front of the SQL JOIN keyword, then INNER JOIN is used. In short "INNER JOIN" = "JOIN" (note that different databases have different syntax for their JOIN clauses).

The INNER JOIN will select all rows from both tables as long as there is a match between the columns we are matching on. In case we have a customer in the Customers table, which still hasn't made any orders (there are no entries for this customer in the Sales table), this customer will not be listed in the result of our SQL query above.

If the Sales table has the following rows:

CustomerID Date SaleAmount
2 5/6/2004 $100.22
1 5/6/2004 $99.95


And we use the same SQL JOIN statement from above:


SELECT Customers.FirstName, Customers.LastName, SUM(Sales.SaleAmount) AS SalesPerCustomer
FROM Customers JOIN Sales
ON Customers.CustomerID = Sales.CustomerID
GROUP BY Customers.FirstName, Customers.LastName




We'll get the following result:

FirstName LastName SalesPerCustomers
John Smith $99.95
Steven Goldfish $100.22


Even though Paula and James are listed as customers in the Customers table they won't be displayed because they haven't purchased anything yet.

But what if you want to display all the customers and their sales, no matter if they have ordered something or not? We’ll do that with the help of SQL OUTER JOIN clause.

The second type of SQL JOIN is called SQL OUTER JOIN and it has 2 sub-types called LEFT OUTER JOIN and RIGHT OUTER JOIN.

The LEFT OUTER JOIN or simply LEFT JOIN (you can omit the OUTER keyword in most databases), selects all the rows from the first table listed after the FROM clause, no matter if they have matches in the second table.

If we slightly modify our last SQL statement to:


SELECT Customers.FirstName, Customers.LastName, SUM(Sales.SaleAmount) AS SalesPerCustomer
FROM Customers LEFT JOIN Sales
ON Customers.CustomerID = Sales.CustomerID
GROUP BY Customers.FirstName, Customers.LastName




and the Sales table still has the following rows:

CustomerID Date SaleAmount
2 5/6/2004 $100.22
1 5/6/2004 $99.95


The result will be the following:

FirstName LastName SalesPerCustomers
John Smith $99.95
Steven Goldfish $100.22
Paula Brown NULL
James Smith NULL


As you can see we have selected everything from the Customers (first table). For all rows from Customers, which don’t have a match in the Sales (second table), the SalesPerCustomer column has amount NULL (NULL means a column contains nothing).

The RIGHT OUTER JOIN or just RIGHT JOIN behaves exactly as SQL LEFT JOIN, except that it returns all rows from the second table (the right table in our SQL JOIN statement).