Efficient Crystal Reports.NET databinding.


Crystal Reports.NET. The mere mention of the word “Crystal” makes most .NET developers shudder. I hope this post can help others with one of the many pitfalls I’ve experienced while working with Crystal Reports.NET.

This post covers Crystal Reports.NET v11 (the version that comes with Visual Studio.NET 2008), and ASP.NET. The Crystal Report Viewer control in this post is manually databound to a strongly typed DataSet in the code behind.

An issue I ran into recently was the problem of navigating through the Crystal Report pages with the viewer's built-in paging controls. When I would land on report page 2, and then attempted to sequence forward to report page 3, the report would go to page 1. This problem was caused by improper databinding of the Crystal Report Viewer control.



I first attempted to handle the server side paging event in the Crystal Report Viewer control, but to no surprise… that event is not exposed by the control. After some further investigation, I discovered that I needed to do my report binding through the Init event of the ASP.NET page.

My report needed to accept a custom parameter, supplied by a DropDownList in the page.

<asp:DropDownList ID="objDropDown" runat="server">
<asp:ListItem Value="0" Text="Parameter 0"></asp:ListItem>
<asp:ListItem Value="1" Text="Parameter 1"></asp:ListItem>
<asp:ListItem Value="2" Text="Parameter 2"></asp:ListItem>
</asp:DropDownList>

<asp:Button runat="server" Text="Run Report" />

<CR:CrystalReportViewer id="objCrystalReportsViewer" runat="server"/>

The problem I ran into was that I could not get the value from the DropDownList on PostBack because I could not access its ViewState. In an ASP.NET’s Init Event, ViewState is not accessible. Here’s how I solved my problem…

Instead of trying to get my report parameters from ViewState on PostBack, I used the Request object (brought back memories of the old classic ASP days). My OnInit code ended up looking like this:

Protected Overrides Sub OnInit(ByVal e As System.EventArgs)

    MyBase.OnInit(e)

    If (Request.Form.Keys.Count > 0) Then

        Dim oFormValues As NameValueCollection = Request.Form
        Dim iParameter As Integer = -1

        If (oFormValues(oDropDown.UniqueID) IsNot Nothing) Then
            Integer.TryParse(oFormValues(oDropDown.UniqueID), _
                             iParameter)
        End If

        Dim oReport As ReportDocument = New ReportDocument()
        oReport.Load(Server.MapPath("MyReport.rpt"))
        oReport.SetDataSource(New MyDataSet(iParameter))
        objCrystalReportsViewer.ReportSource = oReport
        objCrystalReportsViewer.DataBind()

    End If

End Sub

Not only did that solution solve that problem, but it also gave me a great method for future development for manually databinding Crystal Reports.NET viewer controls to custom parameters.

Tags: ,
Categories: Crystal Reports.NET | ASP.NET

Permalink E-mail | Kick it! | DZone it! | del.icio.us Comments (18) Post RSSRSS comment feed

Comments

6/24/2010 4:48:42 PM #

Infrastructure Management Services

I think you should try to get you report parameters through request object, I am sure it will solve your navigation problem and will make it easy to databind Crystal Reports.NET viewer controls to custom parameters.

Infrastructure Management Services United States

6/29/2010 7:32:13 PM #

V.R.Hari

Thanks for the insight into the Crystal Reports. Instead of Using request object i used a session variable for the dropdownlist and it works just as good.

V.R.Hari United States

6/29/2010 7:32:50 PM #

IT Services

I was so frustrated by the navigation problem of navigating through the Crystal Report pages , every time I tried going to the third page from the second, I went back to the first, at one point of time I was ready to through my system.

IT Services United States

8/18/2010 2:03:18 AM #

sWill

Interesting ... as always - is your blog making any cash advance ? ;)

sWill United States

8/19/2010 1:53:09 AM #

букмекерские конторы

Thanks for share. Great post. You\'re the best.

букмекерские конторы United States

8/19/2010 6:46:15 PM #

wow gold

Thoughtful insight and ideas. You have clearly spent some time on this. Well done!<br />http://www.visongold.com/

wow gold Spain

8/19/2010 7:25:35 PM #

Wow Money

I recently came across your blog and have been reading along. I thought I would leave my first comment. I don��t know what to say except that I have enjoyed reading. Nice blog. I will keep visiting this blog very often.<br />http://www.money4wow.com

Wow Money Israel

8/19/2010 8:25:55 PM #

buy wow gold

Hands down, Apple's app store wins by a mile. It's a huge selection of all sorts of apps vs a rather sad selection of a handful for Zune. Microsoft has plans, especially in the realm of games, but I'm not sure I'd want to bet on the future if this aspect is important to you. The iPod is a much better choice in that case.<br />http://www.wowgold-europe.com/

buy wow gold Zimbabwe

8/20/2010 9:37:22 PM #

aion story

Really nice read thanks, I have added this to my Mixx bookmarks<br />http://www.peterhero.com

aion story Indonesia

8/20/2010 10:23:06 PM #

wow gold

Valuable information and excellent design you got here! I would like to thank you for sharing your thoughts and time into the stuff you post!! Thumbs up<br />http://www.wowgold-europe.com/

wow gold Austria

8/28/2010 4:27:05 PM #

psp news

Great stuff from you, man.  Ive read your stuff before and youre just too awesome.  I love what youve got here, love what youre saying and the way you say it.  You make it entertaining and you still manage to keep it smart.  I cant wait to read more from you.  This is really a great blog.<br />http://www.vivopsp.com

psp news Bahrain

8/28/2010 6:20:48 PM #

Cheap necklace

Excellent information here. This blog post made me smile. Maybe if you put in a couple of pictures it will make the whole thing more interesting.<br />http://www.visonnecklace.com/

Cheap necklace Republic of the Philippines

8/31/2010 8:36:42 AM #

tag alter ego

I have get a lot of useful information and fresh knowledge in your website.

tag alter ego United States

8/31/2010 8:36:54 AM #

tank mens watch

Sounds great!

tank mens watch United States

9/5/2010 2:01:07 PM #

game store

This is a really good read for me. Must admit that you are one of the best bloggers I ever saw. Thanks for posting this useful article.<br />http://www.karile.com

game store Bolivia

9/5/2010 3:08:58 PM #

buy wow account

I thought it was going to be some boring old post, but it really compensated for my time. I will post a link to this page on my blog. I am sure my visitors will find that very useful<br />http://www.accountofwow.com/

buy wow account Albania

9/6/2010 8:06:05 AM #

motorola h3 bluetooth

This is a great post; it was very informative. I look forward in reading more of your work. Also, I made sure to bookmark your website so I can come back later. I enjoyed every moment of reading it

motorola h3 bluetooth United States

9/6/2010 8:06:22 AM #

motorola h700 bluetooth headset

It is certainly a subject that is near to me so Im delighted that you choose to authored about this. 

motorola h700 bluetooth headset United States

Add comment


(Will show your Gravatar icon)
  Country flag

biuquote
  • Comment
  • Preview
Loading