Wednesday, May 19, 2010

DataServiceContext IgnoreResourceNotFoundException

Getting a method not found error on your System.Data.Services.Client DataServiceContext when setting IgnoreResourceNotFoundException = true? You may need to upgrade your ADO.NET Data Services.

Wednesday, October 25, 2006

Handling null values when binding to ASP.NET 2.0 DropDownList Controls

Frequently when using 2 way data binding on data controls such as the FormView or DetailsView, DropDownList boxes are used to allow users to select pre-populated values.

Often in this situation the data item the DropDownList control is being bound to contains a null value. In these situations the ASP.NET compiler will throw an exception (SelectedValue property is set to a value not in its collection of list items).

To solve this issue it is necessary to add in a ListItem with an empty value to the DropDownList. To do this add the following line to your DropDownList making sure you also set the property of the AppendDataBoundItems to "true" so that it will be appended to the list:

<asp:dropdownlist AppendDataBoundItems="true">
<asp:listitem value="" text="Select a value"></asp:listitem>

To make your application more userfriendly it would be nice to programmatically show the null value ListItem when your DropDownList has not been bound to a value. To do this we add an
OnDataBound event to the DropDownList control and check to see if it has a selected value. If it doesn't, then we want to show the null value ListItem. Make sure you set the value of Enabled to false on the ListItem so it doesn't appear.

Add the OnBoundEvent to the control:
<asp:dropdownlist id="MyDropDownList" runat="server" OnDataBound="AddNullValueToDropDownList_DataBound" AppendDataBoundItems="true">
<asp:listitem value="" text="Select a value" Enabled="false"></asp:listitem>

Create the event in the code behind:
protected void AddNullValueToDropDownList_DataBound(object sender, EventArgs e)
DropDownList dropdownlist = (DropDownList)sender;
if (dropdownlist.SelectedValue == string.Empty)
dropdownlist.Items[0].Enabled = true;

That's it! Now you can easily databind a DropDownList to your favourite data control with null values.

Wednesday, October 12, 2005

Reporting in Dot Net

Intended audience: Dot Net Developers and IT Managers

1.0 Reporting
Reporting can be a tricky and expensive exercise, especially if you are wanting to publish reports over the internet. There are many reporting options and tools available that can range from $1000 to $15,000 for something like Crystal Reports or SQL Server Reporting Services. Most of these options often requiring you to have a PhD in law to just understand the licensing requirements.

Fortunately there is a better way, a standard way. Using XML as your data source and an XSL stylesheet to define the design of your report, you can parse the XML and XSL files with an XSLT parser to generate your reporting output file as, for example an HTML email, a PDF download link or a Word/RTF document attachment. The beauty of using XML ( and XSLT ( is it's an industry standard. That means you're not locked into using proprietary software and being an industry standard you should be able to get any developer off the street to pick up where your last developer left off. Potentially saving your company a lot of money.

1.1 Common Report Output Types
Most developers are asked to produce 3 common reports; HTML, MS-Word (RTF - Rich Text Format) and PDF each often requiring different applications to design and render the reports. For example one might use Visual Studio to design a HTML report, Crystal Reports to produce a PDF or SQL Server Reporting Services to produce a Word/RTF document.

Using XSL you can design a stylesheet that renders your output as XSL-HTML to produce HTML style reports, XSL-RTF to produce Word style reports or XSL-FO to produce PDF style reports. Alternatively you can use Altova StyleVision to visually design your report and automatically render all 3 reporting output styles(XSL-HTML, XSL-RTF and XSL-FO for PDF's) at once. You can download it for free on a 1 month trial. Unfortunately with the free version you can't produce XSL-FO output for PDF's, but if you're just after HTML or RTF reporting then the free version will work for you.

... This document is still a work in progress

1.2 Rendering your reports
-XSLTransformations (XSL-HTML and XSL-RTF)
-NFop (XSL-FO transformation to PDF)

1.3 Sample Code

2.0 XML Data Sources
-File, SQL Server

2.1 XML and SQL Server

2.2 XML For Auto (tree)

2.3 Sample database Stored Proc

3.0 Altova XMLSpy and StyleVision


3.2 Sample XML, XSD, SPS, XSL-HTML, XSL-FO files and C# rendering code

4.0 Conclusion