Monday 29 February 2016

Code-Behind for Web Application Projects

In this tutorial we will learn the code-behind model and the project structure of pages built For Web Application Projects.
ASP.NET Pages in web project have associated with two files  one is a .aspx file that contains the html and declarative server control mark-up, and the other page is a .cs "code-behind" file that contains the UI logic for the page
Control mark-up declarations are defined within the .aspx file itself.  For example:
And corresponding protected field declarations are added in the .cs code-behind class that match the name and type of controls defined within the .aspx file. For example:
Here we want to show date in the label when we select date from the calendar,so for that see the following code.

You can then set a breakpoint (press the F9 key on the line to set it on), and then hit F5 to compile, run and debug the page:


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

namespace FirstWebApplication
{
    public partial class FirstWebPage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!Page.IsPostBack)
            {
                Calendar1.SelectedDate = DateTime.Now;
                Label1.Text = "please select a date from calendar";
            }
        }
    }
}

Now if you run your application your result will be

Handling server events from controls in our .aspx page
To handle a server event from a control on your page, you can either manually add an event-handler to the control yourself (by overridng the OnInit method in your code-behind class and adding the event delegate there), or by using the WYSIWYG designer to generate an event handler.
You can double-click on any of the events to automatically add a default named event handler.  Alternatively, you can type the name of the event handler you wish to generate.

You can then add whatever code you want within the code-behind file:

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

namespace FirstWebApplication
{
    public partial class FirstWebPage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!Page.IsPostBack)
            {
                Calendar1.SelectedDate = DateTime.Now;
                Label1.Text = "please select a date from calendar";
            }
        }

        protected void Calendar1_SelectionChanged(object sender, EventArgs e)
        {
            Label1.Text = "Your selected date from calendar is '" + Calendar1.SelectedDate.ToShortDateString() +"'";
        }
    }
}
Now run your application and select a date from calendar then o/p will be




asp.net first web application


In this tutorial we will learn how to create, build and run your first asp.net web app using C# and the ASP.NET.
Creating a New Project

Select File->New Project within the Visual Studio 2013. This will open the New Project dialog screen. Click on the “Visual C#” node in the tree-view on the left hand side of the dialog box and choose the "ASP.NET Web Application" icon:





Choose location where you want this project to be created on system Then give the name of this project and click ok.

Visual Studio will then create and open a new web project within the solution explorer. By default it will create a single page (Default.aspx), an AssemblyInfo.cs file in properties , as well as a web.config file.

Visual stdio 2005 screen-


Visual stdio -2013


In VS-2005 this default.aspx will be empty page but in VS-2013 there will be created page means some text are there by default created by the Microsoft.
So for understanding purpose we will add a new item in this project name will be “FirstWebPage.aspx”.
So for that right click on your application and click on add new item and select a empty web Form give the for this and click Add.




After click on Add new item following screen will open



After click on Add button this “FirstWebPage.aspx” webform will add to the project.
Now click on “FirstWebPage.aspx” webform it will open as HTML format(Source file) we can see it below of that page. We can go in design mode from there.
We can made changes in screen from Source mode and design mode.

Here we are drag and drop a calendar and a label from toolbox .we can change view of calendar using calendar properties.



Html Code will be

<body>
    <form id="form1" runat="server">
    <div>
   
        Hello your first asp.net Application<br />
        <br />
        <asp:Calendar ID="Calendar1" runat="server" BackColor="White" BorderColor="Black" BorderStyle="Solid" CellSpacing="1" Font-Names="Verdana" Font-Size="9pt" ForeColor="Black" Height="250px" NextPrevFormat="ShortMonth" Width="330px">
            <DayHeaderStyle Font-Bold="True" Font-Size="8pt" ForeColor="#333333" Height="8pt" />
            <DayStyle BackColor="#CCCCCC" />
            <NextPrevStyle Font-Bold="True" Font-Size="8pt" ForeColor="White" />
            <OtherMonthDayStyle ForeColor="#999999" />
            <SelectedDayStyle BackColor="#333399" ForeColor="White" />
            <TitleStyle BackColor="#333399" BorderStyle="Solid" Font-Bold="True" Font-Size="12pt" ForeColor="White" Height="12pt" />
            <TodayDayStyle BackColor="#999999" ForeColor="White" />
        </asp:Calendar>
        <br />
        <br />
        <asp:Label ID="Label1" runat="server" Height="27px" Text="Label" Width="200px"></asp:Label>
   
    </div>
    </form>
</body>


When you will run this application you will see this page on browser ,but before this we have to set “Startup project “ for that right click on “FirstWebPage.aspx” and set it as startup project  and run this application or you can see this page using open this browser With.






Thursday 25 February 2016

What is asp.net

Asp.net is a web application which is developed by the Microsoft to build the dynamic data driven Web application and Web services.
1.   Asp.Net is abbreviated as “Active Server Pages”.
2.   Asp.net is the subset of Dot net framework. A framework is a collection of classes.
3.   Asp.Net is introduced in 2002 with the 2.0 Dot net framework.
4.   Before this technology we was using “Classic Asp” to build web application.
What is Web Application?
A web application is a application which is assessable by the user using the browser.
Example of the web browsers are
1.   Internet Explorer
2.   Google Chrome
3.    Mozilla FireFox
4.    Apple Safari
5.    Netscape Navigator
Advantages of the web application :>
1.   Web application just need to installed on the web server, but for the windows application we have to installed this on every system.
2.   Maintains and other things are easier to provide.
3.   Only a browser is required for accessing a web application on the client machine.
4.   Cross platform.
5.   Internet is required for accessing the application.

How Web application work
1.   A web application work on the Client/Server architecture .
2.   Only a browser needed which can understand HTML for accessing a web application.
3.   On the server side , the web application run under the “Internet Information Server(IIS)”.

When a client enter URL or user send a request through the browser then this information is received by the web server , after that this request is proceed by the application. The application converts it to HTML and passes it to IIS Server. The IIS Server send this HTML to the client in user interface format.
This all communication is done on the internet using the HTTP. HTTP is the Hyper Text Transfer protocol.
This HTTP protocols stand how to  two  or more items communicate.




Wednesday 17 February 2016

Generic and Non Generic Collections in C#

What is Collections?
            Collections are nothing but a grouping of items in to a single unit. Before we starting to office, we are looking at our collection that is “Bag”. Bag is collection of items like laptop (if company provided), Lunch box, Pen, ID card, Zandu balm, Vicks inhaler.
            Now the question rises in our mind is Generic and Non Generic collections
The collection of different items in to single unit is called Non Generic collections. So we conclude that our Bag is a good example of Non Generic collection, Since it contain different items as we mentioned above.

The collection of similar items in to a single unit is called Generic
Collections. So we conclude that Footwear rack is the example for Generic collections, since it contains only chapples, shoes and slippers, I hope no one can put lunch box in to Footwear rack.

            Now we think in programming view, here generic collections are nothing but the collections of same types (like int,double,long). The generic collections are specific to the type of data we are going to store in it. If we have to clear about type of data, then generic collections is the preferable choice.
        C# is loaded with different type of collections, but here I taking a single collection for explaining, that is Stack, the following program shows how our Generic Stack is going to hold the similar type of data.
namespace GenericAndNonGeneric
{
    class Program
    {
        static void Main(string[] args)
        {
            Stack<int> GenericCollection = new Stack<int>();
            GenericCollection.Push(1);
            GenericCollection.Push(2);
            GenericCollection.Push(3);
            GenericCollection.Push(4);
            GenericCollection.Push(5);
            Console.WriteLine("The Contents of the Generic Collection are:");
            foreach(int i in GenericCollection)
            {
                Console.WriteLine(i);
            }           
            Console.ReadLine();
        }
    }
}

            In above code, our collection “GenericCollection”  is storing only integer values, if you tries to store something else apart form int type, it result in compilation error.
            The generic collections are available in namespace called System.Collections.Generic,need to specify data type in < >.
Example for Generic Collections
Array, Dictionary, List
            Non generic collections are the collections of the different types, like storing different type data as a single unit, in the below program we see Stack collection as a non generic collection.

namespace GenericAndNonGeneric
{
    class Program
    {
        static void Main(string[] args)
        {
          
            Stack NonGenericCollection = new Stack();
            NonGenericCollection.Push(1);
            NonGenericCollection.Push("C# coding");
            object o = "123Vivek";
            NonGenericCollection.Push(o);
            NonGenericCollection.Push("#@@@@*&&&");
            NonGenericCollection.Push(55.67);
            Console.WriteLine("The Contents of the Non Generic Collection are:");
            foreach(object obj in NonGenericCollection)
            {
                Console.WriteLine(obj);
            }           
            Console.ReadLine();
        }
    }
 
}

            In the above code we are putting different type of data(int,string,object,double) in to a “NonGenericCollection
            The Non Generic collections are available in namespace called System.Collections
Example for Non Generic Collections
ArrayList, Hash Table.

Now we have to look at line of code “foreach(object obj in NonGenericCollection) in above example, in this line we are converting all our entered data into a Object type, since we need a common holding point to hold different type of data, so we using object type.
Performance Issue:
Generic collections are faster than Non Generic collections, since type casting (Boxing) is happens in case of non generic collections.

Conclusion:
Based on the requirement we have to decide the usage of collections either Generic or Non Generic.

Some of the collections like Stack and Queue are used as both Generic and Non Generic collections.

Guys please put comments, it will help me to improve.

C# program Selection Sorting

Selection sort is a straightforward sorting algorithm. This algorithm search for the smallest number in the elements array and then swap i...