second argument to the $() function

What does second argument of $() function in jquery.

The second argument is context to limit the search

Suppose we have html like below

<ul id="firstList">
	<li>one</li>
	<li>two</li>
	<li>three</li>
</ul>
<ul id="secondList">
	<li>blue</li>
	<li>green</li>
</ul>

if we want to go through the li only under the firstList div we can use $() function’s second argument as below.


$('li','#firstList').each(function(){
console.log($(this).html());
});

 

Define an exists() function in Jquery

When we are in web development we have to check element is exist using Javascript. Below way we can check it.


console.log($('#elem').length == 1 ? "exists!" : "doesn't exist!");

But it can be cumbersome

So we can define a Jquery function as below


jQuery.fn.exists = function(){ return this.length > 0; }

console.log($('#elem').exists() ? "exists!" : "doesn't exist!");

Is it nice?…

 

 

PIVOT in SQL SERVER

One of the more complex statements that we can execute inside sql server is a PIVOT statement.

What it does? 

PIVOT statement allows us to convert what would normally be row data into column data.

Sounds Crazy…

In simply let’s imagine we have a table as follow.

fulltable

In above table years are in rows. Let’s suppose we want to get data by years. Now it’s time to use PIVOT. Using PIVOT we can convert years into columns.

PIVOT syntax

SELECT <NonPivot>
, <FirstPivotedColumn>
, …
FROM <Table containing data>
PIVOT (FUNCTION(<data column>)
FOR <List of pivoted columns>)
AS <alias>

Let’s do

SELECT * FROM @Table
PIVOT (SUM(Sales)
FOR [Year] IN([2010],[2012])
)AS pvt

The we get result

pivotedtable

Paging in sql server

It is best idea to do paging in server side. From Sql Server 2012 we have useful paging mechanism

We can use FETCH and OFFSET to paging in sql server.

FETCH -> indicates number of rows to retrieve

OFFSET-> indicates the number of rows to skip

Syntax for Paging

SELECT <columns>
FROM <tables>
ORDER BY<columns>
OFFSET x
FETCH NEXT y ROWS ONLY

x means number of record to skip and y means number of record to retrieve

Example

SELECT * 
FROM product 
ORDER BY productId 
OFFSET 20 ROWS 
FETCH NEXT 20 ROWS ONLY

In this it skips first 20 records and fetch next 20 rows

But in this some restrictions are there

  • ORDER BY is required
  • TOP is not allowed

CTE in sql server

CTE stands for Common Table Expression. It is introduced in sql server 2005.

Actualy it is not a terrible thing as we heard. We can consider it as inline view or temporary table.  CTE allows us to do exact same things like what we doing using views.

Benefits:

  • Breakdown complex queries
  • Avoid sub queires
  • Simply certain syntax

Syntax for CTE


WITH <cte name>[(columns)]

AS(

<SELECT statement>

)

Example

WITH SalesData(TotalSold,Year,Branch)
AS(
SELECT SUM(Total) AS 'TotalSold',
       YEAR(OrderDate) AS '[Year]',
       b.BranchName
 FROM Order o INNER JOIN 
 Branch b 
 ON o.branchId = b.branchId
 GROUP BY YEAR(OrderDate),b.BranchName
)

Then we can do any operations to SalesData CTE as we doing to a View.

Like : SELECT * FROM SalesData

Note: CTE column specifying is optional . if not specified it takes select query result columns

We can use it for short hand for sub-query. More over there are many other benefits in CTE. I noted a here very basic usage.

Delete duplicate rows in a table in Sql server

When I am developing I came across to delete duplicate rows in a table. This is solution I have applied. I mentioned it in here coz it may help you.

DECLARE @Table AS TABLE([Id] INT IDENTITY(1 ,1) ,NAME NVARCHAR(50))</code>

INSERT INTO @Table( NAME )

SELECT 'jeevan'

UNION ALL

SELECT 'jeevan'

UNION ALL

SELECT 'Nimal'

UNION ALL

SELECT 'Kuma'

UNION ALL

SELECT 'Kuma'



;WITH cte AS (

SELECT NAME

,ROW_NUMBER() OVER(PARTITION BY C.NAME ORDER BY [Id]) AS [RowId]

FROM   @Table AS C

)


DELETE FROM

FROM   cte

WHERE  [RowId]>1


The remote server returned an error: (417) Expectation failed is solved

what is Expect100Continue Property

       According to w3.org 

The purpose of the 100 (Continue) status  is to allow a client that is sending a request message with a request body to determine if the origin server is willing to accept the request (based on the request headers) before the client sends the request body. In some cases, it might either be inappropriate or highly inefficient for the client to send the body if the server will reject the message without looking at the body.

more details about 100 continue property

I got this error when try to call web method in c#. So finally I salved this error by applying following things.

  1. Adding following statement right before calling web method

 System.Net.ServicePointManager.Expect100Continue = false.

2. Adding following section in web.config inside <configuration>

<settings>
<servicePointManager expect100Continue=”false” />
</settings>

more detail on MSDN

Cannot retrieve property ‘Name’ because localization failed in multi lingual website is soloved

Cannot retrieve property ‘Name’ because localization failed. Resources.Resource’ is not public or does not contain a public static string property with the name ”

Errormsg

This raised because of access modifier of Resource file. It should be public.

But access modifier combo can be disabled.

2
How to Fix:

Change Custom tool property to “PublicResXFileCodeGenerator” .

Change Build Action property to “Embedded Resource”

Add Custom Tool Namespace

Final property window look like below

4
Clean solution and rebulid.

Done!!!!.

ASP.NET MVC Multilingual web site

In this article I am going to explain how to handle multiple language in your ASP.NET MVC web site.

This a very basic step.

Step 1

Create ASP.NET MVC web application

Step 2

Add new controller called BaseController. (any name as you wish)

override BeginExecuteCore() method.


 protected override IAsyncResult BeginExecuteCore(AsyncCallback callback, object state)
 {
 string language = "en";
 HttpCookie cultureCookie = Request.Cookies["_culture"];
 if (cultureCookie != null)
 language = cultureCookie.Value;

 Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(language);

 return base.BeginExecuteCore(callback, state);
 }

Step 3

Inherit Home Controller from BaseController


public class HomeController : BaseController

step 4

Add action to change language.

To store the culture I have used cookies. It is a simple way.


 public ActionResult ChangeLanguage(string lang) {
 HttpCookie cookie = Request.Cookies["_culture"];
 if (cookie != null)
 cookie.Value = lang; 
 else
 {

 cookie = new HttpCookie("_culture");
 cookie.Value = lang;
 cookie.Expires = DateTime.Now.AddYears(1);
 }
 Response.Cookies.Add(cookie);

 return RedirectToAction("Index");
 }

step 5 

Add resource files.

Different languages are stored in resource files.

For this demo I have used English, Dutch and french languages.

I have added three resource files naming as follow

Resource.resx -> default resource file for English language

Resource.fr.resx -> French language

Resource.nl.resx -> Dutch

Add each language words for the English word “Wel Come”

resource

resource.fr

resource.nl

add words as above and build the project.

step 6

add view to change language. Wellcome text will be changes according to language selections.

It is used in view as @Resources.Resource.wellcome .

Behind the seen it is checked current culture and set appropriate word using resource files.

view

step 7 

Done. Run the project and click links to change language

Capture1

Capture2

Capture3