Pages

Thursday, November 18, 2010

Web Services - Method Overloading

This is not as same as we have in the Class. It will be build successfully but when we use or consume, it will generate error message - "Use the MessageName property of the WebMethod custom attribute to specify unique message names for the methods". when WSDL (Web Service Description Language) is generated, it will not be able to make the difference between methods because WSDL does not deal on the base of parameters.
To overcome this just do the following: Also add the following line above the class

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.None)]
[WebMethod(MessageName = "Print1", Description = "Print 1", EnableSession = true)]
    public int A(int a, int b)
    {         return 1;     }
    [WebMethod(MessageName = "Print2", Description = "Print 2", EnableSession = true)]
    public int A(int x)
    {         return 2;     }
Reason
By passing the 'MessageName Property’ it changes the method name in the WSDL as shown below:
<wsdl:operation name="A">
            <wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">Print 1</wsdl:documentation>
            <wsdl:input name="Print1" message="tns:Print1SoapIn" />
            <wsdl:output name="Print1" message="tns:Print1SoapOut" />
</wsdl:operation>
<wsdl:operation name="A">
            <wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">Print 2</wsdl:documentation>
            <wsdl:input name="Print2" message="tns:Print2SoapIn" />
            <wsdl:output name="Print2" message="tns:Print2SoapOut" />
</wsdl:operation>

About Multicast Delegates

Multicast Delegate
Delegate that holds the reference of more than one method.
Multicast delegates must contain only methods that return void, otherwise run-time exception will be thrown.
Example
delegate void DelegateMulticast(int x);
Class ClassX
{
    static void A(int x)
    {
        Console.WriteLine("Hello A");
    }
    static void B(int x)
    {
        Console.WriteLine("Hello B");
    }
    public static void Main()
    {
        DelegateMulticast func = new DelegateMulticast(A);
        func += new DelegateMulticast(B);
        func(1);             // Both A and B are called
        func -= new DelegateMulticast(A);
        func(2);             // Only B is called
    }
}
Description
As you see that there are 2 methods named A & B with one parameter and void as return type.
Create delegate object
DelegateMulticast func = new DelegateMulticast(A);
Then the Delegate are added and removed using the += operator and -= operator resp.

Wednesday, October 27, 2010

Site Down For Maintenance - app_offline.htm

Sometimes we require to display the Site Under Construction message to the visitor while we are fixing bugs, upgrading features etc. For this just place the app_offline.htm on the root directory of the application.When ASP.Net sees that it will shut down the application and stop processing any new incoming request for that application. ASP.Net will send the content of this file "app_offline.htm" for all the incoming request. This file must have at least 512 bytes of content within it. If you do not have that content just place some content and comment them.

Tuesday, October 19, 2010

Display country with flags in the dropdown using javascript

Sometime it is required to display images with the text in the dropdown.
Here is the code to display the images in the dropdown.
Create a folder "flags" and place the flag images there.

<%@ Page Language="C#" AutoEventWireup="true"  %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Display images in dropdown</title>
    <script type="text/javascript">
    function SetFlag()
{ 
    var ddlCountry=document.getElementById('ddlCountry');              
    var iso = ddlCountry.options[ddlCountry.selectedIndex].value; 
  if (iso.length > 0)
  {
    document.getElementById('imgFlag').src =  "flags/" + iso + ".png";
    ddlCountry.options[ddlCountry.selectedIndex].style.backgroundImage = "url(flags/" + iso + ".png" +")";
    ddlCountry.options[ddlCountry.selectedIndex].style.backgroundRepeat="no-repeat";
    ddlCountry.options[ddlCountry.selectedIndex].style.backgroundPosition="right";    
  }
  else
    document.getElementById('imgFlag').src =  "flags/pixel.gif";
}
function getDropdownListTextWithImage()
{
   var ddlCountry=document.getElementById('ddlCountry');       
    for(var j = 0, limit1 = ddlCountry.options.length; j < limit1;  j++)
    {
        ddlCountry.options[j].style.backgroundImage = "url(flags/" + ddlCountry.options[j].value + ".png" +")";
        ddlCountry.options[j].style.backgroundRepeat="no-repeat";
        ddlCountry.options[j].style.backgroundPosition="right";       
    }
}
    </script>
</head>
<body onload="SetFlag();getDropdownListTextWithImage();">
    <form id="form1" runat="server">
    <div style="background-repeat:no-repeat;background-position:top right">
     <asp:DropDownList runat="server" ID="ddlCountry" onchange="SetFlag();" onkeydown="SetFlag();" onkeyup="SetFlag();" TabIndex="4" EnableViewState="false" ValidationGroup="AddComment"
     >
     <asp:ListItem value="">[Not specified]</asp:ListItem>
    <asp:ListItem value="al" >Albania </asp:ListItem>
    <asp:ListItem value="dz">Algeria</asp:ListItem>
    <asp:ListItem value="ar">Argentina</asp:ListItem>
    <asp:ListItem value="am">Armenia</asp:ListItem>
    <asp:ListItem value="au">Australia</asp:ListItem>
    <asp:ListItem value="at">Austria</asp:ListItem>    
     </asp:DropDownList>&nbsp;
  <asp:Image runat="server" ID="imgFlag" AlternateText="Country flag" Width="16" Height="11" EnableViewState="false" />
    </div>
    </form>
</body>
</html>

Display Rupee Symbol Website

To display the Rupee symbol on the website, just place this line in the <head> section and it will convert all the 'Rs' / 'Rs.' for you.

<script src="http://cdn.webrupee.com/js" type="text/javascript"></script>

Tuesday, October 12, 2010

Difference between ref & out

The out and the ref  parameters are very useful when your method needs to return more than one values.
The out Parameter
1. To use an out parameter, both the method definition and the calling method must explicitly use the out keyword.
2. Variables passed as an out arguments need not be initialized prior to being passed.
class Example
{
    static void Method(out int i)
    {
        i = 90;
    }
    static void Main()
    {
        int k;
        Method(out k);
        // k is now 90
    }
}
3. The calling method is required to assign a value before the method returns.
class Example
{
    static void Method(out int i)
    {
        i = 90;
    }
    static void Main()
    {
        int k;
        Method(out k);  // k is now 90       
    }
}
The ref parameter
1. ref requires that the variable be initialized before being passed.
class Example
{
    static void Method(ref int i)
    {
        i += 30;
    }
    static void Main()
    {
        int k=3;
        Method(ref k);  // k is now 33       
    }
}

Note:
Methods cannot be overloaded if one method takes a ref argument and the other takes an out argument. These two methods, for example, are identical in terms of compilation, so this code will not compile:
class Example
{
    // compiler error CS0663: "cannot define overloaded
    // methods that differ only on ref and out"
    public void Method(out int i) {  }
    public void Method(ref int i) {  }
}
Overloading can be done, however, if one method takes a ref or out argument and the other uses neither, like this:
class Example
{
    public void Method(int i) {  }
    public void Method(out int i) {  }
}