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

"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 sqlconnectionnew SqlConnection(connectionString))
            {
          SqlCommand cmd = new SqlCommand(commandStringsqlconnection);
          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); }

8- The float property specifies whether an element should float to the left, right, or not at all.

9- The clear property specifies what should happen with the element that is next to a floating element.


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

ADFS (1) ADO .Net (1) Ajax (1) Angular (43) Angular Js (15) ASP .Net (14) Authentication (4) Azure (3) Breeze.js (1) C# (47) CD (1) CI (2) CloudComputing (2) Coding (7) CQRS (1) CSS (2) Design_Pattern (6) DevOps (4) DI (3) Dotnet (8) DotnetCore (16) Entity Framework (2) ExpressJS (4) Html (4) IIS (1) Javascript (17) Jquery (8) Lamda (3) Linq (11) microservice (3) Mongodb (1) MVC (46) NodeJS (8) React (11) SDLC (1) Sql Server (32) SSIS (3) SSO (1) TypeScript (1) UI (1) UnitTest (1) WCF (14) Web Api (15) Web Service (1) XMl (1)

Dotnet Guru Archives