September 3, 2013

AddressAccessDeniedException: HTTP could not register URL http:// in Self Hosting WCF

Below Code I was using to Self Host my WCF Service.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Description;

namespace ConsoleApplication1
    public interface IService

        string GetData(int value);

        //written by Tanvi
        string GetString(String theString);

    public class Service : IService
        public string GetData(int value)
            return string.Format("You entered: {0}", value);

              public string GetString(String theString)
            return "Hello First WCF Service" + theString;
    class Program
        static void Main(string[] args)
            Uri baseAddress = new Uri("http://localhost:81/TestService");

            // Create the ServiceHost.
            using (ServiceHost host = new ServiceHost(typeof(Service), baseAddress))
                // Enable metadata publishing.
                ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
                smb.HttpGetEnabled = true;
                smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;

                host.AddServiceEndpoint(typeof(IService), new BasicHttpBinding(), "TestService");
                // Open the ServiceHost to start listening for messages. Since

                Console.WriteLine("The service is ready at {0}", baseAddress);
                Console.WriteLine("Press <Enter> to stop the service.");

                // Close the ServiceHost.


And Got an Error "AddressAccessDeniedException: HTTP could not register URL http:// ......."
The problem was Operating System User A/c did not have enough privileges to access this address.
So When I open my Visual Studio in "Run As Administrator" Mode , Right click on visual Studio and Click on Run as Administrator, the problem was solved.

July 25, 2013

Code Example for Wizard Server Control in ASP.NET

Below is the code example for wizard control in ASP.NET

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ExWizard.aspx.cs" Inherits="ExWizard" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<html xmlns="">
<head runat="server">

    <form id="form1" runat="server">
    <asp:wizard ID="Wizard1" runat="server" DisplayCancelButton="True"> 
     <WizardSteps> <asp:WizardStep ID="WizardStep1" title="Personal Info"  runat="server">
             Name <asp:TextBox ID="txtName" runat="server"/><br />
             Phone  <asp:TextBox ID="txtPhone" runat="server"/>

     <asp:WizardStep ID="WizardStep2" title="Company Info"     runat="server">
              Company Name <asp:TextBox ID="txtCompName" runat="server"/><br />
              Company Phone <asp:TextBox ID="txtCompPhone" runat="server" />
         <asp:WizardStep runat="server" StepType="Complete">

         <asp:Label runat="server" Text="" id="lblPersonal"></asp:Label>
         <asp:Label runat="server" Text="" id="lblComp"></asp:Label> </asp:WizardStep>
     </WizardSteps> </asp:wizard>

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class ExWizard : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)
        lblPersonal.Text = txtName.Text + "  " + txtPhone.Text;
        lblComp.Text = txtCompName.Text + "  " + txtCompPhone.Text;

July 15, 2013

Parameters in C# code example

Set of arguments that must be provided for that method are parameters 

  Different ways of passing parameters in C# 
               Pass by Value (Value Of Parameter Passed)
      Pass by ref  (Memory Location or Ref of Parameter is passed)
      Out (  variable is assigned a value before returning from a method)
      Params (  any number of parameters of a particular type)

See the below code example 

using System;
using System.Text;
class Test
    static void DisplayUsingValue(int p) { ++p; }
    static void DisplayUsingRef(ref int p) { ++p; }
    static void DisplayUsingOut(int p , out int y)

    static int Add(params int[] iarr)
        int sum = 0;
        foreach (int i in iarr)
            sum += i;
        return sum;

    static void Main()
        int x = 8;
        DisplayUsingValue(x); // make a copy of the value-type x
        Console.WriteLine("output From Pass By Value Method Demo  ");
        Console.WriteLine(x); // x will still be 8

        DisplayUsingRef(ref x);
        Console.WriteLine("output From Pass By Ref Method Demo  ");

        int a;
        DisplayUsingOut(x, out a);
        Console.WriteLine("output From out parameter  Demo  ");

        int result = Add(1, 2,7);
        Console.WriteLine("output From params[]  Demo  ");


And See the output 
"output From Pass By Value Method Demo
output From Pass By Ref Method Demo
output From out parameter  Demo 
output From params[]  Demo