Saturday, 26 January 2019

Jquery

Can I use multiple versions of jQuery on the same page?

<!-- load jQuery 1.1.3 -->
<script type="text/javascript" src="http://example.com/jquery-1.1.3.js"></script>
<script type="text/javascript">
var jQuery_1_1_3 = $.noConflict(true);
</script>

<!-- load jQuery 1.3.2 -->
<script type="text/javascript" src="http://example.com/jquery-1.3.2.js"></script>
<script type="text/javascript">
var jQuery_1_3_2 = $.noConflict(true);
</script>
Then, instead of $('#selector').function();, you'd do jQuery_1_3_2('#selector').function(); or  jQuery_1_1_3('#selector').function();


DOM Traversing

<html>
   <head>
      <title>The JQuery Example</title>
      <script type = "text/javascript"
         src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js">
      </script>

      <script type = "text/javascript" language = "javascript">
         $(document).ready(function() {
            $("li").eq(2).addClass("selected");
         });
      </script>

      <style>
         .selected { color:red; }
      </style>
   </head>

   <body>
      <div>
         <ul>
            <li>list item 1</li>
            <li>list item 2</li>
            <li>list item 3</li>
            <li>list item 4</li>
            <li>list item 5</li>
            <li>list item 6</li>
         </ul>
      </div>
   </body>
</html>

Filter Selectors

SelectorExampleDescription
:first$("p:first")Selects the first <p> element.
:last$("p:last")Selects the last <p> element.
:even$("tr:even")Selects all even <tr> elements, zero-indexed.
:odd$("tr:odd")Selects all odd <tr> elements, zero-indexed.
:eq()$("tr:eq(1)")Select the 2nd <tr> element within the matched set, zero-based index.
:not()$("p:not(:empty)")Select all <p> elements that are not empty.
:lt()$("ul li:lt(3)")Select all <li> elements at an index less than three within the matched set (i.e. selects 1st, 2nd, 3rd list elements), zero-based index.
:gt()$("ul li:gt(3)")Select all <li> elements at an index greater than three within the matched set (i.e. selects 4th, 5th, ... list elements), zero-based index.
:header$(":header")Selects all elements that are headers, like <h1><h2><h3>and so on.
:lang()$(":lang(en)")Selects all elements that have a language value "en" i.e. lang="en"lang="en-us" etc.
:root$(":root")Selects the document's root element which is always <html>element in a HTML document.
:animated$(":animated")Select all elements that are animating at the time the selector is run.

Child Filter Selectors

SelectorExampleDescription
:first-child$("p:first-child")Selects all <p> elements that are the first child of their parent.
:last-child$("p:last-child")Selects all <p> elements that are the last child of their parent.
:nth-child(n)$("p:nth-child(3)")Selects all <p> elements that are the 3rd child of their parent.
:only-child$("p:only-child")Selects all <p> elements that are the only child of their parent.
:first-of-type$("p:first-of-type")Selects all <p> elements that are the first <p>element of their parent.
:last-of-type$("p:last-of-type")Selects all <p> elements that are the last <p>element of their parent.
:only-of-type$("p:only-of-type")Selects all <p> elements that have no siblings with the same element name.
:nth-last-child(n)$("p:nth-last-child(3)")Selects all <p> elements that are the 3rd-child of their parent, counting from the last element to the first.
:nth-of-type(n)$("p:nth-of-type(2)")Selects all <p> elements that are the 2nd <p>element of their parent
:nth-last-of-type(n)$("p:nth-last-of-type(2)")Selects all <p> elements that are the 2nd-child of their parent, counting from the last element to the first.

Content Filter Selectors

SelectorExampleDescription
:contains()$('p:contains("Hello")')Selects all <p> elements that contains the text "Hello".
:empty$("td:empty")Selects all <td> elements that are empty i.e that have no children including text.
:has()$("p:has(img)")Selects all <p> elements which contain at least one <img> element.
:parent$(":parent")Select all elements that have at least one child node either an element or text.

AJAX Call From JQuery

 $.ajax({
                    type: "GET", //GET or POST or PUT or DELETE verb
                    url: ajaxUrl, // Location of the service
                    data: "", //Data sent to server
                    contentType: "", // content type sent to server
                    dataType: "json", //Expected data format from server
                    processdata: true, //True or False
                    success: function (json) {//On Successful service call
                        var result = json.name;
                        $("#dvAjax").html(result);
                    },
                    error: ServiceFailed // When Service call fails
                });

Friday, 25 January 2019

LINQ C#

Find the Second Max in a list of values using linq c#

List<double> ListOfNums = new List<double> {1, 5, 7, -1, 4, 8};

var secondMax = ListOfNums.OrderByDescending(r => r).Skip(1).FirstOrDefault();

OR

var secondMax = ListOfNums.OrderByDescending(r=> r).Take(2).LastOrDefault();

Left Join in linq in C#

from lst1 in TXs
       join lst2 in TYs on lst1.ID equals lst2.ID into yG
       from y1 in yG.DefaultIfEmpty()
       select new { X = lst1, Y =y1 }

Tuesday, 22 January 2019

Value and reference types in .Net

While value types are stored generally in the stack, reference types are stored in the managed heap.


Where should use abstract and interface?

Methods  which are compulsorily need to be used will be declared in abstract ,
Methods which is used depend on the scenario will be declared in interface using segregation(SOLID Principal).

Serialization in c#

In C#, serialization is the process of converting object into byte stream so that it can be saved to memory, file or database. The reverse process of serialization is called deserialization.


Example:
  1. using System;  
  2. using System.IO;  
  3. using System.Runtime.Serialization.Formatters.Binary;  
  4. [Serializable]  
  5. class Student  
  6. {  
  7.     int rollno;  
  8.     string name;  
  9.     public Student(int rollno, string name)  
  10.     {  
  11.         this.rollno = rollno;  
  12.         this.name = name;  
  13.     }  
  14. }  
  15. public class SerializeExample  
  16. {  
  17.     public static void Main(string[] args)  
  18.     {  
  19.         FileStream stream = new FileStream("e:\\sss.txt", FileMode.OpenOrCreate);  
  20.         BinaryFormatter formatter=new BinaryFormatter();  
  21.           
  22.         Student s = new Student(101, "sonoo");  
  23.         formatter.Serialize(stream, s);  
  24.   
  25.         stream.Close();  
  26.     }  
  27. }  

Friday, 28 December 2018

Code First migrations and deployment with the Entity Framework

The Code First Migrations feature solves this problem by enabling Code First to update the database schema instead of dropping and re-creating the database. In this tutorial, you'll deploy the application, and to prepare for that you'll enable Migrations.
  1. Disable the initializer that you set up earlier by commenting out or deleting the contextselement that you added to the application Web.config file.
    XML
    <entityFramework>
      <!--<contexts>
        <context type="ContosoUniversity.DAL.SchoolContext, ContosoUniversity">
          <databaseInitializer type="ContosoUniversity.DAL.SchoolInitializer, ContosoUniversity" />
        </context>
      </contexts>-->
      <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
        <parameters>
          <parameter value="v11.0" />
        </parameters>
      </defaultConnectionFactory>
      <providers>
        <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
      </providers>
    </entityFramework>
    
  2. Also in the application Web.config file, change the name of the database in the connection string to ContosoUniversity2.
    XML
    <connectionStrings>
      <add name="SchoolContext" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=ContosoUniversity2;Integrated Security=SSPI;" providerName="System.Data.SqlClient" />
    </connectionStrings>
    
    This change sets up the project so that the first migration creates a new database. This isn't required but you'll see later why it's a good idea.
  3. From the Tools menu, select NuGet Package Manager > Package Manager Console.
  4. At the PM> prompt enter the following commands:
    text
    enable-migrations
    add-migration InitialCreate
    
    enable-migrations command
    The enable-migrations command creates a Migrations folder in the ContosoUniversity project, and it puts in that folder a Configuration.cs file that you can edit to configure Migrations.
    (If you missed the step above that directs you to change the database name, Migrations will find the existing database and automatically do the add-migration command. That's okay, it just means you won't run a test of the migrations code before you deploy the database. Later when you run the update-database command nothing will happen because the database already exists.)
    Migrations folder

Sunday, 29 July 2018

Difference between HAVING and WHERE Clause

A HAVING clause is like a WHERE clause, but applies only to groups as a whole, whereas the WHERE clause applies to individual rows. A query can contain both a WHERE clause and a HAVING clause. The WHERE clause is applied first to the individual rows in the tables . Only the rows that meet the conditions in the WHERE clause are grouped. The HAVING clause is then applied to the rows in the result set. Only the groups that meet the HAVING conditions appear in the query output. You can apply a HAVING clause only to columns that also appear in the GROUP BY clause or in an aggregate function. (Reference :BOL)
Example of HAVING and WHERE in one query:
SELECT titles.pub_idAVG(titles.price)FROM titles INNER JOIN publishersON titles.pub_id publishers.pub_idWHERE publishers.state 'CA'GROUP BY titles.pub_idHAVING AVG(titles.price) > 10
Sometimes you can specify the same set of rows using either a WHERE clause or a HAVING clause. In such cases, one method is not more or less efficient than the other. The optimizer always automatically analyzes each statement you enter and selects an efficient means of executing it. It is best to use the syntax that most clearly describes the desired result. In general, that means eliminating undesired rows in earlier clauses.
Answer in one line is : HAVING specifies a search condition for a group or an aggregate function used in SELECT statement.