Friday, 15 July 2011

ASP.Net 3.5 Vs 4.0



Some of the new features that are implemented in ASP.Net version 4.0 are


a)Optional parameters

We can implement a feature like function overloading using Optional parameter.
When we are declaring a function with optional parameters we don’t need to supply the argument for the optional parameter. If we do not supply its value the default value will be used else it will take the passed argument as its value. Optional parameter must be specified as the last parameter.

Sample Code :

class Program
    {
        static void Main(string[] args)
        {
            //Optional Parameter
            //We can call function foo by passing values for only first parameter
            int x = foo(5); // result will be 0. (5 * 0)
            int y = foo(5,2); // result will be 10. (5 * 2)
        }
                
        public static int foo(int a, int b = 0)
        {
            return a * b;
        }
    }


b)Named Parameters
It gives us the flexibility in the order of the passed arguments.  We need not consider the order of the parameters as we can give our own order by specifying the parameter name with the argument.

For example :

class Program
    {
        static void Main(string[] args)
        {
            string y = foo(b: "B", a: "A");// Output wil be B
        }

        public static string foo(string a, string b)
        {
            return b;
        }
    }

Its output will be "B".

c) Dynamic variables 
We don't need to initialize dynamic variables. Compiler is not able to identify its type at runtime,

For Example :

     dynamic d; // You don’t need to initialize the variable
     d = "hi";
     int Len = d.Hi
();//This code compiles fine. But it will through an exception at runtime.
          //when this line of code is executed the compiler will come to know its type and identifies that this object does not have a property called "Hi()"
     int Len = d.Hi();//This code compiles fine. But it will through an exception at runtime.
          //when this line of code is executed the compiler will come to know its type and identifies that this object does not have a property called "Hi()"

Boxing and Unboxing


Boxing


It is the conversion of value type to object type. It makes the value type to be stored on garbage collected heap. Boxing a value type allocates an object instance on the heap and copies the value into the new object.
When we are saving values to an arraylist we need to box the value type (like  int, string etc) to reference type(object);

For example:
int I = 0;
Object o = (object) I;

Unboxing
It is the reverse of boxing. It converts reference types to value types.
An unboxing operation consists of:
Checking the object instance to make sure that it is a boxed value of the given value type.
Copying the value from the instance into the value-type variable.
For example:
int i = 123;      // a value type
object o = i;     // boxing
int j = (int)o;  

How to toggle visibility of row group in SSRS?.


For eg:
Right click on Row Groups -> Details ->  Group Properties -> Visibility -> 
Check "Display can be toggled by this report item" checkbox. Then select



the textbox name (You can get this name from textbox properties)  which groups your data. Now this will create a toggle display for this group.

How to popup a browser window from SSRS report?.


If you want to popup a browser window from a table or textbox then

Right click on the textbox control ->Action->Select URL

In this textbox you can paste your javascript code.

Example :

="javascript:void(JavaScriptFunctionName('" +Fields!DataSetvalue.Value + "')"

If you don't need to pass parameters to Javascript function then

="javascript:void(JavaScriptFunctionName()"

How to check for foreign key dependency before deleting a row in MSSQL


Sample Query

declare @TabName varchar(150) = 'TableName'
declare @ColName varchar(150) = 'ColumnName'
declare @Value varchar(150) = Specify Value
declare @sql varchar(MAX) = ''
Declare @Temp table ( Cnt int)

;With ReferencedKeys as
(
    select    o1.name as Referencing_Object_name, c1.name as referencing_column_Name
        , o2.name as Referenced_Object_name    , c2.name as Referenced_Column_Name
        , s.name as Constraint_name
    from sysforeignkeys fk
        inner join sysobjects o1 on fk.fkeyid = o1.id
        inner join sysobjects o2 on fk.rkeyid = o2.id
        inner join syscolumns c1 on c1.id = o1.id and c1.colid = fk.fkey
        inner join syscolumns c2 on c2.id = o2.id and c2.colid = fk.rkey
        inner join sysobjects s on fk.constid = s.id
    where o2.name = @TabName and c2.name = @ColName
)
select @sql += 'Select ' + referencing_column_Name + ' from ' + Referencing_Object_name 
    + ' where ' + referencing_column_Name + '=' + @Value + ' union all '
from ReferencedKeys

select @sql = Substring(@sql,0,LEN(@sql) - 9)

insert into @Temp
exec (@sql)

select * from @Temp

--Idea got from SQL Authority.com

Thursday, 7 July 2011

what are the differences between dynamic and var keywords in C#


1)var


You must have to initialize var variables at the time of declaration.
The compiler can identify its type at compile time.

2)dynamic

You dont need to initialize dynamic variables.
The compiler would not be able to identify its type at runtime, 

For Eg:
//var v; This is an error. You must have to initialize the variable
 var v = "v";// It is a string type
 //v.Hi(); This will create a compile time error because compiler knows the variable type during compile time. Here Hi() is not belongs to string type objects. So it will rise an error.
 int len = v.Length;//It is ok because string objects do have Length property

dynamic d; // You dont need to initialize the variable
d = "hi";
int Len = d.Hi();//This code compiles fine. But it will through an exception at runtime.
//when this line of code executes compiler comes to know its type and identifies that this object does not have a property called "Hi()".