Thursday, 27 June 2013

jQuery.each() function


jQuery.each( collection, callback(indexInArray, valueOfElement) ) Returns: Object


Description: A generic iterator function, which can be used to seamlessly iterate over both objects and arrays. Arrays and array-like objects with a length property (such as a function's arguments object) are iterated by numeric index, from 0 to length-1. Other objects are iterated via their named properties.

jQuery.each( collection, callback(indexInArray, valueOfElement) )
collectionThe object or array to iterate over.
callback(indexInArray, valueOfElement)The function that will be executed on every object.

The $.each() function is not the same as $(selector).each(), which is used to iterate, exclusively, over a jQuery object. The $.each() function can be used to iterate over any collection, whether it is a map (JavaScript object) or an array. In the case of an array, the callback is passed an array index and a corresponding array value each time. (The value can also be accessed through the this keyword, but Javascript will always wrap the this value as an Object even if it is a simple string or number value.) The method returns its first argument, the object that was iterated.


$.each([52, 97], function(index, value) { 
  alert(index + ': ' + value); 
});

This produces two messages:
0: 52
1: 97

If a map is used as the collection, the callback is passed a key-value pair each time:

var map = { 
  'flammable': 'inflammable', 
  'duh': 'no duh' 
}; 
$.each(map, function(key, value) { 
  alert(key + ': ' + value); 
});

Once again, this produces two messages:
flammable: inflammable
duh: no duh

We can break the $.each() loop at a particular iteration by making the callback function return false. Returning non-false is the same as a continue statement in a for loop; it will skip immediately to the next iteration.

Examples:

Example: Iterates through the array displaying each number as both a word and numeral

<!DOCTYPE html>
<html>
<head>
  <style>
  div { color:blue; }
  div#five { color:red; }
  </style>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
  
  <div id="one"></div>
  <div id="two"></div>
  <div id="three"></div>
  <div id="four"></div>
  <div id="five"></div>
<script>
    var arr = [ "one", "two", "three", "four", "five" ];
    var obj = { one:1, two:2, three:3, four:4, five:5 };

    jQuery.each(arr, function() {
      $("#" + this).text("Mine is " + this + ".");
       return (this != "three"); // will stop running after "three"
   });

    jQuery.each(obj, function(i, val) {
      $("#" + i).append(document.createTextNode(" - " + val));
    });
</script>
</body>
</html>
 
DEMO:
Mine is one. - 1
Mine is two. - 2
Mine is three. - 3
-4
-5



Example: Iterates over the properties in an object, accessing both the current item and its key.
$.each( { name: "John", lang: "JS" }, function(k, v){
   alert( "Key: " + k + ", Value: " + v );
 });

Example: Iterates over items in an array, accessing both the current item and its index.

$.each( ['a','b','c'], function(i, l){
   alert( "Index #" + i + ": " + l );
 });
Continue Reading →

Thursday, 20 June 2013

Execute Stored procedure using ADO.NET (C#)

Namespace used for execute the stored procedure,


using System;
using System.Data;
using System.Data.SqlClient;

public void SelectEmployee()
{
   SqlConnection con = null;
   SqlDataReader rd  = null;
   try
   {
         // Create and Open the SQL server connection object
       con = new SqlConnection("Database Connection string");
       con.Open();
     //  Create a command object and specify the Stored Procedure name and connection as well
       SqlCommand cmd  = new SqlCommand("ProcEmployeeDetails", con);
         //  Set the command object
       cmd.CommandType = CommandType.StoredProcedure;
         // Add parameter and value
       cmd.Parameters.Add(new SqlParameter("@EmployeeID", 550));
         // Execute the command
       rd = cmd.ExecuteReader();
       while (rd.Read())
       {
    Response.Write(rd["Name"].ToString(),rd["Age"].ToString(), rd["Designation"].ToString());
       }
    }
    catch (Exception e)
    {
          Response.Write(e);
    }
    finally
    {
       if (con != null)
       {
            con.Close();
       }
       if (rd != null)
       {
            rd.Close();
       }
     }       
}


Continue Reading →

"using" keyword in C#"


Two ways to use "using" keyword in C#"


In C#,  using Keyword  can be used in two different ways and are hence referred to differently.


 As a Directive In  this usage, the "using" keyword can be used to include a namespace  in your programme.(Mostly used)
As a StatementUsing can also be used in a block of code to dispose a IDisposable objects.(less used)
Example
A simple and straightforward  approach to connect to a database server and read data would be-
SqlConnection sqlconnection = new SqlConnection(connectionString);
  SqlDataReader reader = null;
  SqlCommand cmd = new SqlCommand(commandString.  
sqlconnection);
  
sqlconnection 
.Open();
  reader = cmd.ExecuteReader();
       while (reader.Read())
         {
           //Do
         }
  reader.Close();
  
sqlconnection 
.Close(); 
However, the above given code may generate error.
If any exception occurs inside while block it throws exception the connection.close() process will not executed due to the exception.To avoid this situation, we can take the help of the try....catch... finally block   by closing the connection inside the finally block or inside the catch block.
"Using" keyword takes the parameter of type IDisposable.Whenever you are using any IDisposable type object you should use the "using" keyword  to handle automatically when it should close or dispose. Internally using keyword calls the Dispose() method to dispose the IDisposable object.
So the correct code would be :
using(SqlConnection sqlconnection= new SqlConnection(connectionString) )
            {
              SqlCommand cmd = new SqlCommand(commandString, sqlconnection);
          sqlconnection.Open();
             using (SqlDataReader reader = cmd.ExecuteReader())
             {
                while (reader.Read())
                {
                  //DO
                }
             }
           }
 In this code  if any exception occurs then dispose() will be called and connection will closed automatically.


Continue Reading →

Wednesday, 19 June 2013

Whats New in CSS3


CSS3 Features:

There are several new functionalities have been provided through CSS3. Functions such like opacity, Text-overflow, media queries and box shadows are some of the much attractive introductions.

1- With CSS3 you’ll be able to add not one, not four, but eight background images to a single element.

2- CSS3 is also addingborder-radius that may eliminate the need for images to create rounded corners in most cases.
Example:
<div style=" background-color: #ccc; -moz-border-radius: 5px; -webkit-border-radius: 5px; border: 1px solid #000; padding: 10px;" > 

3-  in CSS3 you Can Set Border-image Property. Border-image, allows an image to be used as the border of an element.

4- you can set text-shadow in CSS3 by using
text-shadow: 2px 2px 2px #000; property.

5- in CSS3 you can set Transparency & Opacity of a element.
  example: <img src="opacity.png" style="margin: 10px; opacity:0.25; width:179px; height:76px;"/>

6- The word-wrap property added to CSS3 and it is really essential. 
   example :
<p style="word-wrap: break-word;...

7- in CSS3 you can Rotate any element by using transform property.
example: div{ transform :rotate(30deg); }



Continue Reading →

Tuesday, 18 June 2013

what is DTO


what is Data Transfer Object?

A Data Transfer Object is an object that is used to encapsulate data, and send it from one subsystem of an application to another.
DTOs are most commonly used by the Services layer in an N-Tier application to transfer data between itself and the UI layer. The main benefit here is that it reduces the amount of data that needs to be sent across the wire in distributed applications. They also make great models in the MVC pattern.
Another use for DTOs can be to encapsulate parameters for method calls. This can be useful if a method takes more than 4 or 5 parameters.
When using the DTO pattern, you would also make use of DTO assemblers. The assemblers are used to create DTOs from Domain Objects, and vice versa.
The conversion from Domain Object to DTO and back again can be a costly process. If you're not creating a distributed application, you probably won't see any great benefits from the pattern, as Martin Fowler explains here
Continue Reading →

Thursday, 13 June 2013

ASP.NET - Web Services


A web service is a web-based functionality accessed using the protocols of the web to be used by the web applications. There are three aspects of web service development:
  • Creating the web service
  • Creating a proxy
  • Consuming the web service

Creating the Web Sevice:

A web service is an web application which is basically a class consisting of methods that could be used by other applications. It also follows a code-behind architecture like the ASP.Net web pages, although it does not have an user interface.

I am just Creating a Simple WebService.


Take the following steps to create the web service:
Step (1): Select File--> New --> Web Site in Visual Studio, and then select ASP.Net Web Service.
Step (2): A web service file called Service.asmx and its code behind file, Service.cs is created in the App_Code directory of the project.
Step (3): The .asmx file has simply a WebService directive on it:

<%@ WebService Language="C#" CodeBehind="~/App_Code/Service.cs" Class="Service" %>


Step (4): Open the Service.cs file, the code generated in it is the basic Hello World service.
Step (5): Add a New WebMethod in Servie Class named "GetUserDetails". this will fetch all Data from a Table named admission.
WebMethod looks like Below Code Snippet:


    [WebMethod]
    public DataSet GetUserDetails()
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["mycon"].ToString());
        con.Open();
        SqlCommand cmd = new SqlCommand("select * from admission ", con);
        cmd.ExecuteNonQuery();
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        // Create an instance of DataSet.
        DataSet ds = new DataSet();
        da.Fill(ds);
        con.Close();
        return ds;
    }

Step (6) : Running to Test your Service right Click on Service.asmx file and select View in Browser option this will open your web service test page, which allows testing the service methods.


Step (7) : Click on a method name, and check whether it runs properly .Click on GetUserDetails webMethod.


Step (8): Click Invoke Button to Display Output that Service Method returns . like this:


Consuming the Web Service:

For using the web service, create a web site under the same solution. This could be done by right clicking on the Solution name in the Solution Explorer.

Firstly you have to Add service Refrence in your Website. for this Right click on your Website, select Add Service Refrence . in the Add Service Refrence Popup Window Click on Discover Button it will Search all   asmx service in your Solution.


now select asmx service that you want to add in your Website and Click OK Button. now Service has been Added in your Website Project. this will look like this :


in Default.aspx add a GridView Control. Code lookes like Below snippet ...

<body>
    <form id="form1" runat="server">
    <div>
    <asp:GridView ID="grid1" runat="server" AutoGenerateColumns="true">
   
    </asp:GridView>
    </div>
    </form>
</body>

Now in Default.aspx.cs file Add the Following Code 


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

//this is the proxy
using ServiceReference1;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        var proxy = new ServiceReference1.ServiceSoapClient();

        grid1.DataSource = proxy.GetUserDetails();
        grid1.DataBind();
    }
}


Now Build Your Project and Right click on default.aspx, select View in Browser option. Grid Display all Data From Database Table.

this was simple Web Service Demo.
You Can Download the complete SourceCode from here Download this Project

 feel free to Ask any Question regarding this Post.

Thanks and Regards:
Suraj Kumar Maddheshiya





Continue Reading →

Topics

ADO .Net (2) Ajax (1) Angular Js (17) Angular2 (24) ASP .Net (14) Azure (1) Breeze.js (1) C# (49) CloudComputing (1) CMS (1) CSS (2) Design_Pattern (3) DI (3) Dotnet (21) Entity Framework (3) ExpressJS (4) Html (3) IIS (1) Javascript (6) Jquery (9) Lamda (3) Linq (11) Mongodb (1) MVC (48) NodeJS (7) RDLC (1) Report (1) Sql Server (29) SSIS (3) SSRS (2) UI (1) WCF (12) Web Api (10) Web Service (1) XMl (1)