Wednesday, February 23, 2011

Xsl include vs import

xsl include vs import

I've recently been asked by a friend to explain the difference between include and import and how it would affect the templates being matched in these files. The answer is very simple, but you also must understand that there are other ways to foce which template will be called in the case that were are identical matchings.

First of all, let's explain "order of precedence". Basically, it gives the xslt transform engine the order in which templates will be prioritized. If two templates in the same document match the same path, then we cannot guarantee which will take precendence unless we specificy the priority attribute on the template. (The value of this attribute is an integer where the lower value has a higher precendence).

When we are importing an xsl file, using xsl:import, this is different. The file being imported always has a lower precedence than the current file. This means that if you have a template matching //Donkey and include a file called specialdonkey.xsl which contains a template //Donkey then the one in the current file will always be called over the one in the specialdonkey.xsl file.

When you include a file using xsl:include, you have the exact same order of precedence as the current file. Therefore, it is just like if the included file was copy/pasted into the current file. As we stated earlier, templates in the same file, that match the same xpath, who do not have a priority attribute, cannot guarantee which will be called. Therefore, if you plan on including an xsl file, make sure that it does not have conflicting templates and if so, either use the priority attribute, or take a look at our mode trick below.

/********Brief Description **********/

There is a common problem in XSLT where you have two XML of the same xPath (whether it be absolute or relative) that end up taking the same template. In many scenarios you do not have the ability to modify the XML so that it suits your XSLT and you must therefore turn to the mode trick to create seperate templates.

For the following example, say you had the following XML.

<Shop>
<FirstCartProduct>
<Product id="123" name="Apples" weight="2.4jg" price="$5.99"/>
</FirstCartProduct>
<CartReferences>
<Product ref="98342kjsd" id="123"/>
</CartReferences>
</Shop>

Now as you can see here, you could enter the FirstCartProduct template and want to do an apply-templates on your Product so that you display the products but then you would enter CartReferences, do any apply templates and end up with a buggy output since these Products do not have the same fields.

What we can do to resolve this issue is to give the templates a mode. By supplying the attribute mode with a unique identifier as a value, you can have multiple templates that match the same xPath but are applied from different sources.

For example, in the FirstCartProduct template, we could do an and then we would have a template that can only be applied from there (which means that the one in CartReference would no longer be applied) by doing this:

<xsl:template match="Product" mode="DisplayProduct">
Name: <xsl:value-of select="@name">
</xsl:template>

In the distinctSection there is no restriction you can start from any of the root map only you need to set the distinct control id in under use property, this distinct key will automatically find the all xml and distinct all the values mapped in the use property, Now using generic-id() you can easily get this distinct values of your xml.


I have placed here an example for my reference:-

<xsl:key name="distinctSection" match="elements/element[@type='Custom' and @title='Pay_Premium']/custom" use="./pansectitle"/>

<xsl:for-each select="/site/elements/element[@type='Custom']/custom[generate-id() = generate-id(key('distinctSection', ./pansectitle))]">

<xsl:value-of select="position()"/> : <xsl:value-of select="pantitle"/> [<xsl:value-of select="pansectitle"/>]

</xsl:for-each>

Tuesday, February 22, 2011

Set Max 85 Char in Xsl Substring

I want to set the char length max 50 Char more then 50 char should be not visible and make sure string willn't terminate in between the sentence/word


<xsl:value-of select="substring($bodyContent, 1, 85 + string-length(substring-before(substring($bodyContent, 86), ' ')))" disable-output-escaping="yes"/>

Thursday, February 17, 2011

Could not load file or assembly 'AjaxControlToolkit' or one of its dependencies.

I am always getting this type of errors while publishing the sites like Could not load file or assembly 'AjaxControlToolkit' or one of its dependencies. like

Line 1:  <%@ Page Language="C#" AutoEventWireup="true" CodeFile="index.aspx.cs" Inherits="index" %>
Line 2:
Line 3: <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>Line 4:
Line 5:


To resolve this issue i had followed these steps, might be this will helpful to others also

1. Registered the assembly by using the following code:

<%@ Register Assembly="AjaxControlToolkit"
Namespace="AjaxControlToolkit" TagPrefix="ajaxControl" %>


You have already done the first step.

2. Copy the AjaxControlToolkit.dll into the /bin folder of the web site application or download library file from this location Download.

3. You must have included ScriptManager in your files

4. If Yet this problem isn't resolved please add/replace this line in your config file
<add assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>

Tuesday, February 15, 2011

SQL SERVER – Query to Find ByteSize of All the Tables in Database

I Am writing here two ways to find the database table size

Method One 1:
---------------------------------------------------

SELECT
CASE WHEN (GROUPING(sob.name)=1) THEN 'All_Tables'
ELSE ISNULL(sob.name, 'unknown') END AS Table_name,
SUM(sys.length) AS Byte_LengthFROM sysobjects sob, syscolumns sysWHERE sob.xtype='u' AND sys.id=sob.idGROUP BY sob.nameWITH CUBE


Method One 2:

---------------------------------------------------
Ever wonder how big a table really is in your database? You know there are a million rows in the table, but how much space is that really taking?

SQL Server provides a built-in stored procedure that you can run to easily show the size of a table, including the size of the indexes… which might surprise you.
Syntax:   sp_spaceused ‘Tablename’

Method One 3:
---------------------------------------------------
Actually SQL Server gives you everything you need with its Stored Procedure sp_spaceused. Unfortunately this SP does not support iterating over all tables in a database, so we needed to leverage another (undocumented) Stored Procedure sp_msForEachTable.

SET NOCOUNT ON 
DBCC UPDATEUSAGE(0) 
-- DB size.
EXEC sp_spaceused

-- Table row counts and sizes.
CREATE TABLE #t 
( 
    [name] NVARCHAR(128),
    [rows] CHAR(11),
    reserved VARCHAR(18), 
    data VARCHAR(18), 
    index_size VARCHAR(18),
    unused VARCHAR(18)
) 

INSERT #t EXEC sp_msForEachTable 'EXEC sp_spaceused ''?''' 

SELECT *
FROM   #t

-- # of rows.
SELECT SUM(CAST([rows] AS int)) AS [rows]
FROM   #t
 
DROP TABLE #t 
 
 
Method One 4:
---------------------------------------------------
CREATE PROCEDURE GetAllTableSizes
AS
/*
Obtains spaced used data for ALL user tables in the database
*/
DECLARE @TableName VARCHAR(100) --For storing values in the cursor
--Cursor to get the name of all user tables from the sysobjects listing
DECLARE tableCursor CURSOR
FOR
select [name]
from dbo.sysobjects
where OBJECTPROPERTY(id, N'IsUserTable') = 1
FOR READ ONLY
--A procedure level temp table to store the results
CREATE TABLE #TempTable
(
tableName varchar(100),
numberofRows varchar(100),
reservedSize varchar(50),
dataSize varchar(50),
indexSize varchar(50),
unusedSize varchar(50)
)
--Open the cursor
OPEN tableCursor
--Get the first table name from the cursor
FETCH NEXT FROM tableCursor INTO @TableName
--Loop until the cursor was not able to fetch
WHILE (@@Fetch_Status >= 0)
BEGIN
--Dump the results of the sp_spaceused query to the temp table
INSERT #TempTable
EXEC sp_spaceused @TableName
--Get the next table name
FETCH NEXT FROM tableCursor INTO @TableName
END
--Get rid of the cursor
CLOSE tableCursor
DEALLOCATE tableCursor
--Select all records so we can use the reults
SELECT *
FROM #TempTable
--Final cleanup!
DROP TABLE #TempTable
GO

Exec GetAllTableSizes

Wednesday, February 2, 2011

Bind Repeater form sql datasource

<asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1" 
        onitemdatabound="Repeater1_ItemDataBound">
    <HeaderTemplate>
        <table border="1" cellpadding="5px">
    </HeaderTemplate>

    <ItemTemplate>
            <!-- Image -->
            <tr>
                <td colspan="2">
                    <asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval("ImgId", "~/Image.aspx?id={0}") %>' />
                </td>
            </tr>
            <!-- Message/Date -->
            <tr>
                <td>
                    <%# Eval("ImgMessage"%>
                </td>
                <td>
                    <%#Eval("ImgDate""{0:d}")%>
                </td>
            </tr>
    </ItemTemplate>

    <FooterTemplate>
        </table>
    </FooterTemplate>
</asp:Repeater>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
    SelectCommand="SELECT * FROM [Images]"></asp:SqlDataSource>

Monday, January 31, 2011

An in depth discussion of JavaScript Arrays - Working with two dimensional arrays: discussion

Within the code in the previous section, I mainly created a simple button (which is identified as “ButtonShow”). The button is defined with an “onclick” event which calls a JavaScript function “ButtonShow_onclick”, which is defined as follows:

function ButtonShow_onclick() {
Show();
}

The above function simply calls another JavaScript function named “Show.” The function “Show” is defined as follows:

function Show()
{
var ITArray = new Array(2300, 3105, 2909, 4800);
var ProductionArray = new Array(1800, 1940, 2470, 4350);
var ResearchArray = new Array(900, 1200, 1923, 3810);
var salaryArray = new Array(ITArray, ProductionArray, ResearchArray);

for (var i = 0; i < salaryArray.length; i++) {
for (var j = 0; j < salaryArray[i].length; j++) {
document.write(salaryArray[i][j] + "\t");
}
document.write("
")
}
}

Before starting this discussion, we need to understand what we are creating (in memory) from the above code. Let me provide you a logical view for the array (“salaryArray”) in the above code. Consider the following figure (Fig 01):

Just consider the above figure as a small part of memory identified by “salaryArray.” All the black numbers correspond to values. All the reds are access notations (the positions) for the values given in black. Every access notation is a combination of a row index (in blue) and a column index (in green).

We are trying to create three single dimensional arrays as part of the main array, and thus we describe the main array as a “two dimensional” array (which is very similar to the concept of matrices in mathematics).

According to the above code, the three single dimensional arrays (or rows) are “ITArray”, “ProductionArray” and “ResearchArray. All of them have their own values (but the number of values in all of them is generally the same). We created a main array named “salaryArray” which contains the three arrays in the form of a list (thus forming a table type of view).

Finally, we retrieve and display all the values in the “salaryArray” with the following nested loop:

for (var i = 0; i < salaryArray.length; i++) {
for (var j = 0; j < salaryArray[i].length; j++) {
document.write(salaryArray[i][j] + "\t");
}
document.write("
")
}

With the above code fragment, the variable “i” corresponds to the “row index” and the variable “j” corresponds to the “column index.”