Friday, August 7, 2009

Converting C# code to VB.Net

A very useful web page if you are working on either c# or vb.net and you are not familiar with any one of them.
You can use below page to convert your code from c# to vb.net and vb.net to c#.

http://www.developerfusion.com/tools/convert/csharp-to-vb/


Cheers,

Wednesday, July 1, 2009

Key cannot be null. Parameter name 'Key' error

You may face this error while setting up Custom Security extension or Forms Authentication with Sql Reproting Services. This is because two new enums added in FolderOperations and CatalogOperation.

You can add this permission for those enums this way:
m_FldOperNames.Add(FolderOperation.CreateModel, OperationNames.OperCreateModel)
m_CatOperNames.Add(CatalogOperation.ExecuteReportDefinition, OperationNames.ExecuteReportDefinition)

Adding this also means that you have to increase those hashtable counters like:
Private Const NrFldOperations As Integer = 10
Private Const NrCatOperations As Integer = 16

With this setting that error should disappear.

Cheers,

Tuesday, June 30, 2009

Error in Custom security extension for SQL Reporting services

If Custom security extension developed with vs.net 2003 is deployed to a report server having sql 2005 reporting services installed will throw an exception:
System.TypeLoadException: Method 'CheckAccess' in type '' from assembly '' does not have an implementation.

Obvious reason is :
IAuthorizaitonExtension has one overridable method CheckAccess with following signature
bool CheckAccess(string userName, IntPtr userToken,
byte[] secDesc, ModelItemOperation requiredOperation);

ModelItemOperation enum is new in sql 2005 reporting services which was not in earlier sql 2000.


Cheers,

Tuesday, June 23, 2009

Custom Functions/Code in SQL Reporting Service

There can be certain instances where we want to call our custom functions on certain Database fields or any other value.

We can write code in .Net class library and Integrate that assembly with our Reports to gain the functionality.

Here I am listing steps to do so:

1. Create a class library project in Visual Studio.
Add the function implementation (for example given below)


Public Class ResourceHelper

Public Function GetString(ByVal Key As String) As String

Return "Formatted Value is " & Key

End Function

End Class


2. Strong name the Assembly.

3. Build the Solution and Deploy this dll to following two locations:

  • Program Files\Microsoft Visual Studio 8\Common7\IDE\PrivateAssemblies
  • Program Files\Microsoft SQL Server\MSSQL.3\Reporting Services\ReportServer\bin
4. Click on Report in Designer area and navigate to "Report/Report Properties..." menu.

5. Go to References tab and add a reference to our .dll

6. Under Classes panel, Give Class name as . (here, ResourceTestClassLibrary.ResourceHelper) and Instance name as say objResource and Click OK. (This step is not required when we have function declaration 'static')

7. Add a TextBox to the report and Right Click and go to "Expression..."
Enter following text

=Code.myObj.GetString("Yahoo!!!")

and click OK.
Here we can pass any Database field value or other argument we wish.

8. Build Report Project and see if it succeeds.

On successful Run, you can see this text box containing the result output of that Custom function with that argument.

This is it.

Likewise, we can use Resource files to localize our labels and headers on report. Though deployment of .resx file will slightly wary in that case.


Cheers,


Wednesday, February 18, 2009

Exporting SSRS Report to Image file

Hi All,

Here I am putting a piece of code which exports SQL Server Reporting Service Report an Image file of type GIF. You can also export to other image file types as well.
Basically, we need to set DeviceInfo according to our requirement and pass that device info to Report's rendering extension mechanism.

string reportType = "Image";
string mimeType;
string encoding;
string fileNameExtension;

string deviceInfo = String.Format(@"
<DeviceInfo>
<OutputFormat>GIF</OutputFormat>
<PageWidth>{0}</PageWidth>
<PageHeight>{1}</PageHeight>
<MarginTop>{2}</MarginTop>
<MarginLeft>{3}</MarginLeft>
<MarginRight>{4}</MarginRight>
<MarginBottom>{5}</MarginBottom>
</DeviceInfo>",
"11in",
"8in",
"0in",
"0in",
"0in",
"0in"
);
Warning[] warnings;
string[] streams;
byte[] renderedBytes;

//Render the report
renderedBytes = reportViewer1.LocalReport.Render(
reportType,
deviceInfo,
out mimeType,
out encoding,
out fileNameExtension,
out streams,
out warnings);

string file = "C:\\MyImage" + "." + fileNameExtension;
Stream streamReportImage = new FileStream(file, FileMode.Create);
streamReportImage.Write(renderedBytes, 0, renderedBytes.Length);
streamReportImage.Position = 0;


Here you can see that, when we set OutPutFormat to GIF then, Render method returns us file extension accordingly which we can use the rendered bytes to output to a stream.

You can find more infromation about DeviceInfo for different output format on below link
Ref: http://msdn.microsoft.com/en-us/library/ms155397.aspx

Cheers,

Tuesday, December 9, 2008

Custom Verbs in your Webparts

Here I would shoud you how I added custom verbs in my custom webpart.

Override
WebPartVerbCollection
property in your webpart class and provide the following implementation

public override WebPartVerbCollection Verbs

{

get

{

WebPartVerb verb = new WebPartVerb("ExportToList", ExportToList); //New Verb

verb.Text = "Save Data to List"; //Text for Verb

WebPartVerb[] newVerbs = new WebPartVerb[] { verb }; //Add the verb into the array

//Add the array of verbs to the collection

WebPartVerbCollection verbs = new WebPartVerbCollection(base.Verbs, newVerbs);

return verbs;

}

}



Here First I am declaring new verb that we want, in the next line I am assigning Text to it.
Then I am declaring an array to contain this new verb and finally add this very to the base Verbs collection and return that collection.

When we declare verb, in the constructor second argument is the Handller event (e.g. ExportToList) which will be fired when this verb is clicked from your webpart.

We need to implement that Handler in our code and we can write actions which we want in that as given below:

public void ExportToList(object sender, WebPartEventArgs e)
{
// Add the logic to export this list
}


With above two implementations, when you deploy this webpart, you would see that verb in your webpart.

Cheers,


Steps for Setting up New Virtual Machine


- Install Virtual Server 2005 Software on the Host system
- Once installed, Open up Virtual Server Administrative Site from Programs
menu
- Create new Virtual Machine
1. Name it,
2. Set hardware config - RAM, Space, Network Adapters
- Insert Windows Server 2003 Installation Disk
O.S. Installation will proceed
- Keep on installing required Software on the virtual machine along with Service Packs needed.
- Setup the virtual machine to be in Domain

Note: Points to remember to Remote desktop to virtual machine
Control Panel > Add Hardware > Install the hardware that I manuall select from the list
From the list, Select Network Adapters, Next Select Microsoft as Manufacturer and from right select Loopback adapter. and proceed to Install.
Next go to Virtual Server Administrative site and configure your Virtual machine to add new Network adapter and restart the machine.

Now you should be able to login to the new virtual machine and start development.

Happy development..