Sunday, March 6, 2016

"Cannot get Mail" The user name or password for imap.gmail.com is incorrect iOS

For iOS if you are using Apple configurator to setup gmail make sure you follow these steps.


If you followed the steps above and still getting the incorrect username or password error make sure you have IMAP enabled on your gmail account.

Still if the issue persists then in your google/gmail security settings make sure you have enabled the Allow less secure apps flag.


Tuesday, January 19, 2016

The this keyword in javascript explained

this keyword in Javascript is a source for confusion. The confusion is mostly due to the fact that the 'this' keyword is used differently in other languages compared to Javascript.

I was also confused of this (Well I still am sometimes) as I was coming from a C# background where the 'this' keyword meant the current instance of that class.

I hope the following will help someone to avoid the 'this' confusion altogether in Javascript.

Let's take an example,


function bar()
{
    console.log(this.age);
}


So what will be the value of 'this'?

Is it an object or is it referring to the function it self?

What if I told you that you cannot answer this question at all at compile time.

The trick is that the value of 'this' would depend on how the function is called.

e.g 1, (Run this on the browser JS console)

bar();

The output would be undefined.

So what was the 'this' object in the above example ? It was the global object (aka window object in the browser)

So naturally the window object did not have a property named 'age' thus the undefined output.

An easy way to think of this is who is calling the bar() function, if its no one then 'this' is going to be the global object.


e.g 2,

var x = { age : 10 , foo : bar };

x.foo();

The above code will actually call the bar function as foo is pointing to bar.

The output will be 10

So in this example bar is being called by x and thus the 'this' object is the x object.


e.g 3,
var y = { age : 15 };
bar.call(y);

The output will be 15

This is because the call() function explicitly sets the this object to the first argument passed into the call function.


e.g 4,

var z = new bar();

Now the 'this' object will be a brand new object and it will also get assigned to z.
This is because of the nature of the 'new' keyword in Javascript.

So the bottom line is the 'this' keyword will depend on how its containing function is called and look to the left of the function call site to determine 'this'


Hope this helps.








Monday, September 28, 2015

ASP.NET Concurrent Ajax Requests and slow performance

While working on a client project I came across an interesting issue. That is when concurrent Ajax requests (originating from Jquery ajax) came in the asp.net web service (A set of aspx web methods in this case) were performing ridiculously slow. The methods it self were executing pretty fast. It turned out that when you have "[WebMethod(EnableSession = true)]" the requests are processed sequentially with in the same session.

The fix is to make the session read only, that is if your web methods are not modifying the session.

If any other page methods are modifying the session then the best approach is to move your web methods to a different aspx file and make the session read only for that page. Below given is the page directive for doing that,

<% @Page EnableSessionState="ReadOnly" %>

Hope this helps.

Monday, November 17, 2014

cassandra vs rdbms data model

If you are coming from a relational database world like me then the initial terminology used in cassandra will be a bit confusing.
I'll state some keywords which are used in cassandra and try to draw parallels of them to the relational world.

Note: These parallels are just for ease of understanding and actually does not equal to each other at all.

Cassandra keyword
RDBMS keyword
Keyspace
Database
Column family
Table
Column
Table Column
Row
Table row

The outermost element of Cassandra is a cluster which can be one or more nodes set up as a ring which appears as a single instance to the end user.

A cluster usually contains one or more keyspaces (Databases).

A keyspace contains one or more column families  (Tables)

A column family contains one or more rows. (Slightly different from a Table which would contain columns )

A row contains one or more columns.

The biggest difference is that the number and type of columns in rows can  differ from one another thus making the Cassandra column family much more flexible compared with the rigid table structure in the rdbms world.

In other words each row in  a column family can have its own schema. This is an extremely useful option for storing dynamic data

Sunday, August 3, 2014

Cannot read remote side has closed Thrift Cassandra C#

I was seeing this error ("Cannot read remote side has closed") while trying to get Thrift API to work with Cassandra using Csharp. For the life of me I couldn't figure out what was wrong as even though the error is saying something like "Hey I think your Thrift server is down". That was not the case because I was connecting to My Cassandra server using other client libraries that was using Thrift under the hood. Eventually I figured out the error, the offending line was transport.Open(); When I replaced it with cassandraClient.InputProtocol.Transport.Open(); The error went away.

Here is the full snippet,

            TSocket socket = null;

            TTransport transport = null;

        

            socket = new TSocket("localhost", 9160);

          

          

            transport = new TFramedTransport(socket);

            TProtocol protocol = new TBinaryProtocol(transport);

            CassandraClient cassandraClient = new CassandraClient(protocol);

            cassandraClient.InputProtocol.Transport.Open();



            string s = cassandraClient.describe_cluster_name();

            List<ksdef> keyspaces = cassandraClient.describe_keyspaces();





Hope this will help another poor soul.

Thursday, June 26, 2014

SQL GROUP BY Example

The GROUP BY clause causes data in a table (or any data source) to be divided into groups based on the expressions (or conditions) given in the GROUP BY clause. Seems confusing? Probably.. Let's see what it means,

Say you have Column1, Column 2..etc on your table.

Now you say GROUP BY Column1 , what this does is it divides your table's data based on Column1. So all data which has  Column1='Apple' will be in one group, then all data which has  Column1='Orange' will be in another group.

Again if you had GROUP BY Column1, Column2 then your data will be divided into groups of Column1 + Column2 combinations . If Column2 had values of Green and Red in different rows then one group would be based on Apple+Red another group Apple+Green. So you get the point.

So in a nutshell,

GROUP BY creates groups for output rows, according to unique combination of values specified in the GROUP BY clause

But why would you need to divide them into groups? So we can operate on those groups.

I'll use AdventureWorks database as an example.

Download it here,
https://msftdbprodsamples.codeplex.com/releases/view/93587


SELECT SalesPersonID, YEAR(OrderDate) AS OrderYear,
COUNT(CustomerID) AS All_Customers
FROM Sales.SalesOrderHeader
GROUP BY SalesPersonID, YEAR(OrderDate);

The above query will return all unique combinations of SalesPersonID  + YEAR(OrderDate) and display all customers each of those combinations.

In other words this means you are seeing all customers for a particular Sales Person in a given year.

Point to note:

SELECT DISTINCT SalesPersonID ,YEAR(OrderDate) AS OrderYear
FROM Sales.SalesOrderHeader

and

SELECT  SalesPersonID ,YEAR(OrderDate) AS OrderYear
FROM Sales.SalesOrderHeader
GROUP BY SalesPersonID, YEAR(OrderDate);

Will give you the same results.

Saturday, May 24, 2014

ASP.Net Webforms Inline tags and code blocks

There are many elements inside a webforms (.aspx) page. To somebody beginning asp.net webforms these may seem quite similar and be confusing at first. (Well it was for me when I started learning asp.net webforms)
So hopefully this might help another soul.

Elements of a asp.net page (Inline tags and code blocks) can be listed as follows,

1. Directives

These come in many flavors like Page directives,
 ex : <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"  Trace="true" %>

or  Import directives,
ex : <%@ namespace="System.Drawing" %>

There are many more directives other than these two.

2. ASP.Net expressions

This is expression builder syntax, typically used to get values from web.config
ex: <%$ ConnectionStrings:AdventureWorks2008 %>  

3. User Controls

These are references to user created controls which are similar to web controls that enhances reusability 
ex: <PH:MainMenu Title="Menu Control" id="mainmenu" runat="server" />

4. Render blocks

Used for outputting elements like variables
ex:  If you have a variable named age then,
<%= age %>
will output the value of age

5. Code blocks

These are similar to javascript tags just that these run on the server,
<script runat="Server" language="c#" > ....some server side code here .... </script>  

6. Server Controls

These are the controls we drag and drop from the designer (Labels, Buttons etc)
ex: <asp:Label id="myLabel" runat="Server" />

7. Data binding controls

Used for biniding expressions such as Eval or Bind, these are typically used with data controls like Gridview
ex : <%# Eval("DBFieldName") %>