Monday, November 30, 2009

Calling an ASP.Net Web Service from Javascript


Page which calls the service has to have Script Manager and the Script Manager has to mention the ServiceReference attribute.

ScriptManager runat="server" ID="ScrpMgr1">
ScriptManagers>

Also, the Web.config file should have an extra entry for ScriptHandlerFactory.

System.Web.Script.Services.ScriptHandlerFactory" />

Below is my JavaScript method which calls the WebService. This has got a SuccessCallBack method to which the Service returns the result set.

var DivIDAct;
function MyWebServiceCallForActuals(ActualsDivId, BudgetYear, BudgetVersion, SalesDivision, row, Display)
{
expandcollapse(ActualsDivId, row, 2);
BrandSerivces.GetBrandsForActuals(BudgetYear, BudgetVersion, SalesDivision, '', SuccessCallBackActuals);
ActualsDivId = 'Division' + ActualsDivId;
var ActualsDivObj = document.getElementById(ActualsDivId);
DivIDAct = ActualsDivObj;
}

function SuccessCallBackActuals(result)
{
DivIDAct.innerHTML = result;
}

This is just to show an Expand Collapse button on click of each row item. A new row item would be displayed under the clicked row, which will show the details of the current row. The Javascript ExpandCollapse method takes care about swaping the Expand(Plus Image) and Collapse(Minus image) images. I am not going to explain much about this as this is self explanatory, just walk through the code.

function expandcollapse(obj, row, ch) {

var div;
var img;
if (ch == 1) {
div = document.getElementById('Div' + obj);
img = document.getElementById('img' + obj);
} else if (ch == 2) {
div = document.getElementById('Division' + obj);
img = document.getElementById('imgActuals' + obj);
}

if (div.style.display == "none") {
div.style.display = "block";
if (row == 'alt') {
img.src = "../App_Themes/Two/images/minus.gif";
}
else {
img.src = "../App_Themes/Two/images/minus.gif";
}
img.alt = "Close to view other Details";
}
else {
div.style.display = "none";
if (row == 'alt') {
img.src = "../App_Themes/Two/images/plus.gif";
}
else {
img.src = "../App_Themes/Two/images/plus.gif";
}
img.alt = "Expand to show Details";
}
}


Make a Dynamic Javascript Function Call from the ASP.Net Gridview Template fields.


Keep an Empty Div in a new Template Field where the Web Service result set (This result set would be returned by the SuccessCallBack method of javascript.) would be displayed. A detailed table for the corresponding row item.

<>
<div id="Division" style="overflow: auto; width: 100%">div>


Thursday, November 26, 2009

Calculating Percentage(%) in the ASP.Net Grid View Footer

Yet another scenario, calculating the percentage based on an common property. Say your Stored Procedure returns the ales and Expenses data pinned together, ORDER BY a particular field. Have a look at the below Grid view control. My stored procedure was returning only the Planned-Sales and Planned Expenses for each Division, for each year. M1, M2 M3...M12 represents the Months.
The data which the Stored Procedure returns will be a repeated with Planned-Sales, Planned-Expense for each Sales Division.
Bind your ASP>Net grid view control with data. Rest to be taken care at the Row Data Bound Event of the grid view control.
private void BindMainGrid()
{
string vBdgYear = "2010";
string vSalesDivision = "1";
string vYtdOrMtd = "YTD";

if (DrpBudgetYear.SelectedValue == "0")
{ vBdgYear = null; }
else { vBdgYear = DrpBudgetYear.SelectedValue; }
List oEntity = new List();
DashboardAnalyticsLogic oLogic = new DashboardAnalyticsLogic();
oEntity = oLogic.DashboardAnalytics(vBdgYear, vSalesDivision, vYtdOrMtd);

if (oEntity.Count != 0)
{
GrvMain.DataSource = oEntity;
GrvMain.DataBind();
}
else
{
LblMsg.Text = "No Record found";
}
}
Declare some public variables to calculate the percentage of Expenses against Sales. Go through the code snippet below, First IF condition checks for each data row, you can access each cell item values here with its index mentioned. Obviously, you would be knowing the number of coulmns of the grid view after bind. (I didnt get a chance where the same to be done with dynamicall colum growing grid ) . The second IF finds the footer row and puts data into each cells after the calculation, which would be available from the public variable.
See, first IF condition takes you in when it is a data row with data. (Header row and Footer rows cant get into this loop.)
System.Int64 TotalM1, TotalM2, TotalM3, TotalM4, TotalM5, TotalM6;
protected void GrvMain_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (TotalM1 == 0) { TotalM1 = Convert.ToInt64(DataBinder.Eval(e.Row.DataItem, "M1")); }
else { TotalM1 = (Convert.ToInt64(DataBinder.Eval(e.Row.DataItem, "M1")) * 100) / TotalM1; }

if (TotalM2 == 0) { TotalM2 = Convert.ToInt64(DataBinder.Eval(e.Row.DataItem, "M2")); }
else { TotalM2 = (Convert.ToInt64(DataBinder.Eval(e.Row.DataItem, "M2")) * 100) / TotalM2; }

if (TotalM3 == 0) { TotalM3 = Convert.ToInt64(DataBinder.Eval(e.Row.DataItem, "M3")); }
else { TotalM3 = (Convert.ToInt64(DataBinder.Eval(e.Row.DataItem, "M3")) * 100) / TotalM3; }

if (TotalM4 == 0) { TotalM4 = Convert.ToInt64(DataBinder.Eval(e.Row.DataItem, "M4")); }
else { TotalM4 = (Convert.ToInt64(DataBinder.Eval(e.Row.DataItem, "M4")) * 100) / TotalM4; }

if (TotalM5 == 0) { TotalM5 = Convert.ToInt64(DataBinder.Eval(e.Row.DataItem, "M5")); }
else { TotalM5 = (Convert.ToInt64(DataBinder.Eval(e.Row.DataItem, "M5")) * 100) / TotalM5; }

if (TotalM6 == 0) { TotalM6 = Convert.ToInt64(DataBinder.Eval(e.Row.DataItem, "M6")); }
else { TotalM6 = (Convert.ToInt64(DataBinder.Eval(e.Row.DataItem, "M6")) * 100) / TotalM6; }
}
else if (e.Row.RowType == DataControlRowType.Footer)
{
e.Row.Cells[0].Text = "Percentage";
e.Row.Cells[4].Text = Convert.ToString(TotalM1) + "%";
e.Row.Cells[5].Text = Convert.ToString(TotalM2) + "%";
e.Row.Cells[6].Text = Convert.ToString(TotalM3) + "%";
e.Row.Cells[7].Text = Convert.ToString(TotalM4) + "%";
e.Row.Cells[8].Text = Convert.ToString(TotalM5) + "%";
e.Row.Cells[9].Text = Convert.ToString(TotalM6) + "%";
e.Row.Cells[1].HorizontalAlign = e.Row.Cells[2].HorizontalAlign = HorizontalAlign.Right;
e.Row.Font.Bold = true;
e.Row.Height = 28;
}
Let me tell you what i did in the Row Data Bound. You agree that the public variables would have value zero at the first time check from the IF condition? If

Sunday, November 22, 2009

The Template Persistent Cache initialization & IIS Application Pool

A quite funny monday morning. Our network police, Mr.Pais rushed to the desk with his lappy and wooed, finding a strange issue with IIS application pool. Obvious, he wont stay cool until and unless finds a solution. The brainstomer had given a glimspe of the error message the Event Viewer had been throwing for the last whole week. He observed the issue was registering in the log every morning before the office hour starts. Here is the event viewr log.

Event Type: Error
Event Source: Active Server Pages
Event Category: None
Event ID: 5
Date: 14/05/2004
Time: 4:32:55 AM
User: N/A
Computer: 001DC001
Description:

Error: The Template Persistent Cache initialization failed for Application Pool 'DefaultAppPool' because of the following error: Could not create a Disk Cache Sub-directory for the Application Pool. The data may have additional error codes..

Googleoscoped the solution, suggested him the workaround.
A. Add the NT AUTHORITY\NETWORK SERVICE account to
C:\WINDOWS\Help\iisHelp\common with "Read and Execute," "List Folder Contents" and "Read".

B. Add the NT AUTHORITY\NETWORK SERVICE account to
C:\WINDOWS\system32\inetsrv\ASP Compiled Templates with Full Control.

C. Add the NT AUTHORITY\NETWORK SERVICE account to
C:\WINDOWS\IIS Temporary Compressed Files with Full Control.
Do an IIS Reset.
You know what, he never turned up saying the same. Issue must be solved. ;)

Tuesday, November 17, 2009

Using Temporary Table, SQL Server 2005

ALTER PROCEDURE dbo.BUDGET_COMMITMENT_QTY_REPORT
(
@BudgetYear NVARCHAR(50) = null,
@BudgetVersion NVARCHAR(50) = null,
@Company NVARCHAR(50) = null,
@Division NVARCHAR(50) = null,
@CostCenter NVARCHAR(50) = null,
@SubAccount NVARCHAR(50) = null,
@SalesDivision NVARCHAR(50) = null,
@AsOnDate DATETIME = null
)
AS
SET NOCOUNT ON
BEGIN

DECLARE @Select NVARCHAR(4000)
DECLARE @Param NVARCHAR(4000)

SET @Param = ' @BudgetYear NVARCHAR(50) = null,
@BudgetVersion NVARCHAR(50) = null,
@Company NVARCHAR(50) = null,
@Division NVARCHAR(50) = null,
@CostCenter NVARCHAR(50) = null,
@SubAccount NVARCHAR(50) = null,
@SalesDivision NVARCHAR(50) = null,
@AsOnDate DATETIME = null '
CREATE TABLE #TEMPTABLE
(
BudgetType NVARCHAR(50), BudgetYear NVARCHAR(50), Company NVARCHAR(50), Division NVARCHAR(50), SalesDivision NVARCHAR(50), Costcenter NVARCHAR(50), SubAccount NVARCHAR(50), Display NVARCHAR(50), Amount DECIMAL(29,2), Quantity DECIMAL(29,2), RQDate DateTime
)

INSERT INTO #TEMPTABLE

SELECT BUDGET_TYPE AS BudgetType, BUDGET_YEAR AS year, COMPANY_CODE AS Company, DIVISION_CODE AS Division,
SALES_DIVISION AS SalesDivision, COST_CENTER AS costCenter, SUB_ACCOUNT_CODE AS subAccount, 'Precommitment' AS Display, SUM(AMOUNT)
AS TotalValue, SUM(QUANTITY) AS Quantity, PR_REQ_DATE AS RQDate
FROM ECMS_PR_BUDGET_CHECK
GROUP BY BUDGET_TYPE, BUDGET_YEAR, COMPANY_CODE, SALES_DIVISION, COST_CENTER, SUB_ACCOUNT_CODE, QUANTITY, DIVISION_CODE,
PR_REQ_DATE

UNION ALL

SELECT BUDGET_TYPE AS BudgetType, BUDGET_YEAR AS BudgetYear, COMPANY_CODE AS Company, DIVISION_CODE AS Division,
SALES_DIVISION AS SalesDivision, COST_CENTER AS CostCenter, SUB_ACCOUNT_CODE AS SubAccount, 'Commitment' AS Display, SUM(AMOUNT)
AS TotalValue, SUM(QUANTITY) AS Quantity, PO_REQ_DATE AS RQDate
FROM ECMS_PO_BUDGET_CHECK
GROUP BY BUDGET_TYPE, BUDGET_YEAR, COMPANY_CODE, SALES_DIVISION, COST_CENTER, SUB_ACCOUNT_CODE, DIVISION_CODE, PO_REQ_DATE

--SELECT * FROM #TEMPTABLE

SET @Select = 'SELECT #TEMPTABLE.BudgetYear, #TEMPTABLE.Display, #TEMPTABLE.Amount,
#TEMPTABLE.Quantity, ECMS_MST_COMPANY.Company_Name, ECMS_MST_COST_CENTER.Cost_center_desc,
ECMS_MST_SUB_ACCOUNT.SUB_AC_NAME, ECMS_MST_SALES_DIVISION.SALES_DIVISION_NAME , ECMS_MST_DIVISION.DIVISION_NAME
FROM #TEMPTABLE
INNER JOIN
ECMS_MST_BUDGET_TYPE ON #TEMPTABLE.BudgetType = ECMS_MST_BUDGET_TYPE.BUDGET_CODE INNER JOIN
ECMS_MST_COMPANY ON #TEMPTABLE.Company = ECMS_MST_COMPANY.Company_Code INNER JOIN
ECMS_MST_COST_CENTER ON #TEMPTABLE.Costcenter = ECMS_MST_COST_CENTER.Cost_center_code INNER JOIN
ECMS_MST_SUB_ACCOUNT ON #TEMPTABLE.SubAccount = ECMS_MST_SUB_ACCOUNT.SUB_AC_CODE INNER JOIN
ECMS_MST_SALES_DIVISION ON #TEMPTABLE.SalesDivision = ECMS_MST_SALES_DIVISION.SALES_DIVISION_CODE INNER JOIN
ECMS_MST_DIVISION ON #TEMPTABLE.Division = ECMS_MST_DIVISION.DIVISION_CODE '


IF @BudgetYear<>0 AND @BudgetYear IS NOT NULL
SET @Select = @Select+ ' AND (BudgetYear=@BudgetYear) '

IF @BudgetYear IS NOT NULL
SET @Select = @Select+ ' AND (BudgetType=@BudgetVersion) '
IF @Company<>0 AND @Company IS NOT NULL
SET @Select = @Select+ ' AND (Company=@Company) '
IF @Division<>0 AND @Division IS NOT NULL
SET @Select = @Select+ ' AND (Division=@Division) '

IF @SalesDivision<>0 AND @SalesDivision IS NOT NULL
SET @Select = @Select+ ' AND (SalesDivision=@SalesDivision) '
IF @Costcenter<>0 AND @Costcenter IS NOT NULL
SET @Select = @Select+ ' AND (Costcenter=@Costcenter) '
IF @SubAccount<>0 AND @SubAccount IS NOT NULL
SET @Select = @Select+ ' AND (SubAccount=@SubAccount) '
IF @AsOnDate<>0 AND @AsOnDate IS NOT NULL
SET @Select = @Select+ ' AND (RQDate<=@AsOnDate) '

--print @Select

Execute sp_Executesql @Select, @Param , @BudgetYear, @BudgetVersion, @Company, @Division, @CostCenter, @SubAccount, @SalesDivision, @AsOnDate

END
SET NOCOUNT OFF
RETURN