Friday 23 November 2012

Learning Silverlight

I recently had to take over a Silverlight application and had to remind myself how it hangs together. Here are some good blogs on the topic

How to blog Code

Here is a great blog on how to post code snippets.
  1. http://www.craftyfella.com/2010/01/syntax-highlighting-with-blogger-engine.html
  2. Copy and paste source into http://www.opinionatedgeek.com/dotnet/tools/htmlencode/encode.aspx
  3. encode the source then copy from above step and paste into blog
  4. Then wrap in tags

I was recommended a good Yum Cha in Sydney

Mr Chao Seafood Restaurant TEL:0295808003 Address:127-137 Forest Road Hurstville Nsw 2220, Hurstville, NSW, 2220

POS Hacked

I recently heard about a Point of Sale (POS) system that was hacked. The owner of the restaurant had to compensate the victims of credit card fraud. http://www.wired.com/threatlevel/2009/11/pos/

SSRS reports in new window

I recently received this request
On Thu, Nov 15, 2012 at 2:26 PM, Ajay wrote: Standard SSRS web interface, where displays the various reports. (from the page, where each reports have a menu which has options for Move, Delete, Subscribe etc.) When the link is clicked it navigates to the report Can this link be configured so that when clicked, it opens the report in a new window. We are using this SSRS page as Iframe in existing web page, and by default on clicking it opens in the iframe and is hard to read the report. Thanks Ajay

Heres the answer. If you are brave. Open Folder.aspx in MSSQLServerSSRSRootFolder\Reporting Services\ReportManager\Pages\Folder.aspx and add the following JQuery to the top of the file
<script type="text/javascript" src=//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script language = "Javascript">
$(function(){
    $('a').click(function() {
        $(this).attr('target', '_blank');
    });
});
</script>

How this works is that the Folder.aspx page contains the HTML that each report will be listed in when displaying the default SSRS folder window. The JQuery will add code to the click event on each hyperlink "a"nchor tag. The JQuery is telling the browser to open in a new window. Cameron __________________

Cannot Restart SQL Services

I recently received this request to restart the SQL Server Services Hi All I've a Local copy of SQL Server installed on my Computer ( SQL Server 2008 R2), it was all working fine up until below situation. I used local SSMS to connect to remote SQL server(SQL Server 2008 ) and performed some queries and closed the session. Then when I tried to connect to my Local SQL server using local SSMS, it pops up a message saying " Cannot Connect to localhost" A network specific or instance specific error occurred while establishing a connection to SQl Server. I observed under "SQL Server Configuration Manager" all services are stopped !! When I tried to re-start them it comes up with another error as " The request failed or service did not respond in a timely fashion. Consult the event log or other applicable error logs for details" Here's the answer Can you send the event log information? Have you tried a complete reboot? other issues to look at are login account username/pwd problem. I have also seen networking problems where the network trace route to the local host is not working. This could be a networking problem e.g. you have logged in to a vpn client or similar and it is still connected etc. Hi, Thanks, it looks like Logon issue, since my password has been changed after SQL server was installed on this machine, ??I Event Log says: "The SQL Server Analysis Services (MSSQLSERVER) service failed to start due to the following error: The service did not start due to a logon failure." Just like it says on the box. The answer is the event log. You just need to remember/know where to know.

ApplicationPoolIdentity in IIS 7.5

I found this fantastic article on ApplicationPoolIdentity in IIS 7.5 http://www.iis.net/learn/manage/configuring-security/application-pool-identities Very Good article on how the IIS7.5 service creates Virtual Accounts and Secures them

SQL Query to Calculate Surrender Amounts

I recently received a request to handle closing out repayment data in a SQL query as per below. (Answer at the end.) Hi All, I am working on a stored procedure which use to update a Table, I have come across a business request, which I need some assistance of developing suitable logic: Appreciate if someone can provide some advice on how to handle below situation in SQL please. My main question on this regard is how to distribute 'Surrender' ( Return Amount ) as per below Logic???? I have two tables,“Master Table” and “Detail_Table”, a stored Proc been used to populate “Detail_Table” based on records in“Master Table” Pseudo Code explanation: Read a record by record from “Master Table” and update "Detail Table" If ‘Transaction Type’ = ‘New’ then Put “Monthly_Net_premium” as Annual_premium/12 into “Detail_Table” NEXT #### My concern is how to handle 'Surrender' ## ### If ‘Transaction Type = “Surrender” then Update “Detail_Table” as below logic ( Which I have no idea how to do ))) Reverse Monthly values received from the policy, reversing from last premium received until it reaches Return Amount. (e.g. 52 + 52 + 52 + 44 as Return amount = 200) Master Table record containing Fin_id =216, is the record for 'Surrender', which has Annual Premium value(624) as well , and we know it distributed (i.e. Annual_premium/12 ) ** What is the methodology to use to apportion Return amount , compensating what had received until it reached return amount?? e.g. of processing first two records in "Master Table" shown in Tables Master Table Fin_id Policy_id Transaction Type Annual Premium Return Amount Start Date End Date 215 4402 New 624 - 1/Apr/2009 31/Mar/2010 216 4402 Surrender 624 (200) 1/Apr/2009 31/Mar/2010 Detail_Table Fin_Id Policy_ID Effective_Date Monthly_Net_Premium 215 4402 April-2009 52 215 4402 May-2009 52 215 4402 June-2009 52 215 4402 July-2009 52 215 4402 August-2009 52 215 4402 September-2009 52 215 4402 October-2009 52 215 4402 November-2009 52 215 4402 December-2009 52 215 4402 January-2010 52 215 4402 February-2010 52 215 4402 March-2010 52 216 4402 December -2009 (44) 216 4402 January-2010 (52) 216 4402 February-2010 (52) 216 4402 March-2010 (52) Part of stored procedure: USE [Mira_Test] GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO ALTER PROCEDURE [dbo].[UpdateDetail] AS BEGIN SET NOCOUNT ON; WITH c_Daily AS ( SELECT fin_id , policy_id , transaction_type , Return_Amount , Start_Date , End_Date , direct_debit_details_id , Surrender_Date , CASE WHEN transaction_type IN ( 'Surrender' ) THEN 0 -- I need Help Here ################# ELSE (Annual_premium) / 12 END AS monthly_net_premium FROM dbo.masterTable ) -- Now generate a row for each transaction day INSERT dbo.DetailTable ( FinID , Policy_id , Effective_Date , monthly_net_premium --- some joins etc... END; RETURN 0; Heres the answer Mira Here is some SQL that may help. I have put in an update statement for a single pass and also a slect statement, so you can test it. Basically, it works out the number of whole months that you need to take the premium out of the return amount then it works out the balance. It does it in reverse order based on the business rule. Update Detail_Table Set Monthly_Net_Premium = CASE WHEN transaction_type IN ( 'Surrender' ) THEN (case When DATEDIFF(month, Effective_Date, End_date) = (floor(-1*Return_amount / (Annual_Premium/12) )) then (-1*Return_amount - ((floor(-1*Return_amount / (Annual_premium/12))) * Annual_premium/12)) else ((Annual_premium) / 12) end) ELSE (Annual_premium) / 12 end from Detail_Table inner join Master_Table on Detail_Table.Policy_id = Master_Table.Policy_id and Detail_Table.Fin_id = Master_Table.Fin_id Select Detail_Table.Policy_id, Detail_Table.fin_id, Monthly_Net_Premium, transaction_type, DATEDIFF(month, Effective_Date, End_date), (floor(-1*Return_amount / (Annual_Premium/12))), DATEADD(month, -1*(floor(-1*Return_amount / (Annual_Premium/12) ))-1, End_date), Effective_Date, (-1*Return_amount - ((floor(-1*Return_amount / (Annual_premium/12))) * Annual_premium/12)), ((Annual_premium) / 12) from Detail_Table inner join Master_Table on Detail_Table.Policy_id = Master_Table.Policy_id and Detail_Table.Fin_id = Master_Table.Fin_id Regards Cameron

SQL basics to Expert

Depends on whether you want to be excellent at TSQL dev or SQL Server management. Assuming dev track first then. The progression I have below should take you from zero to hero pretty quick. Once you have done all of this material you will be a 1%er. Will take you about 1-2years to do everything suggested here. Do the books that lead to 70-433 certification. I did the dev track recently 70-433 and learnt alot about TSQL especially the new additions. The Self Paced Training kits from MS are excellent starting points MCTS Self-Paced Training Kit (Exam 70-433): Microsoft® SQL Server® 2008 Database Development - http://www.amazon.com/MCTS-Self-Paced-Training-Exam-70-433/dp/0735626391 Then there is the excellent SQL Server Internals to understand whats happening under the covers. Microsoft® SQL Server® 2008 Internals (Pro - Developer) - http://www.amazon.com/Microsoft%C2%AE-SQL-Server%C2%AE-2008-Internals/ Improving .NET Application Performance and Scalability - http://msdn.microsoft.com/en-us/library/ff649152.aspx especially Chapter 14 — Improving SQL Server Performance - http://msdn.microsoft.com/en-us/library/ff647793.aspx The book that really mad a difference for me a long time ago was this one. Not available anymore but superb - Microsoft SQL Server 2000 Performance Tuning Technical Reference - http://www.amazon.com/Microsoft-Server-Performance-Technical-Reference/dp/0735612706/ Read Paul Randells blog - phenomenal - http://sqlskills.com/blogs/paul/ Also get yourself a subscription to PluralSight, excellent material presented by good people. Well, there are various tutorials via MS (and a host of other places), there are quite a few samples in the online help as well… This page is archived but still has a list of pretty good links : http://archive.msdn.microsoft.com/SQLTutorials There is also a wide range of tutorials : http://msdn.microsoft.com/en-us/library/ms167593(v=sql.105).aspx (for example look at lesson 1 : http://msdn.microsoft.com/en-us/library/ms365325(v=sql.105).aspx ) If specifically with ASP.NET then there are some good tutorials and sample apps over at : http://www.asp.net/mvc

Monday 12 November 2012

SSRS Automatically View Report after selecting drop down parameter value

In SQL Server Reporting Services (SSRS), I had a client who wanted to automatically rerun their report after selecting a value in a drop down without having to click on the "View Report" button. This can be done using JQuery and below is how it is done. 1.) Go to the "Report.aspx" page in the ReportManager directory. On My machine it is here. C:\Program Files\Microsoft SQL Server\MSRS10_50.MSSQLSERVER\Reporting Services\ReportManager\Pages\Report.aspx" 2) At the top of the file after the "Page" directive in asp.net add the following script.
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
<script language = "Javascript">

 $(function(){
 $('body').on('change', 'select', function () {
 $('input[value="View Report"]').click();
 });
});
</script>
3) Now every time you click on the value in the drop box it will automatically postback and refresh the form with your selected parameter.

Postscript
After various feedback, people have asked for how to make the report refresh only on a particular control. This can be done with jQuery. Naturally with jQuery there are lots of ways of achieving this, so I have opted for the simplest. The short answer is here is the code below.
<%@ Register TagPrefix="MSRS" Namespace="Microsoft.ReportingServices.UI" Assembly="ReportingServicesWebUserInterface" %>

<%@ Page Language="c#" CodeBehind="Report.aspx.cs" AutoEventWireup="false" Inherits="Microsoft.ReportingServices.UI.ReportWrapperPage" EnableEventValidation="false" %>

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
<script type="text/javascript">
    $(function () {
        $('body').on('change', 'select', function () {
            var currentControl = this
            //Need to add checks here that we have selected values etc.
            var allBoxes = $('select');
            var fire = true
            allBoxes.each(function () {
                if (this.value == "0") {
                    fire = false;
                }
            });
            if (fire == true) {
                var relevantControlPrompt = 'Asset Type';
                var relaventspanText = $('label[for="' + currentControl.id + '"]').children("span").html();
                if (relaventspanText == relevantControlPrompt) {
                    fire = true;
                } else {
                    fire = false;
                }
            }
            if (fire == true) {
                $('input[value="View Report"]').click();
            }
        });
    });
</script>

How does it work? It relies on the way that SSRS renders the report page. SSRS programmatically generates the controls on the form. We don't know the name/id of the controls before we start looking at the HTML that is generated. However Microsoft kindly took advantage of the HTML "for" attribute which links form prompt labels with their relevant input control. So in jQuery I can take advantage of this.
SSRS a) Creates a list of controls with prompts stored in labels. b) the labels are flagged with "for" with a pointer to the relevant select drop down list box. c) using jQuery we can now find out if the current Select is the one that we want to watch. In the example below, we have chosen to look at the "Asset Type" drop down. ...snippet from outputted html by SSRS
<label for="ctl32_ctl04_ctl06_ddValue"><span>Asset Type</span></label>
the relevant select looks like this
<select name="ctl32$ctl04$ctl06$ddValue" id="ctl32_ctl04_ctl06_ddValue" style="width: 182px;">
So the label has a "for" attribute "ctl32_ctl04_ctl06_ddValue" which points to the id of the relevant select. The code will run for every select on the form but will only refresh the report for the chosen select. The selector uses the jquery syntax to search for a label with the for attribute = to my control id. I then get the child span and then get its html. This should then say "Asset Type" or whatever is the relevant select for your requirement.
var relaventspanText = $('label[for="' + currentControl.id + '"]').children("span").html();
Note: The javascript also includes logic to suppress refreshing until all values have been selected, if that is required.