Friday, October 26, 2018

Difference between the 'var' and 'dynamic'

var           -    The type of the variable declared is decided at compile time
dynamic   -    The Type of the variable declared is decided at run-time. 
  • variable declared using var should be initialized at time of declaration. However variables declared using keyword dynamic don't have to initialize at time of declaration.
  • The type of variable value assigned to var is decided at compile time and cannot be assigned with other types in the code later, having though gives compile. However variables declared using dynamic, compiler will not check for the type errors at the time of compilation. So variable can be assigned with values of any type through the code consecutively.
  • variable declared using var gives the intelliSense. However the variable using dynamic doesn't show the intelliSense because the type is decided at run-time, compiler never knows its type during compilation time.
Code Snippet:

            var str = "Hello Developer.!"; // str is decided as type string at compile time.
            str = 1;
            // compiler error - Cannot implicitly convert type 'int' to 'string'
           

            dynamic dn = 1001;
            dn = "text"; 

            // NO error is thrown by compiler, though variable is assigned to string. compiler will create the type of dn as integer and then it recreates the type as string when the string is assigned to it.


Now,
dynamic dn;
dn = "text";
var vr = dn;

vr = 1001;

// vr is assigned with dynamic dn - So vr type is decided at run-time.

Friday, September 21, 2018

Check the current Identity and Reset the Identity

Code Snippet:



// Check the current identity of the table
SELECT IDENT_CURRENT('tablename') 


 //Reset the identity to next incremental value
DBCC CHECKIDENT ('tablename', RESEED, 0)




When RESEED is used then the numbervalue in third argument of the CHECKIDENT is set as current identity for the table. Here in this case RESEED to 0 means 



Calling a WebMethod from User control in ASP.NET

This is a framework limitation, we cannot call the webmethod in the code behind file of user control from ascx file.

However try calling the webmethod in aspx code behind file from the ascx file. This will work

Monday, September 17, 2018

jQuery Ajax GET with params using WebMethod in c#

jQuery Ajax GET with params using WebMethod in c#

Code Snippet:
UI:
            <asp:Button ID="Button1" runat="server" Text="Call Ajax Get" OnClientClick="AjaxCall2();" />


jQuery:

        function AjaxCall2() {
            debugger;
            $.ajax({
                type: "GET",
                url: "AjaxTest.aspx/TestAjaxCallGET?name='Radha'",
                contentType: "application/json; charset=utf-8",
                dataType: "text",
                async: true,
                success: function (response) {
                    alert("Success");
                },
                failure: function (response) {
                    alert("failure");

                }
            });
        }


WebMethod C#

        [WebMethod]
        [System.Web.Script.Services.ScriptMethod(UseHttpGet = true)]
        public static string TestAjaxCallGET(string name)
        {
            return name;
        }

jQuery Ajax POST using WebMethod in c#

Code Snippet:

UI:

<asp:Button ID="AjaxCall" runat="server" Text="Call Ajax" OnClientClick="AjaxCall1();" />


jQuery Ajax:

        function AjaxCall1() {
            debugger;
            $.ajax({
                type: "POST",
                url: "AjaxTest.aspx/TestAjaxCall",
                data: '{postParam: "Hello Ajax web method" }',
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                async: true,
                success: function (response) {
                    alert("Success");
                },
                failure: function (response) {
                    alert("failure");

                }
            });
        }

c# WebMethod:

        [WebMethod]
        public static string TestAjaxCall(string postParam)
        {
            return postParam;       
        }




Sample snippet that explains jQuery Ajax POST using WebMethod in c#


Thursday, September 6, 2018

Types of Inheritance in c#

Single Inheritance - Derived class is created from a single base class. SUPPORTED

Multi-level inheritance - Derived class is created from another derived class. SUPPORTED

Multiple inheritance - A derived class is created from more than one base class. NOT SUPPORTED

Multipath inheritance - In this inheritance, a derived class is created from multiple derived classes and each of these derived classes have same base class. NOT SUPPORTED

Hierarchial inheritance - In this inheritance, more than one derived classes are created from a single base. SUPPORTED

Hybrid inheritance - This is combination of more than one inheritance. So depending on type of combination we can say SUPPORTEDor NOT SUPPORTED

Friday, March 23, 2018

Sql Server: Get count of * or distinct rows for column A grouped by column B

Scenario 1 - count of * rows:

A table has 10 records. Column B has duplicate rows with different values in Column A

then., Sql Query:


Code Snippet:
SELECT COUNT(ColumnA) AS Count_Headertext,
ColumnB AS ColumnB_Headertext
FROM Tablename
GROUP BY ColumnB



Scenario 2 - count of distinct rows:

A table has 10 records. Column B has duplicate rows with different values in Column A

then., Sql Query:


Code Snippet:
SELECT COUNT(DISTINCT ColumnA) AS Count_Headertext,
ColumnB AS ColumnB_Headertext
FROM Tablename
GROUP BY ColumnB



Tuesday, March 20, 2018

Field and Property in C#

Object orientated programming principles say that the internal workings of a class should be hidden from the outside world. If you expose a field you're in essence exposing the internal implementation of the class. So we wrap the field using the property to hide the internal working of the class.

We can see that in the above code section we define property as public and field as private, so user can only access the property but internally we are using the field, such that provides a level of abstraction and hides the field from user access.

Another  important difference is that interfaces can have properties but not fields.

Using property we can throw an event but this is not possible in field.

A private field that stores the data exposed by a public property is called a backing store or backing field.

Code Snippet:

        public class Career
        {
            // field
            private int _path; 

            // Path is Property
            public int Path
            {
                get
                {
                    return _path;
                }
                set
                {
                    _path = value;
                }
            }
        }