Hello world!

Welcome to WordPress. This is your first post. Edit or delete it, then start blogging!

Posted in Uncategorized | 1 Comment

The C# @ String Literal

C# is a pretty sweet language, and there are many, many, many little things that just make the code that much nicer. Have you ever been in a situation where for some reason you NEEDED to have a string/code fragment/js fragment/sql statement inline in your code because you don’t believe in resource files or stored procedures? 



Aside from whatever great debate about “if you should or shouldn’t”. If you are going to, please, please, learn to take advantage of what C# has to offer, yes I’m talking about the ‘@’ string literal.


Now I’m not going to target anyone in particular, but my feeling is that VB as a language seems to be more notorious for having hundreds of lines of String.Append()s. String.Append is the absolute worst, hardest and most illegible way of possibly including your fragment inline in code.


I’d say the most precious and special super power of a string literal is its multi-line ability. So instead of writing code that may look something like:
var1 += @”some text” + Environment.NewLine;
var1 += @”some more text” + Environment.NewLine;
var1 += @”even more text” + Environment.NewLine;

You could just use the @-quoting and write:
var1 = @”some text
some more text
event more text”;

There are also the programmers that ‘know of’ the @ symbol and prefix all their strings with it because it looks cool or something. Doing this without reason may actually alter the behaviour you excepted because it causes escape sequences to NOT be processed.
Eg. “c:\\my\\file.txt” could be done as @”c:\my\file.txt”
This means your \n and \t or whatever it is will also not be processed.

Posted in C#, Development | Leave a comment

>JSON – Insight

>

JSON stand for  JavaScript Object Notation.
It is a lightweight text-based open standard designed for human-readable data interchange.
It is derived from the JavaScript  programming language for representing simple data structures and associative arrays, called objects.
Despite its relationship to JavaScript, it is language-independent, with parsers available for virtually every programming language. The JSON filename extension is .json.
It is easy for humans to read and write. It is easy for machines to parse and generate.
Example:
{"skillz": {"web":[{"name": "html","years": "5"},{"name": "css","years": "3"}],"database":[{"name": "sql","years": "7"}]}}

Squiggles, Squares, Colons and Commas
1. Squiggly brackets act as ‘containers’ { }
2. Square brackets holds arrays [ ]
3. Names and values are separated by a colon. :
4. Array elements are separated by commas. 

Posted in Development | Leave a comment

>All about Client ID Mode in ASP.NET 4

>

Introduction
We have been using ClientID’s in ASP.NET 2.0/3.5 that makes each control to generate the unique client side id attribute to that page or browser. But these ID’s were long and randomly generated. Developers who work on Client-Side programming that uses Java Script, JQuery or Ajax have been suffering a lot spending considerable amount of time when it they need to reference those ClientID’s in the client side scripts.
Now the good news is that, ASP.NET 4.0 comes with new ClientIDMode property, gives full control to the developer on the ClientID’s generated by ASP.NET controls.
ClientIDMode can take the following four possible values
  • AutoID – ASP.NET generate IDs as it does in v3.5 and earlier versions.
  • Static – ASP.NET use exactly the same ID given to the server control for its client-side ID.
  • Predictable – ASP.NET try to generate IDs that are guessable from looking at the structure of the page.
  • Inherit – ASP.NET generate a client-side ID for the page or control using the same ClientIDMode as its parent. i.e. the client ID gets inherited from the parent control.

You can set this property in 3 ways
1.Control Level
2.Page Level
3.Application Level
Setting ClientIDMode at Control Level
Each and every server control in ASP.NET 4.0 has this property and the default value is inherit.

1.<asp:panel id="pnl" runat="server" cssclass="newStyle1"
2.ClientIDMode ="Static"> </asp:panel>

Setting ClientIDMode at Page Level

1.<%@ Page Language="C#" ClientIDMode ="Inherit"
2.AutoEventWireup="true"
3.CodeBehind="Category.aspx.cs"
4.Inherits="WebApplication3.Cat" %>

Setting ClientIDMode at Application Level
You need to set it at System.Web section of Web.config

1.<system.web>
2.<pages clientIDMode="Predictable">
3.</pages>
4.</system.web>

ClientIDRowSuffix
Another interesting feature of the ClientID improvement in ASP.NET 4.0 is the ClientIDRowSuffix .This can be applied to DataBound or List controls. This is used to take control on how ID values for template controls in databound controls are generated. This requires that the ClientIDMode is set to Predictable.

1.<asp:GridView runat="server"
2.ID="gvEmp" AutoGenerateColumns="False"
3.ClientIDMode ="Predictable" ClientIDRowSuffix="EMPID">
4.</asp:GridView>

Best Practices

  • Add ClientIDMode = “Static” in application level web.config
  • Using ClientIDMode = “AutoId” will work the best even in worst cases.
  • Add ClientIDMode = “Predictable” to each List Control Children Item Template of Databound Controls.
  • When ever naming conflicts occurs Override ClientIDMode to Predictable
  • When working with Web Server Control Development leave at default behavior i.e. Inherit from parent
  • Override if and only if necessary that might be in individual sub controls 
Posted in C#, Development | Leave a comment

>Recovering a dropped stash in GIT

>

the GUI way
You dropped a stash that was created recently, and now you want to recover it. As long as you did not do a garbage collection in between, this should work:
gitk $(git fsck | grep commit | cut -f3 -d' ') --since='1 week ago'
The part within the parenthesis finds all unreachable commit objects and returns their hashes. If you never did a garbage collect there might be too many false positives so the --since clause (which you can adjust to whatever you want of course; mine is just an example) limits the display to commits created recently.
A “stash” has a very recognisable, triangular, shape in the commit DAG, and with gitk you can visually find stashes really fast. For me, this is the kind of task that calls out for a GUI — infrequently required, no conceivable need to automate, and containing data that stands out visually.


the command line way
Writing something longer than that for a problem that occurs once in a blue moon is not my style (very low ROI!), but doener gave me this:
git fsck --unreachable | grep commit | cut -d\  -f3 |xargs git log --no-walk --merges --grep='^\(WIP on \|On \)\((no branch)\|[^ ]\+\):'
I knew about --merges but not --no-walk, and I anticipate using it a lot more in future :-)
Posted in Development | Leave a comment

>Temporary Tables vs Cursors

>

There is nothing wrong with cursors except that 99.99% of the time there is a faster set based way to do accomplish what you want.  That's not always true, sometimes a cursor is faster and sometimes it's the only way to do something.  But those cases are very rare.  And the performance hit for using cursors can be very large.  And the performance hit gets worse as the amount of data increases.


Cursors work row-by-row and are extremely poor performers. They can in almost all cases be replaced by better set-based code (not normally temp tables though)  Temp tables can be fine or bad depending on the data amount and what you are doing with them. They are not generally a replacement for a cursor.

Avoidance of cursons is not not just personal taste.  Because of the performance problems, you should almost always avoid them. 
Here is the syntax for creating the dummy table: 
DECLARE @myTable AS TABLE ( [Index] INT NOT NULL IDENTITY(1, 1) ,Dbname NVARCHAR(max) ,TrackerCount INT )
Posted in RDBMS, SQL Server | Leave a comment

>Iterating through all databases on the SQL Server.

>

Here is the SQL script that will run through all databases on the SQL Server. These kind of scripts are really handy when you are generating a report or auditing your databases.

DECLARE @db_name varchar(300)
DECLARE @Schema varchar(200)
DECLARE @tableName varchar(200)
DECLARE @InSQL varchar(200)


SET @tableName = ‘ItemDetailTypes’
SELECT @db_name = NAME FROM master.sys.SYSDATABASES ORDER BY NAME 
WHILE @@ROWCOUNT > 0
BEGIN
SET @InSQL = ‘[' + @db_name + '].[' + @Schema + '].[ + @tableName + ']‘
IF OBJECT_ID(@InSQL,’U') IS NOT NULL
BEGIN 
print @db_name
END
SELECT @db_name = NAME FROM master.sys.SYSDATABASES WHERE NAME < @db_name ORDER BY NAME 
END
SET ROWCOUNT 0

Posted in SQL Server | Leave a comment

>git – The Version Control System

>

Git is a version control system used by development and programming teams, popular open source projects, and other team collaboration projects. 

Git is a distributed revision control system, which just means that if you have multiple people in a project, they can work individually without being connected to a central network, and then they can just push to the project when they are ready.



Advantages of Using Git


  • Git is super easy to install: I will take you through the installation process – it’s a breeze.
  • Git is easier to learn compared to other systems: by the end of this guide, you will have enough knowledge to get going with Git.
  • Git is fast: So much so that it doesn’t become one of those things you have to force yourself to remember to do and you can integrate it seamlessly with your current workflow.
  • Git is decentralized: If many people are working on a project, they each can have their own copy and not save over each other.


Disadvantages of Using Git
  • Git has a learning curve: Whilst I did say that it’s one of the easier version control systems to use, any new thing you introduce to your workflow will need some learning time. Learning Git will be similar to learning a new software application such as Word or Excel.
Posted in Development | Leave a comment

>RANK, DENSE_RANK, FIRST and LAST Analytic Functions

>

RANK
Let’s assume we want to assign a sequential order, or rank, to people within a department based on salary, we might use the RANK function like:
SELECT empno,
       deptno,
       sal,
       RANK() OVER (PARTITION BY deptno ORDER BY sal) “rank”
FROM   emp;


     EMPNO     DEPTNO        SAL       rank
———- ———- ———- ———-
      7934         10       1300          1
      7782         10       2450          2
      7839         10       5000          3
      7369         20        800          1
      7876         20       1100          2
      7566         20       2975          3
      7788         20       3000          4
      7902         20       3000          4
      7900         30        950          1
      7654         30       1250          2
      7521         30       1250          2
      7844         30       1500          4
      7499         30       1600          5
      7698         30       2850          6


SQL>
What we see here is where two people have the same salary they are assigned the same rank. When multiple rows share the same rank the next rank in the sequence is not consecutive.

DENSE_RANK

The DENSE_RANK function acts like the RANK function except that it assigns consecutive ranks:
SELECT empno,
       deptno,
       sal,
       DENSE_RANK() OVER (PARTITION BY deptno ORDER BY sal) “rank”
FROM   emp;


     EMPNO     DEPTNO        SAL       rank
———- ———- ———- ———-
      7934         10       1300          1
      7782         10       2450          2
      7839         10       5000          3
      7369         20        800          1
      7876         20       1100          2
      7566         20       2975          3
      7788         20       3000          4
      7902         20       3000          4
      7900         30        950          1
      7654         30       1250          2
      7521         30       1250          2
      7844         30       1500          3
      7499         30       1600          4
      7698         30       2850          5


SQL>


FIRST and LAST

The FIRST and LAST functions can be used to return the first or last value from an ordered sequence. Say we want to display the salary of each employee, along with the lowest and highest within their department we may use something like:
SELECT empno,
       deptno,
       sal,
       MIN(sal) KEEP (DENSE_RANK FIRST ORDER BY sal) OVER (PARTITION BY deptno) “Lowest”,
       MAX(sal) KEEP (DENSE_RANK LAST ORDER BY sal) OVER (PARTITION BY deptno) “Highest”
FROM   emp
ORDER BY deptno, sal;


     EMPNO     DEPTNO        SAL     Lowest    Highest
———- ———- ———- ———- ———-
      7934         10       1300       1300       5000
      7782         10       2450       1300       5000
      7839         10       5000       1300       5000
      7369         20        800        800       3000
      7876         20       1100        800       3000
      7566         20       2975        800       3000
      7788         20       3000        800       3000
      7902         20       3000        800       3000
      7900         30        950        950       2850
      7654         30       1250        950       2850
      7521         30       1250        950       2850
      7844         30       1500        950       2850
      7499         30       1600        950       2850
      7698         30       2850        950       2850


SQL>

Posted in RDBMS | Leave a comment

>LAG and LEAD Analytic Functions

>

The LAG and LEAD analytic functions were introduced in 8.1.6 to give access to multiple rows within a table, they prove to be really handy and you can easily omit the need for a self-join.

Both LAG and LEAD functions have the same usage, as shown below.
LAG  (value_expression [,offset] [,default]) OVER ([query_partition_clause] order_by_clause)
LEAD (value_expression [,offset] [,default]) OVER ([query_partition_clause] order_by_clause)
value_expression – Can be a column or a built-in function, except for other analytic functions.
offset – The number of rows preceeding/following the current row, from which the data is to be retrieved. The default value is 1.
default – The value returned if the offset is outside the scope of the window. The default value is NULL.
Looking at the EMP table, we query the data in salary (SAL) order.

SELECT empno,
       ename,
       job,
       sal
FROM   emp
ORDER BY sal;


     EMPNO ENAME      JOB              SAL
———- ———- ——— ———-
      7369 SMITH      CLERK            800
      7900 JAMES      CLERK            950
      7876 ADAMS      CLERK           1100
      7521 WARD       SALESMAN        1250
      7654 MARTIN     SALESMAN        1250
      7934 MILLER     CLERK           1300
      7844 TURNER     SALESMAN        1500
      7499 ALLEN      SALESMAN        1600
      7782 CLARK      MANAGER         2450
      7698 BLAKE      MANAGER         2850
      7566 JONES      MANAGER         2975
      7788 SCOTT      ANALYST         3000
      7902 FORD       ANALYST         3000
      7839 KING       PRESIDENT       5000


SQL>

LAG
The LAG function is used to access data from a previous row. The following query returns the salary from the previous row to calculate the difference between the salary of the current row and that of the previous row. Notice that the ORDER BY of the LAG function is used to order the data by salary.
SELECT empno,
       ename,
       job,
       sal,
       LAG(sal, 1, 0) OVER (ORDER BY sal) AS sal_prev,
       sal – LAG(sal, 1, 0) OVER (ORDER BY sal) AS sal_diff
FROM   emp;


     EMPNO ENAME      JOB              SAL   SAL_PREV   SAL_DIFF
———- ———- ——— ———- ———- ———-
      7369 SMITH      CLERK            800          0        800
      7900 JAMES      CLERK            950        800        150
      7876 ADAMS      CLERK           1100        950        150
      7521 WARD       SALESMAN        1250       1100        150
      7654 MARTIN     SALESMAN        1250       1250          0
      7934 MILLER     CLERK           1300       1250         50
      7844 TURNER     SALESMAN        1500       1300        200
      7499 ALLEN      SALESMAN        1600       1500        100
      7782 CLARK      MANAGER         2450       1600        850
      7698 BLAKE      MANAGER         2850       2450        400
      7566 JONES      MANAGER         2975       2850        125
      7788 SCOTT      ANALYST         3000       2975         25
      7902 FORD       ANALYST         3000       3000          0
      7839 KING       PRESIDENT       5000       3000       2000


SQL>


LEAD
The LEAD function is used to return data from the next row. The following query returns the salary from the next row to calulate the difference between the salary of the current row and the following row.
SELECT empno,
       ename,
       job,
       sal,
       LEAD(sal, 1, 0) OVER (ORDER BY sal) AS sal_next,
       LEAD(sal, 1, 0) OVER (ORDER BY sal) – sal AS sal_diff
FROM   emp;


     EMPNO ENAME      JOB              SAL   SAL_NEXT   SAL_DIFF
———- ———- ——— ———- ———- ———-
      7369 SMITH      CLERK            800        950        150
      7900 JAMES      CLERK            950       1100        150
      7876 ADAMS      CLERK           1100       1250        150
      7521 WARD       SALESMAN        1250       1250          0
      7654 MARTIN     SALESMAN        1250       1300         50
      7934 MILLER     CLERK           1300       1500        200
      7844 TURNER     SALESMAN        1500       1600        100
      7499 ALLEN      SALESMAN        1600       2450        850
      7782 CLARK      MANAGER         2450       2850        400
      7698 BLAKE      MANAGER         2850       2975        125
      7566 JONES      MANAGER         2975       3000         25
      7788 SCOTT      ANALYST         3000       3000          0
      7902 FORD       ANALYST         3000       5000       2000
      7839 KING       PRESIDENT       5000          0      -5000


SQL>

Posted in RDBMS | Leave a comment