Saturday, 22 August 2015

Default error page in web.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.web>
         <customErrors defaultRedirect="error.htm" mode="On" />
    </system.web>
</configuration>

Stack and Heap

Stack is used for static memory allocation and Heap for dynamic memory allocation, both stored in the computer's RAM .

Stack:

Variables allocated on the stack are stored directly to the memory and access to this memory is very fast, and it's allocation is dealt with when the program is compiled. When a function or a method calls another function which in turns calls another function etc., the execution of all those functions remains suspended until the very last function returns its value. The stack is always reserved in a LIFO order, the most recently reserved block is always the next block to be freed. This makes it really simple to keep track of the stack, freeing a block from the stack is nothing more than adjusting one pointer.

Heap :

Variables allocated on the heap have their memory allocated at run time and accessing this memory is a bit slower, but the heap size is only limited by the size of virtual memory . Element of the heap have no dependencies with each other and can always be accessed randomly at any time. You can allocate a block at any time and free it at any time. This makes it much more complex to keep track of which parts of the heap are allocated or free at any given time.







Managed code and unmanaged code in .NET

Managed Code

The resource, which is with in your application domain is, managed code. The resources that are within domain are faster.

The code, which is developed in .NET framework, is known as managed code. This code is directly executed by CLR with help of managed code execution. Any language that is written in .NET Framework is managed code.

Managed code uses CLR which in turns looks after your applications by managing memory, handling security, allowing cross - language debugging, and so on.

Unmanaged Code

The code, which is developed outside .NET, Framework is known as unmanaged code.

Applications that do not run under the control of the CLR are said to be unmanaged, and certain languages such as C++ can be used to write such applications, which, for example, access low - level functions of the operating system. Background compatibility with code of VB, ASP and COM are examples of unmanaged code.

Unmanaged code can be unmanaged source code and unmanaged compile code.

Unmanaged code is executed with help of wrapper classes.



Applicants in a .NET interview should be able to point out that all Intermediate Language (IL) is managed code. (See Question 15 below.) One difference between managed and unmanaged code is that Visual Basic and C# compiler create managed code in .NET, but the only way to get unmanaged code is to write the application in C or C++. Since the .NET compiler creates managed code, this type of code does not depend on the target machine’s architecture. The Common Language Runtime (CLR) executes it, and not the operating system (OS). In contrast, unmanaged code is dependent on the architecture of the target machine. It is executed directly by the OS and directly compiled to native machine code.

MVC, MVP and MVVM Design Patterns

MVC Pattern

MVC stands for Model-View-Controller. It is a software design pattern which was introduced in 1970s. Also, MVC pattern forces a separation of concerns, it means domain model and controller logic are decoupled from user interface (view). As a result maintenance and testing of the application become simpler and easier.
MVC design pattern splits an application into three main aspects: Model, View and Controller
  1. Model

    The Model represents a set of classes that describe the business logic i.e. business model as well as data access operations i.e. data model. It also defines business rules for data means how the data can be changed and manipulated.
  2. View

    The View represents the UI components like CSS, jQuery, html etc. It is only responsible for displaying the data that is received from the controller as the result. This also transforms the model(s) into UI.
  3. Controller

    The Controller is responsible to process incoming requests. It receives input from users via the View, then process the user's data with the help of Model and passing the results back to the View. Typically, it acts as the coordinator between the View and the Model.
Today, this pattern is used by many popular framework like as Ruby on Rails, Spring Framework, Apple iOS Development and ASP.NET MVC.

MVP pattern

This pattern is similar to MVC pattern in which controller has been replaced by the presenter. This design pattern splits an application into three main aspects: Model, View and Presenter.
  1. Model

    The Model represents a set of classes that describes the business logic and data. It also defines business rules for data means how the data can be changed and manipulated.
  2. View

    The View represents the UI components like CSS, jQuery, html etc. It is only responsible for displaying the data that is received from the presenter as the result. This also transforms the model(s) into UI.
  3. Presenter

    The Presenter is responsible for handling all UI events on behalf of the view. This receive input from users via the View, then process the user's data with the help of Model and passing the results back to the View. Unlike view and controller, view and presenter are completely decoupled from each other’s and communicate to each other’s by an interface.
    Also, presenter does not manage the incoming request traffic as controller.
This pattern is commonly used with ASP.NET Web Forms applications which require to create automated unit tests for their code-behind pages. This is also used with windows forms.

Key Points about MVP Pattern:

  1. User interacts with the View.
  2. There is one-to-one relationship between View and Presenter means one View is mapped to only one Presenter.
  3. View has a reference to Presenter but View has not reference to Model.
  4. Provides two way communication between View and Presenter.

MVVM pattern

MVVM stands for Model-View-View Model. This pattern supports two-way data binding between view and View model. This enables automatic propagation of changes, within the state of view model to the View. Typically, the view model uses the observer pattern to notify changes in the view model to model.
  1. Model

    The Model represents a set of classes that describes the business logic and data. It also defines business rules for data means how the data can be changed and manipulated.
  2. View

    The View represents the UI components like CSS, jQuery, html etc. It is only responsible for displaying the data that is received from the controller as the result. This also transforms the model(s) into UI..
  3. View Model

    The View Model is responsible for exposing methods, commands, and other properties that helps to maintain the state of the view, manipulate the model as the result of actions on the view, and trigger events in the view itself.
This pattern is commonly used by the WPF, Silverlight, Caliburn, nRoute etc.

Key Points about MVVM Pattern:

  1. User interacts with the View.
  2. There is many-to-one relationship between View and ViewModel means many View can be mapped to one ViewModel.
  3. View has a reference to ViewModel but View Model has no information about the View.
  4. Supports two-way data binding between View and ViewModel.

Sunday, 2 August 2015

Insert the data from view With MVC


Controller 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication1.Models;

namespace MvcApplication1.Controllers
{
    public class TestController : Controller
    {
        //
        // GET: /Test/

     

        public ActionResult Test()
        {
            return View("Test");
        }



        [HttpPost]
        public ActionResult Test(ModelTest e, string Command)
        {
            if (Command == "Submit")
            {
                e.Insert();
                e.Reset();
               
            }

            return View("Test");
        }

    }

}

Model :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;

namespace MvcApplication1.Models
{
    public class ModelTest
    {
        public string Name { get; set; }
        public string Address { get; set; }
        public int Phone_landline { get; set; }
        public int Mobile { get; set; }
        public string Relation { get; set; }

        public string  Insert()
        {
            //SqlConnection con = null;
            string result = "";
            try
            {
                string s = "Data Source=XXXX;Database=EmpDetails;Trusted_Connection=Yes;";
                //SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["mycon"].ToString());
                SqlConnection con=new SqlConnection(s);
                SqlCommand cmd = new SqlCommand("spcontacts", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@Flage", "I");
                // i will pass zero to MobileID beacause its Primary .
                cmd.Parameters.AddWithValue("@Name", Name);
                cmd.Parameters.AddWithValue("@Address", Address);
                cmd.Parameters.AddWithValue("@Phone", Phone_landline);
                cmd.Parameters.AddWithValue("@Mobile", Mobile);
                cmd.Parameters.AddWithValue("@Relaction", Relation);
                con.Open();
                result = cmd.ExecuteScalar().ToString();
                return result;
            }
            catch
            {
                return result = "";
            }
            finally
            {
                //con.Close();
            }
        }

        public void Reset()
        {
            Name = string.Empty;
            Address = string.Empty;
            Phone_landline = Convert.ToInt16(string.Empty);
            Mobile = Convert.ToInt16( string.Empty);
            Relation = string.Empty;
        }

    }


}

View :

@model MvcApplication1.Models.ModelTest
@{
    ViewBag.Title = "Test";
}




<h2>Test</h2>
@using (Html.BeginForm())
{
<table><tr><td>Name</td><td>@Html.TextBoxFor(a=>a.Name)</td></tr>
<tr><td>Address</td><td>@Html.TextBoxFor(a=>a.Address)</td></tr>
<tr><td>LandLine No.</td><td>@Html.TextBoxFor(a=>a.Phone_landline)</td></tr>
<tr><td>Mobile/Cell</td><td>@Html.TextBoxFor(a=>a.Mobile)</td></tr>
<tr><td>Comments/Relations</td><td>@Html.TextBoxFor(a=>a.Relation)</td></tr>
<tr><td></td><td><input id="Submit1" type="submit" value="Submit" name="Command" /></td></tr>
<tr><td></td><td></td></tr></table>
}