Thursday, February 9, 2012

Magic Table in SQL Server



Magic Table
In SQL server magic table is nothing more than an internal table which is created by the SQL server to recover recently inserted, deleted and updated data into SQL server database. That is when we insert or delete any record from any table in SQL server then recently inserted or deleted data from table also inserted into inserted magic table or deleted magic table with help of which we can recover data which is recently used to modify data into table either use in delete, insert or update to table. Basically there are two types of magic table in SQL server namely: inserted and deleted, update can be performed with help of these twos. Generally we cannot see these two table, we can only see it with the help Trigger’s in SQL server. Let’s see the following

Example

INSERTED Magic Table
When we insert record into table then SQL server automatically created ‘inserted’ magic table and recently inserted record are available in this table, If we want to recover this data which are recently inserted then we can access this record with the help of Trigger’s. Let’s see the demonstration of inserted magic table in SQL Server.

CREATE TABLE TO DEMONSTRATION OF INSERT MAGIC TABLE 

CREATE TABLE INSERT_MAGIC(ID INT,TRIGGER_MESSAGE VARCHAR(50) )
 DEMONSTRATION OF CREATING TRIGGER TO EXPLORE INSERTED MAGIC TABLE

  CREATE TRIGGER TRI_MAGIC_INSERT
  ON USERLOGIN
  INSTEAD OF INSERT
  AS  BEGIN
  DECLARE @ID INT
  SELECT @ID = (SELECT ID FROM inserted)
  INSERT INTO INSERT_MAGIC VALUES (@ID,'RECORD ADDED')
  END
  GO

Note: Here ‘inserted’ is insert magic table

DELETED Magic Table
When we delete record from the table then SQL automatically create a deleted magic table which holds are deleted record from original table if we want to recover it then we can access that data from deleted magic table. Let’s see demonstration of recover data from deleted magic table.
Example: Creating Trigger for Deleted Magic table

DEMONSTRATION OF CREATING TRIGGER TO EXPLORE INSERTED MAGIC TABLE

  CREATE TRIGGER TRI_MAGIC_DELETE
  ON USERLOGIN
  INSTEAD OF INSERT
  AS  BEGIN
  DECLARE @ID INT
  DECLARE @NAME VARCHAR(50)
  SELECT @ID = (SELECT ID FROM deleted)
  SELECT @NAME = (SELECT NAME FROM deleted)
  INSERT INTO INSERT_MAGIC VALUES (@ID,@NAME)
  END
  GO

Garbage Collection


The garbage collector checks to see if there are any objects in the heap that are no longer being 
used by the application. If such objects exist, then clean them. 

Note: If no more memory is available for the heap, then the new operator throws an OutOfMemoryException. 

Tuesday, February 7, 2012

Create Watermark TextBox using JavaScript


JavaScript code:


<script type = "text/javascript">
          var defaultText = "Search Pathfinder Trainers";
           function WaterMark(txt, evt) {
            if (txt.value.length == 0 && evt.type == "blur") {
                txt.style.color = "gray";
                txt.value = defaultText;
            }
            if (txt.value == defaultText && evt.type == "focus") {
                txt.style.color = "gray";
                txt.value = "";
            }
        }
</script>


HTML code:


<asp:TextBox ID="txtsearch" runat="server" Width="320px" Text = "Search Pathfinder Trainers" CssClass="rcbInput" onblur = "WaterMark(this, event);"
                                           onfocus = "WaterMark(this, event);">
 </asp:TextBox> 


C# code:


          if (!Page.IsPostBack)
           {
                txtsearch.Attributes.Add("onblur", "WaterMark(this, event);");
                txtsearch.Attributes.Add("onfocus", "WaterMark(this, event);");
          }

Saturday, February 4, 2012

debug and release mode


Whether your code is built in Release or Debug mode doesn't matter.The generated IL is very nearly the same. The Debug version will have an attribute that the JIT uses to set compilation defaults, that attribute is missing in your. The next thing that matters is exactly how you debug or run your application. 


The setting that's important is Tools:

  • Options, 
  • Debugging, 
  • General

Suppress JIT optimization on module load. It is ticked by default.

How to use ListView selected index?


void listView_ItemCreated(object sender, ListViewItemEventArgs e)
{
        if ( listView.SelectedIndex > -1 ) return; 


        ListViewDataItem item = e.Item as ListViewDataItem;
         if (DoSelectDataItem(item)==true)
        {
                listView.SelectedIndex = item.DisplayIndex;
                if ( listView.SelectedItemTemplate != null )
                {
                        e.Item.Controls.Clear();
                        listView.SelectedItemTemplate.InstantiateIn(e.Item);
                }
        }
}


bool DoSelectDataItem(ListViewDataItem item)
{
        return item.DisplayIndex == 0; 
}

Thursday, February 2, 2012

What is Silverlight ?


It is a cross-browser, cross-platform technology. It runs in all popular Web browsers, including Microsoft Internet Explorer, Mozilla Firefox, and Apple Safari, Google Chrome, and on Microsoft Windows etc.


It enables you to create Windows Phone applications. To learn more about creating Windows Phone applications with Silverlight.


It is supported by a small download that installs in seconds.


It streams video and audio. It scales video quality to everything from mobile devices to desktop browsers to 720p HDTV video modes.


It includes compelling graphics that users can manipulate—drag, turn, and zoom—directly in the browser.


It reads data and updates the display, but it doesn't interrupt the user by refreshing the whole page.


The application can run in the Web browser or you can configure it so users can run it on their computer (out-of-browser). In addition, you can use your knowledge of the Silverlight framework to create Windows Phone applications.


Features


WPF and XAML. Silverlight includes a subset of the Windows Presentation Foundation (WPF) technology, which greatly extends the elements in the browser for creating UI. Silverlight lets you create immersive graphics, animation, media, and other rich client features, extending browser-based UI beyond what is available with HTML alone. XAML provides a declarative markup syntax for creating elements.


Extensions to JavaScript. Silverlight provides extensions to the universal browser scripting language that provide control over the browser UI, including the ability to work with WPF elements. 


Cross-browser, cross-platform support. Silverlight runs the same on all popular browsers (and on popular platforms). You can design and develop your application without having to worry about which browser or platform your users have.


Integration with existing applications. Silverlight integrates seamlessly with your existing JavaScript and ASP.NET AJAX code to complement functionality you have already created.


Networking support. Silverlight includes support for HTTP over TCP. You can connect to WCF, SOAP, or ASP.NET AJAX services and receive XML, JSON, or RSS data. 


LINQ. Silverlight includes language-integrated query (LINQ), which enables you to program data access using intuitive native syntax and strongly typed objects in .NET Framework languages.


New in Silverlight 5


Silverlight 5 includes several new features that are based on customer suggestions. This topic introduces some of the new features and improvements in Silverlight 5.


The following sections are:

  1. Controls
  2. Media
  3. Graphics
  4. Text
  5. Trusted Applications
  6. Printing
  7. User Interface
  8. Data
  9. XAML
  10. Performance Improvements
  11. Related Topics


Generics classes Overview


Generic classes and methods combine re-usability, type safety and efficiency also. 
Generics are most commonly used with collections and the methods that operate on them. 


Version 2.0 of the .NET Framework class library provides a new namespace.


System.Collections.Generic, which contains several new generic-based collection classes. 


It is recommended that all applications that target Version 2.0 use the new generic collection classes instead of the older non-generic counterparts such as ArrayList. 


Use generic types to maximize code reuse, type safety, and performance.


The most common use of generics is to create collection classes.


The .NET Framework class library contains several new generic collection classes in the System.Collections.Generic namespace. These should be used whenever possible in place of classes such as ArrayList in the System.Collections namespace.


You can create your own generic interfaces, classes, methods, events and delegates.


Generic classes may be constrained to enable access to methods on particular data types.
Information on the types used in a generic data type may be obtained at run-time by means of reflection.




// type parameter T in angle brackets
public class GenericList<T> 
{
    // The nested class is also generic on T
    private class Node
    {
        // T used in non-generic constructor
        public Node(T t)
        {
            next = null;
            data = t;
        }


        private Node next;
        public Node Next
        {
            get { return next; }
            set { next = value; }
        }
        
        // T as private member data type
        private T data;


        // T as return type of property
        public T Data  
        {
            get { return data; }
            set { data = value; }
        }
    }


    private Node head;
    
    // constructor
    public GenericList() 
    {
        head = null;
    }


    // T as method parameter type:
    public void AddHead(T t) 
    {
        Node n = new Node(t);
        n.Next = head;
        head = n;
    }


    public IEnumerator<T> GetEnumerator()
    {
        Node current = head;


        while (current != null)
        {
            yield return current.Data;
            current = current.Next;
        }
    }
}


As the type of a method parameter in the AddHead method.
As the return type of the public method GetNext and the Data property in the nested Node class.
As the type of the private member data in the nested class.


// Declare the generic class
public class GenericList<T>
{
    void Add(T input) { }
}
class TestGenericList
{
    private class ExampleClass { }
    static void Main()
    {
        // Declare a list of type int
        GenericList<int> list1 = new GenericList<int>();


        // Declare a list of type string
        GenericList<string> list2 = new GenericList<string>();


        // Declare a list of type ExampleClass
        GenericList<ExampleClass> list3 = new GenericList<ExampleClass>();
    }
}
/A generic method is a method that is declared with type parameters, as follows:


static void Swap<T>(ref T lhs, ref T rhs)
{
    T temp;
    temp = lhs;
    lhs = rhs;
    rhs = temp;
}


The following code example shows one way to call the method, using int for the type argument:
public static void TestSwap()
{
    int a = 1;
    int b = 2;


    Swap<int>(ref a, ref b);
    System.Console.WriteLine(a + " " + b);
}
You can also omit the type argument and the compiler will infer it. The following call to Swap is equivalent to the previous call:


Swap(ref a, ref b);


The same rules for type inference apply to static methods as well as instance methods. The compiler is able to infer the type parameters based on the method arguments you pass in; it cannot infer the type parameters solely from a constraint or return value. Therefore type inference does not work with methods that have no parameters. Type inference takes place at compile time before the compiler attempts to resolve any overloaded method signatures. The compiler applies type inference logic to all generic methods that share the same name. In the overload resolution step, the compiler includes only those generic methods on which type inference succeeded.


Within a generic class, non-generic methods can access the class-level type parameters, as follows:


class SampleClass<T>
{
    void Swap(ref T lhs, ref T rhs) { }
}
If you define a generic method that takes the same type parameters as the containing class the compiler will generate warning CS0693 because within the method scope, the argument supplied for the inner T will hide the argument supplied for the outer T. If you require the flexibility of calling a generic class method with type arguments other than the ones provided when the class was instantiated, consider providing another identifier for the method's type parameter, as shown in GenericList2<T> in the following example.
class GenericList<T>
{
    // CS0693
    void SampleMethod<T>() { }
}
class GenericList2<T>
{
    //No warning
    void SampleMethod<U>() { }
}
Use constraints to enable more specialized operations on type parameters in methods. This version of Swap<T>, now called SwapIfGreater<T>, can only be used with type arguments that implement IComparable<T>.
void SwapIfGreater<T>(ref T lhs, ref T rhs) where T : System.IComparable<T>
{
    T temp;
    if (lhs.CompareTo(rhs) > 0)
    {
        temp = lhs;
        lhs = rhs;
        rhs = temp;
    }
}
Generic methods can be overloaded on a number of type parameters. For example, the following methods can all exist in the same class:


void DoWork() { }
void DoWork<T>() { }
void DoWork<T, U>() { }