Wednesday, 20 July 2016

XSL - If Else condition Choose When Otherwise

In XSL, there is no If - Else construct. Only If is present.

If your requirement is stricly If - Else, instead use the Choose When Otherwise construct as below:

<xsl:choose>
 <xsl:when test="$a > $b">
  <h2> A is greater than B </h2>
 </xsl:when>
 <xsl:otherwise>
  <h2> B is greater than A </h2>
 </xsl:otherwise>
</xsl:choose>

In the above example, a ($a) and b ($b), are variables.

Tuesday, 19 July 2016

XSL - Check if item is contained in a list

Use the below sample if you want to check if an item is contained in a list.
This sample is using XSL and XPATH with XML input.
In the below sample:
<!-- .xsl file -->

<!-- List of items to be checked against -->
<xsl:variable name="students" select="'Jon Tom Ana Ben'" />

<!-- Item to be checked if contained in list -->
<xsl:variable name="classTopper" select="'Ana'" />

<!-- Alternately the item can be dynamically taken from the xml input file as below -->
<!-- <xsl:variable name="classTopper" select="/root/@topper" /> -->

<!-- Check if class topper is in the students list -->
<xsl:if test="
  contains(
    concat(' ', $students, ' '),
    concat(' ', $classTopper, ' ')
  )
">
  <xsl:value-of select="concat('Student ', $classTopper, ' is in the students list.')" />
</xsl:if>

Note: Here space is used as separator in the list. You can replace with something else like a pipe "|". In that case, the concat in contains() will be like:
concat ('|', $list, '|') ...

Thursday, 14 July 2016

JavaScript - Replace Comma with New Line

Use JavaScript String functions "split" and "join":
var formattedString = yourString.split(",").join("\n")
If you'd like the newlines to be HTML line breaks replace "\n" with "<br />"

Alternate method using "replace" function.
yourString.replace(/,/g, '\n');

Monday, 11 April 2016

SQL - Insert Multiple Rows in a Single Query

Below code snippets can be used to insert multiple rows in a Single SQL Query Statement.
This will be faster than using multiple Insert statements.

INSERT INTO MyTable ( Column1, Column2 ) 
VALUES
  ( Value1, Value2 ), 
  ( Value1, Value2 );

-- Ex:

INSERT INTO tblPerson (First,Last) 
VALUES 
  ('Fred','Smith'),
  ('John','Smith'),
  ('Michael','Smith'),
  ('Robert','Smith');

-- If data is from another table
INSERT INTO table1 ( col1, col2, col3, col4 )
SELECT  table2.column1, table2.column2, 8, 'some string etc.'
FROM    table2
WHERE   table2.ID = 7;

Thursday, 17 March 2016

C# - String to Stream & Stream to String

Following code can be used to convert Stream to String and Stream to String using C#

Tuesday, 8 March 2016

ASP.NET MVC - WebGrid Column Width

Its easy to set the individual column width for ASP.NET MVC WebGrid. You can apply a CSS Style Class to the column. Find a sample below.

As in the sample above, you can apply multiple classes as well for the same column.

Friday, 12 February 2016

HTML / ASP.NET / Javascript - 301 Redirect

There are multiple ways to implement a 301 Redirect on a webpage.
Below are methods to do redirection as soon as the page gets the visit using HTML Meta tag, ASP.NET and Javascript.
The most preferred way is ASP.NET than HTML Meta tag and Javascript as it is the most compatible option.

The above sample will redirect user to http://yahoo.com as soon as he reaches this page.