Tuesday, January 31, 2012

Compare Two Instances of a Reference Type


class Equality
{
    static void Main()
    {
        // Numeric equality: True
        Console.WriteLine((2 + 2) == 4);

        // Reference equality: different objects, 
        // same boxed value: False.
        object s = 1;
        object t = 1;
        Console.WriteLine(s == t);

        // Define some strings:
        string a = "hello";
        string b = String.Copy(a);
        string c = "hello";

        // Compare string values of a constant and an instance: True
        Console.WriteLine(a == b);

        // Compare string references; 
        // a is a constant but b is an instance: False.
        Console.WriteLine((object)a == (object)b);

        // Compare string references, both constants 
        // have the same value, so string interning
        // points to same reference: True.
        Console.WriteLine((object)a == (object)c);
    }
}
/*
Output:
True
False
True
False
True
*/
 
http://msdn.microsoft.com/en-us/library/362314fe.aspx
 
http://msdn.microsoft.com/en-us/library/53k8ybth.aspx 

Facebook Keyboard Shortcut Keys

"Know How to be fast Facebook Savvy"
Facebook Keyboard Shortcut KeysFor fast browsing experience we must know about the keyboard shortcuts. Most of the net savvy people love to use keyboard in  place of mouse for handling all the activities done while browsing and surfing process. In the same way, I think you never heard about Facebook shortcuts but Its true and we have many more facebook shortcuts which makes the use of facebook too easy on various browsers. Therefore I want to tell you some shortcut keys specially used for facebook handling like: notification, view profile, privacy setting and much more...

                                "List of facebook Shortcuts"
Alt+1: View your News Feed
Alt+2: View your own Profile
Alt+3: View pop-up of friend requests
Alt+4: View pop-up of messages
Alt+5: View pop-up of notifications
Alt+6: View Account Settings
Alt+7: View Privacy Settings
Alt+8: View Facebook’s own profile
Alt+9: Read latest Terms of Service agreement.
Alt+?: Search
Alt+m: Compose a new message

For Firefox:

Shift+Alt+1: View your News Feed
Shift+Alt+2: View your own Profile
Shift+Alt+3: View pop-up of friend requests
Shift+Alt+4: View pop-up of messages
Shift+Alt+5: View pop-up of notifications
Shift+Alt+6: View Account Settings
Shift+Alt+7: View Privacy Settings
Shift+Alt+8: View Facebook’s own profile
Shift+Alt+9: Read latest Terms of Service agreement.
Shift+Alt+0: Open Facebook Help Center
Shift+Alt+?: Search
Shift+Alt+m: Compose a new message

                                       Use the Facebook shortcuts of Google chrome,but you have to press ENTER after each shortcut.

Monday, January 30, 2012

implement cursor with sql server 2008

Find the solution of following questions:
What is cursor?
Why cursor?
When to apply cursor?
How to implement cursor?

   
"Cursors are an extension to the result sets that provide the mechanism to do operation on a particular row of result set or small block of rows at a time"

   
"Microsoft SQL Server statements produce a complete result set, but there are times when the results are best processed one row at a time. Opening a cursor on a result set allows processing the result set one row at a time. You can assign a cursor to a variable or parameter with cursor data type.
 
Steps while implementing cursor:
  • Declare a cursor that defines a result set.
  • Open the cursor to establish the result set.
  • Fetch the data into local variables as needed from the cursor, one row at a time.
  • Close the cursor when done
  • Deallocate the cursor
CURSOR Declaration:
DECLARE cursor_name CURSOR [ LOCAL | GLOBAL ]
    [ FORWARD_ONLY | SCROLL ]
    [ STATIC | KEYSET | DYNAMIC | FAST_FORWARD ]
    [ READ_ONLY | SCROLL_LOCKS | OPTIMISTIC ]
    [ TYPE_WARNING ]
    FOR select_statement
    [ FOR UPDATE [ OF column_name [ ,...n ] ] ]

LOCAL:
    Scope of Local cursor is only to batch , stored procedure , or trigger in   which it is declared. And LOCAL cursor automatically deallocated when container block ,sp or trigger terminated.

GLOBAL:
    Specifies that the scope of the cursor is global to the connection. The cursor name can be referenced in any stored procedure or batch executed by the connection. The cursor is   only  implicitly deallocated at disconnect.

FORWARD_ONLY
          - Specifies that cursor can only fetch data sequentially from the first to the last row. FETCH NEXT is the only fetch option supported. It is the fastest cursor that you can update.
     The effects of all INSERTUPDATE, and DELETE statements made by the current user or committed by other users that affect rows in the result set are visible as the rows are fetched from the cursor. Because the cursor cannot be scrolled backward, changes made to rows in the database after the row was fetched are not visible by using the cursor.                    Note: by default it is forward-only.

 
STATIC: Defines a cursor that makes a temporary copy of the data in tempdb to be used by the cursor. Therefore modification made to base table does not reflect to temporary table , and this cursor does not allow modification.
 

DYNAMIC: Defines a cursor that reflects all data changes made to the rows in its result set as you scroll around the cursor. The data values, order, and membership of the rows can change on each fetch. The FETCH ABSOLUTE option is not supported with dynamic cursors.


FAST_FORWARD: Specify forward-only , read-only cursor. FAST_FORWARD can not be applied if SCROLL or  FOR_UPDATE is specified.
 

READ_ONLY:  Prevent updates made through this cursor.
 

SCROLL_LOCK: specify that updation  or deletion made through this cursor guarantee to succeed. Because SQL server lock the corresponding row.  
 

OPTIMISTIC: Specifies that positioned updates or deletes made through the cursor do not succeed if the row has been updated since it was read into the cursor. SQL Server does not lock rows as they are read into the cursor.


FOR UPDATE [OF column_name [,...n]]
           Defines updatable columns within the cursor. If OF column_name [,...n] is supplied, only the columns listed allow modifications. If UPDATE is specified without a column list, all columns can be updated, unless the READ_ONLY concurrency option was specified.

Open the CURSOR:
Syntax for opening cursor is
Open <cursor_name>

Fetch row from CURSOR:
FETCH [NEXT | PRIOR | FIRST | LAST |ABSOLUTE{n}|RELATIVE{n}]                        [FROM]    <cursor_name>     [INTO @variable_name[1,2,…n variable]]   
Let us define the terms used in syntax---
NEXT: Returns the immediate  next row .And increment current row pointer to  that row.
PRIOR: Returns the immediate  previous row .And increment current row pointer to  that row.  
FIRST: Returns the first row in the cursor and makes it the current row.
LAST: Returns the last row in the cursor and makes it the current row.
ABSOLUTE{ n}    :Return nth row of result set. If n is positive, counting start from top of result set .If n is negative , counting start from bottom of result set. In both situation returned row is made as new current row. If n is 0 no row is returned.
RELATIVE { n }: Same      functionality as absolute , but only difference is that counting start from current row ,forward of backward ,according to sign of n.                                         NOTE:
If either FORWARD_ONLY or FAST_FORWARD is specified, NEXT is the only FETCH option supported.
If DYNAMIC, FORWARD_ONLY or FAST_FORWARD are not specified, and one of KEYSET, STATIC, or SCROLL are specified, all FETCH options are supported.
DYNAMIC SCROLL cursors support all the FETCH options except ABSOLUTE.

Close the CURSOR:
Closes an open cursor by releasing the current result set and freeing any cursor locks held on the rows on which the cursor is positioned. CLOSE leaves the data structures available for reopening, but fetches and positioned updates are not allowed until the cursor is reopened. CLOSE must be issued on an open cursor; CLOSE is not allowed on cursors that have only been declared or are already closed.

SYNTAX:
      CLOSE   <cursor_name>

DEALLOCATE CURSOR:
Removes a cursor reference. When the last cursor reference is deallocated, the data structures comprising the cursor are released by Microsoft SQL Server.
 
                                                 SIMPLE EXAMPLE
DECLARE @email VARCHAR(100)
DECLARE Temp_cursor CURSOR
    FOR SELECT TOP 10 PrimaryEmail FROM Users
    
    OPEN Temp_cursor
    FETCH NEXT FROM Temp_cursor INTO @email
    WHILE @@FETCH_STATUS=0
    BEGIN
        PRINT @email
        FETCH NEXT FROM Temp_cursor INTO @email
    END
    CLOSE Temp_cursor
    DEALLOCATE Temp_cursor

@@FETCH_STATUS  Values:    return the status  of last cursor FETCH statement.
      0: the FETCH statement was successful
    -1: failed or row was beyond the result set

    -2: the row fetched is missing


Thursday, January 26, 2012

Mostly Asked Oops Interview Questions

Common OOPs Questions Frequently Asked By Interviewers

"When we face the technical Interview in an IT company(small OR MNC) we found that the interviewer mostly start interview with some basic  oops questions and we feel very bad when unable to reply. So dear friends I want to discuss you some common questions"

What is class and objects?
Sol:
Class is collection of functions and variables. and object is used to use this template.The keyword class in C# indicates that we are going to define a new class (type of object)

What is Virtual method?
Sol: Virtual Method has implementation & provide the derived class with the option to override it.

What is the use of constructor ?
Sol:
Constructor is used to create and instantiate a object. 

Can we declare a constructor to private?
Sol:
Yes

Can Struct be inherited?
Sol: No, Struct can't be inherited as this is implicitly sealed.

Can we specify the accessibility modifier inside the interface?
Sol:
It should be public there is no use of private methods in interface.
 
How to prevent a public class method to be overridden in derived class?
Sol: Just make method to sealed.

What is Sealed modifiers?
Sol:
Sealed types cannot be inherited & are concrete.
Sealed modifiers can also be applied to instance methods, properties, events & indexes. It can't be applied to static members. 
Sealed members are allowed in sealed and non-sealed classes.

How to prevent a class being instantiated?
Sol:
Make class abstract or static

How to override a private virtual method?
Sol:
You can not override a private method.

Can you derive a static method of base class in child class?
Sol:
No

What is Static field?
Sol:
To indicate that a field should only be stored once no matter how many instance of the class we create.

How to stop a class being inherited by another class?
Sol:
Just make the class sealed. by using sealed keyword.

Can you override a normal public method of base class and make it static?
Sol:
No , the method signature should be same to be overridden.
Question 11. what is early and late binding?
Answer. when we call a non virtual method it decide at compile time and this is called early binding. while if method calling decide at runtime is called late binding.

What is Static Method?
Sol:
It is possible to declare a method as Static provided that they don't attempt to access any instance data or other instance methods.

What is Inheritance?
Sol:
It provides a convenient way to reuse existing fully tested code in different context thereby saving lot of coding. 

What is Virtual keyword?
Sol:
This keyword indicates that a member can be overridden in a child class. It can be applied to methods, properties, indexes and events.

What is New modifiers?
Sol:
The new modifiers hides a member of the base class. C# supports only hide by signature.

Can namespace contain the private class?
Sol:
No, having a single private class does not mean anything , it is applicable only in case of nested class.

What is Abstract method?
Sol: Abstract method doesn't provide the implementation & forces the derived class to override the method.

When to use Interface over abstract class?
Sol:
Abstract Classes: Classes which cannot be instantiated. This means one cannot make a object of this class or in other way cannot create object by saying ClassAbs abs = new ClassAbs(); where ClassAbs is abstract class. 
Abstract classes contains have one or more abstarct methods, ie method body only no implementation.
Interfaces: These are same as abstract classes only difference is we can only define method definition and no implementation.
When to use wot depends on various reasons. One being design choice.
One reason for using abstarct classes is we can code common
functionality and force our developer to use it. I can have a complete
class but I can still mark the class as abstract.
Developing by interface helps in object based communication.

Tuesday, January 24, 2012

Description about SQL Server Data Types

SQL Server Data Types:Remove Confusion and Get Detailed Description

"SQL Server has a variety of data types, and when we want to implementation, 90% we get confused  that which one should be use at what time. Most misunderstandings arise from data type limitations rather than functionality. So dear friends I want to discuss you much more for the solution of this common problem"
  •  Differentiate char, varchar, nchar, and nvarchar:

Data Type Length Storage Size Max Characters Unicode
char Fixed Always n bytes 8,000 No; each character requires 1 byte
varchar Variable Actual length of entry in bytes 8,000 No; each character requires 1 byte
nchar Fixed Twice n bytes 4,000 Yes; each character requires 2 bytes
nvarchar Variable Twice actual length of entry in bytes 4,000 Yes; each character requires 2 bytes

Few general rules:
  1. Don’t use nchar or nvarchar unless you truly need it. (Unicode provides a unique number for up to 65,536 characters. ANSI, the one most of us are most familiar with, has only 256.) Unless you’re working with an international application, you probably don’t need a Unicode data type.
  2. Use the smallest data type necessary, but make sure it can accommodate the largest possible value.
  3. Use a fixed-length data type when the values are mostly about the same size.
  4. Use a variable length when the values vary a lot in size.
    •  Differentiate tinyint, smallint, int, bigint:

    Data type Minimum value Maximum value Storage size
    tinyint 0 255 1 byte
    smallint -32,768 32,767 2 bytes
    int -2,147,483,648 2,147,483,674 4 bytes
    bigint -9,223,372,036,854,775,808 9,223,372,036,854,775,807 8 bytes
    Note: Always use the smallest integer data type that accommodates the largest possible value.
    •  Differentiate numeric and decimal:

    There’s no difference between the numeric and decimal data types. Use them interchangeably or use one or the other to store integer and floating-point numbers scaled from 1 to 38 places, inclusive of both sides of a decimal. Use this data type when you need to control the accuracy of your calculations in terms of the number of decimal digits.
    Total characters (precision) Storage size
    1 - 9 5 bytes
    10 - 19 9 bytes
    20 - 28 13 bytes
    29 - 38 17 bytes
    •  Differentiate float and real:

    The only differences between float and real are their minimum and maximum values and their required storage.
    Data type n Minimum Value Maximum value Precision Storage size
    float(n) 1 - 24 -1.79E + 308 1.79 + 308 7 digits 4 bytes
    25 - 53 -1.79E + 308 1.79E + 308 15 digits 8 bytes
    real n/a -3.40E + 38 3.40E + 38 7 digits 4 bytes
    The real data type is the same as float(24) — a floating data type with 24 digits to the right of the decimal point.
    • Differentiate Smalldatetime and datetime:

    Both smalldatetime and datetime store a combination date and time value, but the minimum and maximum values, accuracy, and storage size are different.
    Data type Minimum value Maximum value Accuracy Storage size
    smalldatetime January 1, 1900 June 6, 2079 Up to a minute 4 bytes (the first 2 bytes store the date; the second 2 bytes store the time)
    datetime January 1, 1753 December 31, 9999 One three-hundredth of a second 8 bytes (the first 4 bytes store the date; the second 4 bytes store the time)
    •  Differentiate smallmoney and money:

    Use both smallmoney and money to store currency values. However, the minimum and maximum values for both differ. Both data types are accurate up to ten-thousandths of a monetary unit.
    Data type Minimum value Maximum value Storage size
    smallmoney -214,748.3648 214,748,3647 4 bytes
    money -922,337,203,685,477.5808 922,337,203.685,477.5807 8 bytes
    • Differentiate  bit data type(boolean):

    SQL Server doesn’t have a Boolean data type, at least not by that name. To store True/False, Yes/No, and On/Off values, use the bit data type. It accepts only three values: 0, 1, and NULL. (NULL is supported by SQL Server 7.0 and later.)
    •  Define text, ntext, and image:

    SQL Server is phasing out text, ntext, and image. There’s no way to know how long SQL Server will support the older data types. Upgrade legacy applications to varchar, nvarchar, and varbinary.
    • How do I assign a cursor or table data type?

    You don’t, at least not in the traditional manner. You don’t assign these data types to a column. You can use cursor and table only as variables:
    1. The cursor data type allows you to return a cursor from a stored procedure or store a cursor as a variable.
    2. The table data type returns a table from a stored procedure or stores a table as a variable for later processing.
      • SQL Server 2008 new data types:

      1. date stores only date values with a range of 0001-01-01 through 9999-12-31.
      2. time stores only time values with a range of 00:00:00.0000000 through 23:59:59.9999999.
      3. datetime2 has a larger year and second range.
      4. datetimeoffset lets you consider times in different zones.
      5. hierarchyid constructs relationships among data elements within a table, so you can represent a position in a hierarchy.
      6. spatial identifies geographical locations and shapes — landmarks, roads, and so on.

        Monday, January 23, 2012

        Create Animated GIF Image From Videos

        Create animated GIF from a movie scene or a music video that you can later set as an avatar image in web forums

        There are free tools available that let you convet images and videos into animated GIFs very easily, that you can later host on sites like Flickr or Tumblr.
        Create Animated GIF Image From Videos
        Create Animated GIF Image From Videos
        This process consists of two steps:- first cut the video into a sequence of images frames and then assemble these frames into looping GIF animation with some custom delay.
        STEP 1:
                    Download FFmpeg for Windows from sourceforge.net and extract the ffmpeg.exe file to, say, the c:\ffmpeg\ folder. The .7z files can be unzipped using the 7-zip utility.
        STEP 2:
                   Now we’ll extract the frames from our video file. The command is very simple.
        c:\ffpeg\ffpeg.exe-I movie.avi img-%o3d.gif
        If your video file is in another folder, you need to specify the full path of that file. Also, you may use MP4, MOV or any video format as well instead of AVI
        STEP 3:
                  Grab this portable utility called Unfreeze and drag-n-drop all the images frames created in the previous step from Windows Explorer into Unfreeze Window.
        Enable “Loop animation” and set the frame delay to say 20cs(1cs=0.01 sec). Click “make animated GIF” and preview your images in any browser.

        Saturday, January 21, 2012

        Logic behind Wedding Ring Puts on Fourth Finger Always

        "Wedding Ring is Only for the Fourth Finger"

        Its very interested topic that why we always put the wedding ceremony ring to our fourth finger only

        Thumb represents parents
        Second finger represents brothers & sisters
        Center finger represents own self
        Fourth finger represents your partner
        Last finger represents your children
        Logic behind Wedding Ring Puts on Fourth Finger Always
         
        Pls follow the following steps for this miracle:
        • Firstly, show your palm, center finger bend and put together back to back Secondly, the rest 4 fingers tips to tips
        • Game begins. now follow the below arrangement,
        1. Try to open your thumb, the thumb represents parents, it can be open because all human does go thru sick and dead. Which are our parents will leave us one day.

        2. Please close up your thumb, then open your second finger, the finger represent brothers and sisters, they do have their own family which is too they will leave us too.

        3. Now close up your second finger, open up your little finger, this represent your children. Sooner or later they too will leave us for they got they own living to live.

        4. Nevertheless, close up your little finger, try to open your fourth finger which we put our wedding ring; you will be surprise to find that it cannot be open at all. Because it represent husband and wife, this whole life you will be attach to each other.

                                              "Real love will stick together ever and forever"

        How to fix orphaned SQL Server users

        How to Remove Orphaned Users From Database
        "When you restore a SQL Server database on a different machine, you cannot access the database until you fix the permissions"

        The problem is that the user in the database is an "orphan". This means that there is no login id or password associated with the user. This is true even if there is a login id that matches the user, since there is a GUID (called a SID in Microsoft-speak) that has to match as well.
        This used to be a pain to fix.
        Now I am telling you the multiple solutions for fixing the orphaned user problem.

        • To find orphaned users:
        EXEC sp_change_users_login 'Report'
        •  If user exist then remove one by one, and execute:
          EXEC sp_change_users_login 'update_one', 'orphan user name'  'orphan user name'
        • If you already have a login id and password for this user, fix it by:
        EXEC sp_change_users_login 'Auto_Fix', 'user'
        • If you want to create a new login id and password for this user, fix it by:
        EXEC sp_change_users_login 'Auto_Fix', 'user', 'login', 'password'
        Now you will be able to access your database


        Twitter Delicious Facebook Digg Stumbleupon Favorites More

         
        Powered by Code Imagine