Infolink

 

Search This Blog

Showing posts with label Chirag Kanzariya. Show all posts
Showing posts with label Chirag Kanzariya. Show all posts

Dec 2, 2012

How to migrate your ASP.NET site to the Azure cloud

Cloud computing is one of the hot topics of 2011 with those willing to make the jump to a cloud-based solution finding financial savings in this new approach as well as, in many cases, better fault tolerance and a more responsive service turnaround in many cases. However, what many developers may not realise is just how straightforward it can be to migrate a web site from a local or hosted server into the cloud.

To demonstrate, this article will show you how to upgrade and deploy an existing ASP.NET 3.5 or 4.0 web application to the Windows Azure cloud platform.

We will cover:
  1. The SDKs and tools to be installed on your machine before you can continue
  2. How to set up your Windows Azure instance in preparation for the application
  3. How to alter the application so that it will function in the cloud
  4. How to deploy the application to Windows Azure

Nov 30, 2012

ArrayList in Asp.net

The ArrayList object is a collection of items containing a single data value.

Items are added to the ArrayList with the Add() method.

The following code creates a new ArrayList object named books and three items are added:
  ArrayList books = new ArrayList();
  books.Add(”book1");
  books.Add(”book2");
  books.Add(”book3");

Nov 29, 2012

How to create MVC (model view controller) views faster by using HTML helper classes

MVC using HTML helper classes 

<table width="50%" runat="server" id="tbl1">
    <tr>
    <td>Student Id:</td>
    <td><asp:TextBox runat="server" ID="txt1"></asp:TextBox></td></tr>
    <tr>
    <td>Student Name:</td>
    <td><asp:TextBox runat="server" ID="txt2"></asp:TextBox></td></tr>
    <tr>
    <td>Student Percentage:</td>
    <td><asp:TextBox runat="server" ID="txt3"></asp:TextBox></td></tr>
    <tr>
    <td colspan="2" align="center"><asp:Button ID="Button1" runat="server" Text="Button" /></td>
    </tr>
</table>


How to pass data from controllers to views in ASP.NET MVC ?

The controller gets the first hit and loads the model. Most of the time we would like to pass the model to the view for display purpose.

As an ASP.NET developer your choice would be to use session variables, view state or some other ASP.NET session management object.

How to create a simple ASP.NET MVC data entry screen?

Creating simple MVC data entry screen

Every project small or big has data entry screens. In this lab we will create a simple customer data entry screen as shown in the below figure.



As soon as the end user enters detail and submits data it redirects to a screen as shown below. If he enters amount less than 100 it displays normal customer or else it displays privileged customer.

Basic MVC

The Model

Models are the main component of your app, which is usually; what is stored and retrieve from the database, in our app, it will the tweet info and text.

The View

Views are the date in formatted form, delivered to the user. Here it will be the HTML pages.

The Controller

Controllers are the components that respond to all user requests, decide the appropriate response; which might be render a view, redirect to other URI or many other things.

Pros and Cons
  • Pros
It has many pros, but these are the most remarkable:
  1. Full control over the HTML, CSS, and JavaScript code, helping you to write clean, standards-compliant markup
  2. It's Extensible which means that you can use a default component, or reconfigure it, or even use another component
  3. HTTP friendly, it gives you total control over the requests passing between browser and server
  4. Testability of MVC makes it simple to unit test the logic that is specific to a page, by testing controller methods
  • Cons
It does not really have many cons compared to its pros:
  1. The lake of drag and drop component in WebForms, despite that JQuery and other JavaScipt library overcome this problem
  2. It's not as easy as WebForms, you should understand the role of each component individually and all of them together.

Nov 28, 2012

How to create a simple model in ASP.NET MVC and display the same?(PART-4)

Step1:- Create a simple class file

The first step is to create a simple customer model which is nothing but a class with 3 properties code, name and amount. Create a simple ASP.NET MVC project, right click on the model folder and click on add new item as shown in the below figure.


Nov 26, 2012

ASP.NET MVC (PART-3)

MVC stands for "Model-View-Controller", which is a very old (but still very useful) design pattern. This design pattern will split your application in 3 different parts ("model", "view", "controller"). "Seperation of concern" is one of it´s main benefits.

Nov 25, 2012

MVC 2 - Dropdownlist with JSON

I setup the Database and LinqToSql classes for my app. The table structure is MeterTypes > MeterCollections > MeterUsages. Where a MeterType is the top level (ie. Electricity, Gas, Waste, etc.) the MeterCollections are the next level down (a MeterType may contain many of these, for Electricity I have Peak and Off-Peak) and MeterUsage is the actual usage on a meter for that MeterCollection.

OOPS Concepts (PART-2)

Multiple inheritance

Multiple inheritance is the possibility that a child class can have multiple parents. Human beings have always two parents, so a child will have characteristics from both parents.

In OOP, multiple inheritance might become difficult to handle because it allows ambiguity for the compiler. There are programming languages such as C++ that allow multiple inheritance; however, other

programming languages such as Java and the .NET Framework languages do not allow multiple inheritance. Multiple inheritance can be emulated in .NET using Multiple Interface Inheritance.

Nov 16, 2012

Interface class

Sometimes it is simple and helpful to define a class that does nothing but defines a standardised interface that can be used by other classes to make sure the implementation follows a standard set of input and output paramaters. Also this approach results in faster code runs and more maintainable code.

The interface class can only be approached by using a pointer to the virtual table that exposes the methods in the interface. Interface does not come by itself, it usually comes with an inherited class that implements the exposed method in the interface. Such a class that implements the interface exposed methods is often called a co-class.

OOPS Concept with Real-world example

Introduction

OOP is Nothing but Object Oriented Programming.According to Wikipedia,  Object-oriented programming (OOP) is a programming paradigm that uses "objects" and their interactions to design applications and computer programs.

Asp.Net Tutorial (PART-15)

Displaying Information

The ASP.NET Framework includes two controls you can use to display text in a page: the Label control and the Literal control. Whereas the Literal control simply displays text, the Label control supports several additional formatting properties.

Nov 15, 2012

Web Server Controls vs. HTML Server Controls

One of the first possible points of confusion when learning to build web forms is "what is the difference between a Web Server Control and an HTML Server Control?" This probably stems largely from the fact that most of the Web and HTML Server Controls don’t differ much – for example, the Label Web Server Control operates almost identically to the Label HTML Server Control. However, there are some subtle differences between the two.

Nov 14, 2012

Adding Code to the Event Handler (PART-14)

Adding code to the event handler is, well, exactly as you would expect it to be. Place the cursor in the space between Protected Sub Button1_Click(…) and End Sub and type the appropriate code in. For

now, all the code will do is change the text of the label (Label1) to, you guessed it, "Hello world!". Type in the following code, and pause after you type the period:

Oct 18, 2012

Generics classes Overview in C#.Net

Generics main benefit is eliminating boxing and unboxing that is true, there are also other benefits like type safety, by using generics you eliminate type ambiguitiy.

  1. Generics provide type safety without the overhead of multiple implementations. Ex. We can create a linked list of string. LinkedListlinkList=new LinkedList(); There is no need to inherit from a base type and override members.The linked list is ready for immediate use.
  2. There is no need to write code to test for the correct data type because it is enforced at compile time. The need for type casting and the possibility of run-time errors are reduced.
  3. Generic collection types generally perform better for storing and manipulating value types because there is no need to box the value types
  4. Generic delegates enable type-safe callbacks without the need to create multiple delegate classes.  

Oct 17, 2012

C# KeyNotFoundException

A KeyNotFoundException was thrown. This is likely caused by an invalid usage of the Dictionary collection. As always we want a quick way to fix the problem.

Example

First, here we see some code that looks correct, but actually has a severe flaw. The problem is that you cannot look up a key that doesn't exist in the Dictionary and try to assign your variable to its value.

The KeyNotFoundException here is thrown on the final line of try-block. The string "test" doesn't exist in the collection.

Program that throws KeyNotFoundException [C#]

using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
    try
    {
        //
        // Create new Dictionary with string key of "one"
        //
        Dictionary<string, string> test = new Dictionary<string, string>();
        test.Add("one", "value");

        //
        // Try to access key of "two"
        //
        string value = test["two"];
    }
    catch (Exception ex)
    {
        //
        // An exception will be thrown.
        //
        Console.WriteLine(ex);
    }
    }
}

Output

System.Collections.Generic.KeyNotFoundException:
    The given key was not present in the dictionary.
at System.ThrowHelper.ThrowKeyNotFoundException()

Fix

Here we look at how you can fix this exception by using the TryGetValue method on the Dictionary constructed type. Note that could use ContainsKey instead of TryGetValue, but the below code preserves the intention of the previous code.

Program that does not throw [C#]

using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
    Dictionary<string, string> test = new Dictionary<string, string>();
    test.Add("one", "value");
    //
    // Use TryGetValue to avoid KeyNotFoundException.
    //
    string value;
    if (test.TryGetValue("two", out value))
    {
        Console.WriteLine("Found");
    }
    else
    {
        Console.WriteLine("Not found");
    }
    }
}
Output

Not found

You always have to use the if statement when testing values in the Dictionary, because there is always a possibility that the key won't exist.

We saw how to raise and catch the KeyNotFoundException during runtime. We then saw how to avoid causing the exception. We discussed alternatives, such as TryGetValue and ContainsKey, and looked at a program that does not have this problem.

Oct 16, 2012

ASHX handler in Asp.Net

An ASHX handler allows you a discrete http handler without the overhead of processing a page request (an ASPX file).

<%@ WebHandler Language="C#" Class="Handler" %>

using System;
using System.Web;

public class Handler : IHttpHandler {
   
    public void ProcessRequest (HttpContext context) {
        var fileName = "must.sql";
        var r = context.Response;
        r.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
        r.ContentType = "text/plain";
        r.WriteFile(context.Server.MapPath(fileName));

    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}
 Now, You have to create button in your .aspx page write the code as following

<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />

protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("Handler.ashx");
    }
 

Stop default Auto-Complete of ASP.net TextBox on all browser

If you use textBox properties AutoCompleteType="Disabled" it disables the autocompletes of textBox on only Internet explorer. There's another way to stop auto-complete on all browsers. Use autocomplete="off" on from tag instead of AutoCompleteType = "Disabled". Again you can use the TextBox1.Attributes.Add("autocomplete", "off"); to stop auto-complete on only specific TextBox control.

So, my findings are use the following code to stop autocomplete on specific control

Activity logging and Error logging in ASP.NET

There are lots of error logging providers available such as log4Net, AnLogger, etc. they are easy to use as well, but just to keep it simple, I have written my own. I call it "Activity and Error Logger", it stores the data in XML format, request by request and session by session, you can actually create the user behaviour maps if you properly call the activity log at while performing any major operations. It also takes care of the concurrency, two threads will not overwrite each other. It logs the errors on page in try catch block and also at global level, in Application_Error event in global.asax.
Related Posts Plugin for WordPress, Blogger...