Friday, September 21, 2018

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;
                }
            }
        }

Friday, July 28, 2017

Unable to find the service reference in Portable class library / Also unable to update service reference in Portable Class Library

Unable to find the service reference in Portable class library / Also unable to update service reference in Portable Class Library


I have created a portable class library for my cross platform application. However my plan is to consume the WCF methods for the data access.

Now the issue is...........

When I am trying to add the service reference in the portable class library - I don't see any provision to do it.

Here is the quick solution I figured out!...


Step 1:

Go to the application (ClassLibrary) properties and uncheck the Windows Phone 8.1 and uncheck the ASP.NET Core 1.0 and check the Silverlight (This is a temporary stuff until you will be able to see the provision to add service reference.)



Step 2:

Now you could see the Add service reference. So, create the service reference by entering the Url, give a meaning full name for service reference. Click ok

Step 3:

Rename Packages.config to Packages.config.bak - We are doing this to temporarily remove the package references.

Step 4:

Now revert back the changes in the 'Change Targets' window (displayed image above).

Step 5:

Revert back the file name for Packages.config.bak to Packages.config. Finally build the project without any errors.

If still errors exist comment on the post for quick answers.