Tuesday, October 25, 2016

Why .net cant become a platform independent language

The CLI (Common Language Infrastructure) is a standard, and as such it is designed to be
mostly platform-independent. Here, platform refers to the underlying computer architecture, including
the operating system.

.NET is Microsoft's principal implementation of the CLI and runs only on Windows systems. Therefore,
.NET is not platform-independent.

Mono is also an implementation of the CLI, but one designed to work on different platforms such as Linux
and Windows. Mono is definitely more platform-independent than .NET.

Why does .NET generate two web.config files in an MVC asp.net application?

Why does .NET generate two web.config files in an MVC asp.net application?
----------
I'd like to add to this that the Web.Config in the /Views folder is a great (if not thé) way to declare namespaces specifically for your views in. In a web application it is very possible almost every view gets a ViewModel (instead of your actual model) passed to it. Declaring the full namespace after @model or having the same @using App.Web.Viewmodels gets tedious. This way, all viewmodels are automatically available and you have to do extra work to get the real models in scope, which should then set of some alarm bells immediatly.

Also, usually an application can get a lot of extension-methods specifically for use in the view (the HTML-helper jumps into mind). It makes sense to define the namespace to this extension class in the /Views/Web.Config. That way you never wonder "Why cant IntelliSense find the @Html.ImageLink() method??!"
-----------------
You can have multiple configuration files in your project at the same level too. We do it like this. We have one main configuration file (Web.Config), then we have two more configurations files. App.Config and Database.Config. in App.Config we defined all the application level settings. in Database.Config we define all the database level settings. And in Web.Config we refer App.Config and Database.Config like this:

 <appSettings configSource="app.config">
 </appSettings>
 <connectionStrings configSource="database.config">
 </connectionStrings>
Also , you can have multiple web.config files in sub directories. Settings will be override automatically by asp.net.

Thursday, October 13, 2016

How to use session in a web service?

Add Customer.cs file...

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services; 
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

//To support Session in Web service then session class must be inheited from
//System.Web.Services.WebService
public class Customer : System.Web.Services.WebService
{
    //to Enable session it must to set EnableSession=true
    [WebMethod (EnableSession=true)]
    public int GetAddition(int Number)
    {
        //cekcing wheter the session is null
        if (Session["GetAddition"] == null)
        {
            //set session value to 0 if session is null
            Session["GetAddition"] = 0;      
        }
        else
        {
            //Add the user Input value into the existing session value
            Session["GetAddition"] = (int)Session["GetAddition"] + Number;             
        }
        //returen the session value
        return (int)Session["GetAddition"];
    }    
}
Customer.asmx
<%@ WebService Language="C#" CodeBehind="~/App_Code/Customer.cs" Class="Customer" %>
Default.aspx design
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> 
<!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></title>
</head>
<body style="background-color:Blue">
<h4 style="color:White">Article by Vithal Wadje</h4>
    <form id="form1" runat="server">
    <table style="margin-top:60px;color:White">
    <tr>
    <td>Enter Value</td>
    <td>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></td>
    </tr>
    <tr>
    <td></td><td></td>
    </tr>
     <tr>
    <td></td><td>
        <asp:Button ID="btngetAddition" runat="server" Text="Get Addition"
             onclick="btngetAddition_Click" /> </td>
    </tr>
    <tr>
    <td>
  Addition
    </td>
    <td id="tdoutput" runat="server">
   
    </td>
    </tr>
    </table>
    </form>
</body>
</html>
Default.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    { 
    }
    protected void btngetAddition_Click(object sender, EventArgs e)
    {
        Customer obj = new Customer(); 
        tdoutput.InnerText = Convert.ToString(obj.GetAddition(Convert.ToInt32(TextBox1.Text)));
    }
}
Web.config file
<?xml version="1.0"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0"/>
  </system.web>
</configuration>

Sunday, September 25, 2016

Constructor and Its Use

What is  constructor?

A special method of the class that will be automatically invoked when an instance of the class is created is called a constructor. The main use of constructors is to initialize private fields of the class while creating an instance for the class. When you
 have not created a constructor in the class, the compiler will automatically create a default constructor in the class. The default constructor initializes all numeric fields in the class to zero and all string and object fields to null.

Some of the key points regarding the Constructor are:
  • A class can have any number of constructors.
  • A constructor doesn't have any return type, not even void.
  • A static constructor can not be a parametrized constructor.
  • Within a class you can create only one static constructor. 
Constructors can be divided into 5 types:
  1. Default Constructor
  2. Parametrized Constructor
  3. Copy Constructor
  4. Static Constructor
  5. Private Constructor 
Now let us see  each constructor type with example as below
Default Constructor

A constructor without any parameters is called a default constructor; in other words this type of constructor does not take parameters. The drawback of a default constructor is that every instance of the class will be initialized to the same values and it is not possible to initialize each instance of the class to different values. The default constructor initializes:
  1. All numeric fields in the class to zero.
  2. All string and object fields to null.
Example
using System;
namespace
 DefaultConstractor
 {
  
  class addition
    {
       
 int a, b; 
        public addition()   //default contructor
        {
            a = 100;
            b = 175;
        }

        public static void Main()
        {
            addition obj = new addition(); //an object is created , constructor is called
            Console.WriteLine(obj.a);
            Console.WriteLine(obj.b);
            Console.Read();
        }
      }
    }
Now run the application, the output will be as in the following:

Parameterized Constructor 

A constructor with at least one parameter is called a parametrized constructor. The advantage of a parametrizedconstructor is that you can initialize each instance of the class to different values.
 
using System;
namespace Constructor
{
   
 class paraconstrctor
    {
     
 public  int a, b;
      
public paraconstrctor(int x, int y)  // decalaring Paremetrized Constructor with ing x,y parameter
        {
            a = x;
            b = y;
        }
   }
    class MainClass
    {
        static void Main()
        {
            paraconstrctor v = new paraconstrctor(100, 175);   // Creating object of Parameterized Constructor and ing values 
            Console.WriteLine("-----------parameterized constructor example by vithal wadje---------------");
            Console.WriteLine("\t");
            Console.WriteLine("value of a=" + v.a );
            Console.WriteLine("value of b=" + v.b);
            Console.Read();
        }
    }
}
Now run the application, the output will be as in the following:

Copy Constructor

The constructor which creates an object by copying variables from another object is called a copy constructor. The purpose of a copy constructor is to initialize a new instance to the values of an existing instance.

Syntax

public employee(employee emp)
{
name=emp.name;
age=emp.age;
}

The copy constructor is invoked by instantiating an object of type employee and ing it the object to be copied.

Example
employee emp1=new  employee (emp2);
Now, emp1 is a copy of emp2.

So let us see its practical implementation.
using System;
namespace copyConstractor
{
    class employee
    {
        private string name;
        private int age;
        public employee(employee emp)   // declaring Copy constructor.
        {
            name = emp.name;
            age = emp.age;
        }
        public employee(string name, int age)  // Instance constructor.
        {
            this.name = name;
            this.age = age;
        }
        public string Details     // Get deatils of employee
        {
            get
            {
                return  " The age of " + name +" is "+ age.ToString();
            }
        }
    }
    class empdetail
    {
        static void Main()
        {
            employee emp1 = new employee("Vithal", 23);  // Create a new employee object.
            employee emp2 = new employee(emp1);          // here is emp1 details is copied to emp2.
            Console.WriteLine(emp2.Details);
            Console.ReadLine();
        }
    }
}
Now run the program, the output will be as follows:


Static Constructor

When a constructor is created as static, it will be invoked only once for all of instances of the class and it is invoked during the creation of the first instance of the class or the first reference to a static member in the class. A static constructor is used to initialize static fields of the class and to write the code that needs to be executed only once.

Some key points of a static constructor is:
  1. A static constructor does not take access modifiers or have parameters.
  2. A static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced.
  3. A static constructor cannot be called directly.
  4. The user has no control on when the static constructor is executed in the program.
  5. A typical use of static constructors is when the class is using a log file and the constructor is used to write entries to this file.
Syntax

class employee
 {
// Static constructor
 
 static employee(){}
 }
Now let us see it with practically
using System;
namespace staticConstractor
{
public class employee
{
    static employee() // Static constructor declaration{Console.WriteLine("The static constructor ");
}
public static void Salary()
 {
    Console.WriteLine();
    Console.WriteLine("The Salary method");
 }
}
class details
{
    static void Main()
    {
        Console.WriteLine("----------Static constrctor example by vithal wadje------------------");
        Console.WriteLine();
        employee.Salary();
        Console.ReadLine();
    }
  }
}
Now run the program the output will be look as in the following:

Private Constructor 
When a constructor is created with a private specifier, it is not possible for other classes to derive from this class, 
neither is it possible to create an instance of this class. They are usually used in classes that contain static members
only. Some key points of a private constructor are:
  1. One use of a private constructor is when we have only static members.
  2. It provides an implementation of a singleton class pattern
  3. Once we provide a constructor that is either private or public or any, the compiler will not add theparameter-less public constructor to the class.
Now let us see it practically.
using System;
namespace defaultConstractor
{
    public class Counter
    {
        private Counter()   //private constrctor declaration
        {
        }
        public static int currentview;
        public static int visitedCount()
        {
            return ++ currentview;
        }
    }
    class viewCountedetails
    {
        static void Main()
        {
            // Counter aCounter = new Counter();   // Error
            Console.WriteLine("-------Private constructor example by vithal wadje----------");
            Console.WriteLine();
            Counter.currentview = 500;
            Counter.visitedCount();
            Console.WriteLine("Now the view count is: {0}", Counter.currentview);
            Console.ReadLine();
        }
    }
}
Now run the application; the output is:

If you uncomment the preceding statement that is commented in the above program then it will generate an error
because the constructor is inaccessible (private).
Summary: