Difference between == and === in JavaScript

So we know in javascript we have == and === comparisons. Why two? What are the differences between them?

So what is the difference between followings


'1' == 1

this returns true.

when it comes === equals


'1' === 1

this returns false.

So what it is?

The triple-equals comparator compares both type and contents

So ‘1’ === 1 contains string value and int value. Even contents are same they are in different types. So it is not equals.

Double equals tries to help with type coercion. But it’s not always the help we want. So keep in mind when you are using above equal comparisons.
 

format currency in jquery

So when we are in web development we want show Currency value with some formatting. Like thousand separator, decimal separator and currency symbol.

I am using below Jquery function to format the currency values.


function formatCurrency(value, decimals, decpoint, separator, symbol) {
decimals = (typeof decimals == 'undefined' ? 2 : decimals);
decpoint = (typeof decpoint == 'undefined' ? '.' : decpoint);
separator = (typeof separator == 'undefined' ? ',' : separator);
symbol = (typeof symbol == 'undefined' ? '$' : symbol);

var parts = value.toFixed(decimals).toString().split(decpoint);
parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, separator);

return (symbol + parts.join(decpoint)).toLocaleString();
}

We can format currency value as we required. If we just pass the currency value it will give us value with default formatting.

formatCurrency(5873839) will give us ” $5,873,839.00″

reading xml in sql server

So some times we want to read xml file in sql server. I am passing list of details to sql server using xml file format. It reduce several db calls when we passing list of data.

Suppose we have List of Students. In List of Student we have to loop it and fire db calls to save data. To overcome this I create xml of List of Student and Pass it to sql server with one db call and then in my stored procedure I will handle those data as follow.

DECLARE @Xml XML
DECLARE @HDoc INT

SET @Xml = '<ArrayOfStudent xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Student>
<Id>1</Id>
<FirstName>jeevan</FirstName>
<LastName>JLast name</LastName>
</Student>
<Student>
<Id>2</Id>
<FirstName>john</FirstName>
<LastName>johnLast name</LastName>
</Student>
</ArrayOfStudent>'
EXECUTE SP_XML_PREPAREDOCUMENT @HDoc OUTPUT,@Xml

SELECT [Id],
[FirstName],
[LastName]
INTO #Temp
FROM OPENXML(@HDoc, '/ArrayOfStudent/Student', 1)
WITH (
[Id] INT 'Id',
[FirstName] VARCHAR(400) 'FirstName',
[LastName] VARCHAR(400) 'LastName'
)

EXECUTE SP_XML_REMOVEDOCUMENT @HDoc

SELECT  *  FROM  #Temp

xml reading

Serializing Lists of Classes to XML

So some times we want to convert object to xml. We can follow severel cumbersome methods. So I am using Utlity methods to convert c# object to xml and xml to c# object.

Suppose we have class like below


public class Student
 {
 public int Id { get; set; }
 public string FirstName { get; set; }
 public string LastName { get; set; }
 }

And we want serialize List<Student> to xml. So we can use this utlity function


/// <summary>
/// Serialize object to xml
/// </summary>
/// <typeparam name="T">Object type</typeparam>
/// <param name="obj">Object</param>
/// <returns>XML</returns>
public static string ToXml<T>(T obj)
{
using (StringWriter stringWriter = new StringWriter(new StringBuilder(), CultureInfo.InvariantCulture))
{
XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
xmlSerializer.Serialize(stringWriter, obj);
return stringWriter.ToString();
}
}

As a example we populate List<student>  with dummy data


 List <Student> studentList = new List<Student>();

studentList.Add(new Student {
 FirstName = "jeevan",
 LastName = "JLast name",
 Id = 1
 });

studentList.Add(new Student
 {
 FirstName = "john",
 LastName = "johnLast name",
 Id = 2
 });

Then call out utility function


string xml = Utility.ToXml(studentList);

then out xml variable look like as below


<?xml version="1.0" encoding="utf-16"?>
<ArrayOfStudent xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Student>
<Id>1</Id>
<FirstName>jeevan</FirstName>
<LastName>JLast name</LastName>
</Student>
<Student>
<Id>2</Id>
<FirstName>john</FirstName>
<LastName>johnLast name</LastName>
</Student>
</ArrayOfStudent>

 

unable to open bcp host data-file

So when I am using bcp utility in sql server I got this error. All syntax are fine. I was wandering.

Issue was sql agent’s user not having permission to access host file location.

So first I wanted to know sql agent’s current user.

I used following query to sql server service acounts.


SELECT DSS.servicename,
DSS.startup_type_desc,
DSS.status_desc,
DSS.last_startup_time,
DSS.service_account,
DSS.is_clustered,
DSS.cluster_nodename,
DSS.filename,
DSS.startup_type,
DSS.status,
DSS.process_id
FROM sys.dm_server_services AS DSS;

Then grant permission to sql agent user. Everything works fine.

jQuery ajax only works in IE when the IE debugger is open

It was very annoying. Ajax request not works perfectly in IE. But when developer tool is opened ajax quires are works fine. It was very cumbersome . It happened because IE chache Ajax requests. When Developer tool opened it is always refresh from server.

I get this fixed by making cache false in ajax request.


$.ajax({
type: "GET",
url: url,
dataType: "html",
contentType: "application/json; charset=utf-8",
data: JSON.stringify({}),
cache: false,
success: function (r) {
}
});

I think it might help you.

Login failed for user IIS APPPOOL\DefaultAppPool

Again I got a Error.

Login failed for user iis APPPOOL\Default. I fixed this issue by running following sql script in sql server.


IF NOT EXISTS (SELECT name FROM sys.server_principals WHERE name = 'IIS APPPOOL\DefaultAppPool')
BEGIN
CREATE LOGIN [IIS APPPOOL\DefaultAppPool]
FROM WINDOWS WITH DEFAULT_DATABASE=[MISDB],
DEFAULT_LANGUAGE=[us_english]
END
GO
CREATE USER [WebDatabaseUser]
FOR LOGIN [IIS APPPOOL\DefaultAppPool]
GO
EXEC sp_addrolemember 'db_owner', 'WebDatabaseUser'
GO

Set your required database as default database.

 

This configuration section cannot be used at this path. This happens when the section is locked at a parent level. Locking is either by default (overrideModeDefault=”Deny”), or set explicitly by a location tag with overrideMode=”Deny” or the legacy allowOverride=”false”.

So when I am hosting ASP.NET MVC web site in iis 7 I got following error.

error1

So I followed following steps to get this solved.

Click “Start button”
in the search box, enter “Turn windows features on or off”
in the features window, Click: “Internet Information Services”
Click: “World Wide Web Services”
Click: “Application Development Features”
Check (enable) the features. I checked all but CGI.

Now it is solved. I think it might help you.