Sunday, November 15, 2009

SQL Server Report Viewer Autosize

In Visual Studio, report viewer control for web is used control for displaying Microsoft SQL Server Reporting Services reports on an ASP.NET page. When the report viewer control is dropped on a web page, it will be displayed in the default size; width:400px and height:400px. That will not fit for most of the live reports.




We can set the width of the report viewer control to 100% or to a desired size, because the width of the report won't change dynamically. if the width is specified as 100%, that means the report viewer should fit to 100% of the browser width. If the report's width exceeds the browsers width, scroll bar will be displayed inside the report viewer control. Setting the height property to 100% will not work and we are not able to predict how many records will come on a single page (that is the required height of the report viewer control). So this will bring a horizontal scroll bar inside the report viewer control. This is not the expected behavior of a report page normally. If the report exceeds the width and height of the browser, the scroll bars should come inside the browser.



So what is the solution? Either report viewer should resize automatically to fit the content or we have to set the size of the report viewer dynamically.

Report Viewer control has a property FitToContent. The description of this property says it will expand/shrink according to the report content. However it will work only with another property AsyncRendering. This should be true.

When I worked with report viewer and experienced the same situation, I tried to use the above properties. Unfortunately a host of javscript errors prevented me to use these properties for my purpose. I searched all over the internet for a solution. But nothing solved my issue. So I started to investigate. I knew that writing some javascript to dynamically set the size of the report control is the solution to the problem. But how do I know the size of the report content? That was the big issue.

I examined the client side source of the page after the report is rendered. I scratched my head for some days to find the following information. Report viewer uses a number of iframes and divs to display reports. Report viewer itself is a div with id as same as the report viewer control. So setting the height & width to this div will set the size of the report. Again, reporting services report has two sections. The first one displays the input fields (parameters) and the view report button. The second part is the actual report. The next part of the investigation was to find the container of the report. It was not an easy task, because the report viewer control generates a lot of script on client side. This container is an iframe with id "ReportFrame" + ReportViewer1.ClientID. Here ReportViewer1 is the name of the report viewer control. But that was not the end of the research. This control again contained other containers. Finally I found that one div with id="oReportDiv" is the actual report container and it has the actual width and height. Then I had to establish the relation between the report frame and the report div. After hours of experiments the relation evolved as
div=reportFrame.document.getElementById("report").contentDocument.getElementById("oReportDiv").

Okay, now I have to implement this in the onload event. The resultant script was a very simple script as follows.

window.onload=function()
{
var viewer = document.getElementById("ReportViewer1");
var frame = document.getElementById("ReportFrame<%=ReportViewer1.ClientID %>");
if (frame != null && viewer != null)
{
var reportDiv = eval("ReportFrame<%=ReportViewer1.ClientID %>").document.getElementById("report").contentDocument.getElementById("oReportDiv");
viewer.style.height = reportDiv.scrollHeight;
viewer.style.width = reportDiv.scrollWidth;
}

}


And this solved my issue.

Saturday, July 4, 2009

Visual Studio 2010 - Overview

For the last one week I was working with the new release of Visual Studio & .NET framework - Visual Studio 2010.

Today creating presentation for the company to present the new features available in the new version of .NET and Visual Studio

Tuesday, February 5, 2008

Creating dynamic tables and text boxes inside using .NET with C#


I recently saw a post requesting help to create text boxes dynamically using C#. Besides the dynamic text boxes the requester needed another funcationality that these text boxes should be in a table. That is, each text box in a seperate cell. I left the post at that time and couldn't find where the question is.



Well, we can do that. How? To accomplish this task we have to know about the following properties.
1. The Controls collection of Control class.
2. The Add method of collections
3. The LiteralControl class.

The LiteralControl class allows us to create html tags that we normally put in the html view of the aspx page. It is almost similar to write the html tags directly to the page using Response.Write(). However we can't control the position of these html tags easily. LiteralControl can be treated as of any other web control and can be added to any of the container classes like Panel, PlaceHolder etc.

Ok, here we are going to use Panel, LiteralControl and TextBox server controls and generate the text boxes dynamically by taking a list.

Here is the code:

Create a web project in Visual Studio using C#. A web form will be added to the project by default (what ever the version of the Visual Studio be).

Drop a Panel on top of the web page. This is our container. Just give the name container. Switch to the code view.

First let us write a small function that determines the items in the dynamic list.
// This method can be anything that returns a string array
// Can be read from a configuration file or from database

private string[] GetItems()
{
string items = "Item1,Item2,Item3,Item1,Item2";
return items.Split(',');
}

Now we have to write the method that does the actual work

private string[] CreateDynamicControls() 
{
Container.Controls.Add(new LiteralControl("<TABLE border=1>"));
Container.Controls.Add(new LiteralControl("<TR>"));
string[] parameters = GetItems
foreach(string parameter in parameters)
{
Container.Controls.Add(new LiteralControl("<TD width=150px>"));
Label label = new Label();
label.ID = string.Format("{0}Label", parameter);
label.Text = string.Format("{0}: ", parameter);
Container.Controls.Add(label);
TextBox editBox = new TextBox();
editBox.ID = parameter;
Container.Controls.Add(editBox);
Container.Controls.Add(new LiteralControl("</TD>"));
}
Container.Controls.Add(new LiteralControl("</TR>"));
Container.Controls.Add(new LiteralControl("</TABLE>"));
}

Now we have to call this method from any of the Page events. The better place is OnInit, though we can successfully call this method from PageLoad.

Wednesday, December 5, 2007

String.Format - include { in output

I had to work with string formatting today for a number of time.
string.Format is a powerful mechanism to display strings in different format. It formats numeric values, date values and many other objects into different formats, different patterns.

I had to format a number based on the format given from the configuration file. It is just a simple case of left padding with zeros. The value in the configuration file will be like "######". Based on the length of this mask, I have to pad the resultant number. For example, if the mask is "######" and the number is 6, I should display the result as "000006".

It is a simple issue, if I use string concatenation connected to string.Format. So first I did the same in the following way.

string format = "{0:d" + mask.Length.ToString() + "}";
string formattedValue = string.Format(format, count);

But I felt that this is wrong. I don't use string concatenation. A performance issue. I have to use string.Format for the total formatting. So I tried many ways to format; but most of them resulted into run time error. For example,

string format = string.Format("{0:d{0}}", mask.Length);
string formattedValue = string.Format(format, count);

I understood that this happens because there I need "{" in the output of the first line. An escape sequence? I tried to put a "\" just before the "{". Now even the compiler didn't allow me to do so.

So what is next? After a moment of silence, I thought about putting an additional "{" where I require a "{" come as out put. So my code looked like this.

string format = string.Format("{{0:d{0}}}", mask.Length);
string formattedValue = string.Format(format, count);

To my happiness, that code worked.

Wednesday, October 24, 2007

Thread aborted while response.redirect. ASP.NET

Thread was being aborted - Response.Redirect or Server.Transfer Yesterday I was working with the maintenance of a project that was working fine. After the required changes in the code, my team mate did the unit testing. At that time she found that at some point the application throws an error message "Thread was being aborted". The same code was working fine earlier and we didn't expect that error there. She started to debug and found that the error is thrown from a Server.Transfer() call. However the code was there in the previous version and it was working. Our eyebows became more curved and forehead with wrinkles.
As I couldn't solve the problem in the first look, I decided to revert the code and test. Yes.. it is working. The main change what we had done was to take out the user interaction of that page and the required values came to this page as query string. I had a button clik event earlier. The code resided in that event is now (by the change) is called from the Page.Load event. I understood that the reason is that we are calling this method from the Page_Load(). But we have to do that.
So the next step is to do some googling. What I got is the information from Microsoft that this is by design. They suggested to use Server.Transfer("page", false) or Response.Execute(). But I was not ready to change my worked code. I started to do "comment and uncomment" debugging method to find tha actual villain. At last I found.

In my code Page_Load(), there was a line in the beginning "Session.Abandon()". I used this line of code to release the session when the user logged out from the application. When I commented that line my application worked with the the new code. I am happy, but I have to think about the reason behind it. I will tell later.

Wednesday, October 17, 2007

Introduction

I wonder all days, in the morning, mid-day, evening or in the night. What all the new information we gather all days! How many new faces, we see! Many kind of people we met! Even if it is an ordinary gray day. Yes! There are new... A lot of, a host of new....

I am working in the field of Information Technology. Life as a software engineer. Hectic, but enjoying.
One day I thought that I will write all the new information I get every day some where. Started to think long back. Years! And now comes my first blog. The introduction.