Friday 2 August 2013

ASP.NET MVC - Basics

MVC  is the abbreviation of Model View Controller, MVC is the Framework for building web applications

Model - Business Layer (Database)
View   - UI Layer (front end)
Controller - Action takes on Controller event


Let we see how the MVC works

Steps to create MVC application 

  • Open Visual studio 2010
  • Select ASP.NET MVC 2 Application.
  • Give the Project name and select OK.



In Solution Explorer You can see the Various Folders , Now we can see few of them which is important for this example. Controll

Controllers Folder : Hold the controller that means action to perform
Models Folder : Hold the Model or Database information with data
Views Folder : Holds the View of each and every Controller.

Now press F5 and see , The URL deosn't end with any extension but it shows the Home page Related to
HomeController -> action of Index, Because under the home view folder the action of Index page is set as Default page to launch at startup.


http://localhost:1429/  

Actual URL of above url is 

http://localhost:1429/Home

OR

http://localhost:1429/Home/Index

To Call a About Page or About Action inside a HomeController Just Place the Prefix name of the Controller Like "Home" For "HomeController" Along with concat of forward slash then add the name of the Action to call the page Like "Home/About".

http://localhost:1429/Home/About.

Now Let we see Programming , Add the Following code in Controller class "HomeController.cs"

Let we see Create a New View in Existing Controller
Step 1 :
Assign the value of name in ViewData 

public ActionResult Test()
{
   ViewData["name"] = "Rajesh";
   return View();

}


Step 2 : Add the Test.aspx view page in Home Folder of Views .

  •   Right Click the Home Folder Under the Views
  •   Select the Add Option
  •   Select the View,Then click ok.

Step 3 :
Add the following code in test.aspx, This will get the value of name from the viewData which is assigned in the HomeController class of Test Action.

   <h2><%: ViewData["name"] %></h2>


 Step 4 :
     Press the F5, Go to The URL "http://localhost:1429/Home/test".


Let we see Create a New View in New Controller

Instead of Step 1 We can add a new controller class  in controller and place the code in new controller. Instead of placing in existing controller.

Place the following steps instead of step 1 for add a new controller.


 Step 1 :
  •   Right Click the Controller folder 
  •   Select the Add Option
  •   Select the Controller to add the New controller, give any name post fix with controller "ValidateController" and click ok.

Now place the below code in ValidateController

public ActionResult Test()
{
   ViewData["name"] = "Rajesh";
   return View();

}



 Step 2 :


  • Add the Folder nameValidate under the View Folder.
  • Add the view name Test.aspx and Index.aspx under the Validate Folder.
  • Paste the following code in that Text.aspx
   <h2><%: ViewData["name"%></h2>

Press F5 , Go to URL http://localhost:1429/Validate/Test . You will get the Result.




Output:



From this article I hope that you can learn how to add the controller and view and how to transfer the data between them . 

Sunday 28 July 2013

Add Row Dynamically in Table Using JQuery

Below code is used to add the row dynamically to table using JQuery.

How to find the row in tbody inside the Table using Jquery : 
$(‘#Search_table’).find("tbody").find("tr");

<table id="Search_table">
<thead>
<tr><td>USER ID</td>
<td>USER NAME</td>
<td>CURRENT POINT</td>
<td>ADD POINT</td>
<td>ADD DESCRIPTION</td>
<td>ACTION</td>
</tr>
 </thead>
<tbody> </tbody>
</table>

<script>
$(document).ready(function ()
{      
var table_tr="<tr><td>RajeshG</td><td>Rajesh
</td><td>100</td><td>100</td><td>
point added </td><td>ACTION </td></tr>";

$('#Search_table').find("tbody").append(table_tr);
//to append the tr in the tbody tag                  
});
</script>


Add a FACEBOOK like button in your website using JQuery

  Sometime we had wonder how the like button is integrated with corresponding Social Networking, if we gave like then the corresponding website like information is posted in the social network.

Now let we see how to add the like button in our website?

Get the Jquery SDK from the Facebook then add the element in DIV tag.

<html xmlns="http://www.w3.org/1999/xhtml">
<title>jQuery Implement on facebook Like button </title>
 <div id="fb-root"></div>
<script>(function (d, s, id)
{
    var js, fjs = d.getElementsByTagName(s)[0];
    if (d.getElementById(id))
      return;
    js = d.createElement(s);
    js.id = id;
    js.src = "//connect.facebook.net/en_US/all.js#xfbml=1&appId=497727420302756";
    fjs.parentNode.insertBefore(js, fjs);
}
(document, 'script', 'facebook-jssdk'));

window.onload = function ()
{
  var url = window.location.protocol + "//" + window.location.hostname +
 window.location.pathname;
  document.getElementsByClassName ("fb-like")[0].attributes[4].nodeValue =url;
};
</script>
   
<body>
    <form id="form1" runat="server">
 <div class="fb-like" data-href="" data-send="true" data-width="250"
data-show-faces="true"></div>
    </form>
</body>
</html>





From this article you can see the how to add the Facebook like button in your own website.

Output : Facebook Like Button


jQuery Implement Twitter follw us plugin like facebook

Create a Custom Exception - Information about Exception class



What is an Exception?
In C# Language, Exception handling is done by Try, Catch, Finally to handle the Failure and clean up the resources afterwards. Once a Exception is raised with in a Try statement Then the flow of program control immediately jumps to the associated Exception Handler.

If there is no corresponding exception handler for the Thrown Exception then program stops execution.

How to create a Custom Exception?
          Derive the Class from the Exception class and implement the Constructor and pass the parameter to the base class.

Note:
1.               When the message is doesn’t pass to the base class,Then when your custom Exception is caught in General Exception. When you try to print the message it will print the Exception in following format. “Exception of type ‘CustomException’ was thrown” So base(message) is to be used then only the General exception can knew the message .(or) Catch with the same Exception Type 
public class CustomException:Exception
{
    public CustomException():base()
    { 
    }
    public CustomException(string _message):base(_message)
    {       
    }
    public CustomException(string _message,Exception _innerException):base(_message,_innerException)
    { 
    } 
    public CustomException(SerializationInfo _serinfo,StreamingContext _strcontext):base(_serinfo,_strcontext)
    { 
    }

}

There is an Interesting thing in catch the custom exception, Throw a custom exception with empty message. When you catch that exception with the same type, and print the exception message in console screen , You will notice that it will print a message

public static void Main(string[] args)
    {
        try
        {
            throw new CustomException();
        }
        catch (CustomException ex)
        {
            Console.WriteLine(ex.Message);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }

Exception of type 'CustomException' was thrown.




Why this error message is print, Even though it is catch by same exception type, The Reason is the Message is empty in that Exception when Thrown because of that only this is thrown as That “Exception of type ‘CustomException’ was thrown”.

Some times, some of them thought that Exception is not catch by corresponding Type,so this problem raises. So what they do is they check for the code where they left the catch block for corresponding type.

Now add the Error Message in Custom Exception when it is Thrown

    public static void Main(string[] args)
    {
        try
        {
            throw new CustomException("Custom Error");
        }
        catch (CustomException ex)
        {
            Console.WriteLine(ex.Message);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }

Now the Output is  
Custom Error


Now you can see the Error in Catch instead of type in catch block
From this article I hope that you can learn some of the basic things and some interested things about Exception.