Tuesday, June 26, 2007

Design time databinding to a control within an ItemTemplate

This is a kind of a hack, but comes in handy for certain situations such as assigning a data value to a control (i.e. ASP:HiddenField 'Value' attribute) contained within an ItemTemplate when binding in the code-behind is not feasible.

Value='<%# Eval("data_column_name") %>'

Tuesday, June 19, 2007

Setting ReportServerCredentials for ReportViewer

Here is a solution for a situation in which an instance of a ReportViewer control was on a different server and domain than the SQL 2005 Report Server. It also allows for limiting security to only a specific directory on the Report Server.

1) On the Report Server machine, create a local user account and assign the necessary permissions to interact with Reporting Services

2) In the Report Manager on the reporting server machine, assign the above account to a role of System User. Next, navigate to the directory containing the reports, to to the Properties tab, select Security and create a New Role Assignment using the account created in step #1.

3) Implement a public class that implements IReportServerCredentials as follows:

using System;
using System.Net;
using System.Security.Principal;
using System.Text;

using Microsoft.Reporting.WebForms;

namespace Common
{
public class ReportServerCredentials : IReportServerCredentials
{
private string reportServerUserName;
private string reportServerPassword;
private string reportServerDomain;

public ReportServerCredentials(string userName, string password, string domain)
{
reportServerUserName = userName;
reportServerPassword = password;
reportServerDomain = domain;
}

public WindowsIdentity ImpersonationUser
{
get
{
// Use default identity.
return null;
}
}

public ICredentials NetworkCredentials
{
get
{
// Use default identity.
return new NetworkCredential(reportServerUserName, reportServerPassword, reportServerDomain);
}
}

public void New(string userName, string password, string domain)
{
reportServerUserName = userName;
reportServerPassword = password;
reportServerDomain = domain;
}

public bool GetFormsCredentials(out Cookie authCookie, out string user, out string password, out string authority)
{
// Do not use forms credentials to authenticate.
authCookie = null;
user = null;
password = null;
authority = null;

return false;
}
}
}


4) In the code hosting the ReportViewerControl, assign the account credentials created in step #1 to the ReportServerCredentials property using the above class. It is recommended that the credentials string values be placed in a .config file with a public accessor class. For now, actual string values will be passsed for the illustration of this example:

ReportViewer1.ServerReport.ReportServerCredentials = new ReportServerCredentials("MaerskReports", "lCt!25!", "");

Monday, June 18, 2007

Converting a String value to a URI object instance

I came across a situation in .NET where I needed to programatically set the 'ReportServerUrl' property for a ReportViewer object in my C# code-behind. This was mostly due to the fact the application can be deployed on several different servers of which the I placed the URL to the Report Server in web.config.

So, I thought that I could simply implement:
ReportViewer1.ServerReport.ReportServerUrl =
http://servername/reportserver;

NOT!

I learned that ReportServerUrl needed to be assigned to a System.Uri object. So, I came up with the following function to convert a given string to a Uri object:

public Uri NavigateTo(string location)
{
return new Uri(location);
}


So, now all I need to do is:

1) Put URL into web.config
<add key="reportserver_url" value="http://localhost/reportserver"/>


2) Write a public interface to the configuration string

public static string GetReportServerUrl()
{
return AppConfig.ReportServerUrl;
}


3) Assign property in code-behind
ReportViewer1.ServerReport.ReportServerUrl = NavigateTo(AppConfig.GetReportServerUrl());

Thursday, June 14, 2007

Part 2 - Implementing PARSENAME to make use of the date range

Nuances about PARSENAME
* Evaluates from Right to Left
* Works with only a period as a delimiter


Code:
-- Note: @Week should be a stored proc input param
Declare @Week varchar(30)
Set @Week = '04/29/2007 to 05/05/2007'

Declare @NewDateRange varchar(30)
Declare @WeekBegin varchar(30)
Declare @WeekEnd varchar(30)


Set @NewDateRange = REPLACE(@Week, ' to ', '.')
Set @WeekBegin = PARSENAME(@NewDateRange, 2)
Set @WeekEnd = PARSENAME(@NewDateRange, 1)


SELECT @WeekBegin as BeginDate
SELECT @WeekEnd as EndDate


/* Results:
BeginDate = 04/29/2007
EndDate = 05/05/2007


Sources:
http://www.sqlteam.com/article/using-the-parsename-function-to-split-delimited-data
http://msdn2.microsoft.com/en-us/library/ms186862(SQL.90).aspx

SQL to create a list of weeks - Handy for reporting

Declare @StartDate DateTime
Declare @EndDate DateTime

Set @StartDate = '04/29/2007'
--Date of the sunday to show in the combo box.
Set @EndDate = '06/01/2008'; --Date used to as the stop point for the combo box. Does NOT get shown

With DateList( WeekCommencing ) as (
SELECT @StartDate WeekCommencing

UNION All

SELECT DateAdd( w, 7, WeekCommencing )

FROM DateList

WHERE DateAdd( w, 7, WeekCommencing ) < @EndDate ) SELECT Convert( VarChar(10), WeekCommencing, 101 ) + ' to ' + Convert( VarChar(10), DateAdd( d, 6, WeekCommencing ), 101 ) Week FROM DateList dl WHERE WeekCommencing <= GetDate()

Tuesday, June 12, 2007

FormatDate Function

I need a reminder of the parameters specifying which portions to display of a datetime value. Here is what I found on MSDN:

1 = Display a date and/or time. If there is a date part, display it as a short date. If there is a time part, display it as a long time. If present, both parts are displayed.

2 = Display a date using the long date format specified in your computer's regional settings.

3 = Display a date using the short date format specified in your computer's regional settings.

4 = Display a time using the 24-hour format (hh:mm).

http://msdn2.microsoft.com/en-us/library/8aebkz6s.aspx