Fixing Visual Studio’s System.OutOfMemoryException

Over the last little while Visual Studio 2005 has been crashing seemingly “randomly”. Sometimes it would happen when just trying to open a source file, other times when trying to debug a web app. A few co-workers asked if I wanted to try ReSharper, so I installed that. After installation crashing seemed much more frequent, so I went into Add-in Manager and disabled ReSharper.

My machine at work has 4GB of memory, and is running Windows XP. So I had assumed I had more than enough memory available for VS 2005 to use.

I had almost given up on fixing the issue, but yesterday came across the following blog post by Steven Harman titled Hacking Visual Studio to Use More Than 2Gigabytes of Memory.

Basically the solution was to boost the user-mode memory from 2GB to 3GB in XP via adding the /3GB to your BOOT.ini file, then making Visual Studio “Large Address” aware using the editbin command.

Paging the DataList

A few days ago I looked at various solutions to enhance an exhisting DataList control at work.

The DataList brings back a fair bit of data from several different tables and seemed a bit bloated.

The first solution I looked at was data paging and only showing rows as needed. The DataList doesn’t come with built in Paging capabilities, so you’ll need to write your own. Scott Guthrie has a great post about Efficient Data Paging with the DataList control.

Although that is a great solution, in this current environment of Ajaxifying everything you can get your fingers upon I found a great open source product named Ajax Data Controls (or ADC for short) and their DataList Paging example. You can download the controls from their CodePlex page.

The controls are basically an Ajaxified replacement for the exhisting ASP.NET 2.0 Data Controls (GridView/DataList/Repeater), with a few minor differences.

One of the authors, Kazi Manzur Rashid has a great blog post on how to use their GridView control.

A third solution is to use the PagedDataSource, which comes with ASP.NET 2.0. Sreejith Thathanattu has posted a good example of using the PagedDataSource on CodeProject with the DataList. If you decide to go down this path, consider assigning a Cached version of your DataSource to PagedDataSource as that will help with performance (you’ll be hitting the Database less).

Using Reports locally in ASP.NET 2.0

I was asked recently to develop reports for an internal ASP.NET app, after doing some research found that the best solution was to use the ASP.NET ReportViewer control set in LocalMode.

One of my big restrictions was the inability to upgrade from SQL Server 2000 to 2005, which meant avoiding I needed to avoid MS SQL Reporting Services. After much research into third party reporting components such as the Telerik Reporting Control as well as the SharpDevelop Reports, in the end I found a CodeProject article on Using the ASP.NET 2.0 ReportViewer in Local Mode which sounded perfect for my requirements.

Another requirement was to have the reports emailed out to respective contacts within the system, with PDF being the preferred format. My initial thought was to export to a temporary file on the system and attach it to the system generated email.

Found some code on the MSDN Forums about Printing Local Reports in ASP.NET which contained a useful looking ReportPrinterHelper class. Reading the code I noticed that it called the ReportViewer’s LocalReport.Render() function, and passed “PDF” as a parameter and also returned a byte[] array. An aha! moment struck, I could create a MemoryStream from the Byte[] array and attach that to an email as a PDF file.

Here’s my new RenderToPDFBytes function:

public static byte[] RenderToPDFBytes(ReportViewer reportViewer, ExportType exportType) 
{ 
  if (exportType == ExportType.Pdf) 
  { 
    Warning[] warnings; 
    string mimeType, encoding, fileNameExtension; 
    string[] streams; 
    byte[] pdfContent = reportViewer.LocalReport.Render("PDF", null, out mimeType, out encoding, out fileNameExtension, out streams, out warnings); 
    return pdfContent; 
  } 
  else if (exportType == ExportType.Xls) 
  { 
    // Implement if needed, just change PDF to XLS 
  } 
  return new byte[0]; 
}

Just place that into your ReportPrinterHelper class and reference it in your code as follows:

// Render our ReportViewer control to PDF format as bytes 
byte[] data = ReportPrinterHelper.RenderToPDFBytes(rptViewer1, ExportType.Pdf); 
// Transform our byte[] into a format suitable for attaching to an email. 
// Eg: Either in memory, or as a file on the system. 
MemoryStream ms = new MemoryStream(data); 
// Now to email it out as an attachment 
try 
{ 
  MailMessage mm = new MailMessage(); 
  mm.From = new MailAddress("noreply@example.com", "Ex Corp"); 
  mm.To.Add(new MailAddress("toaddress@example.com", "Bob Code")); 
  mm.Subject = "Daily Random Report"; 
  mm.Body = "Please find attached your Daily Report";    

  // Add our attachment to the MailMessage 
  mm.Attachments.Add(new Attachment(ms, "random.pdf", "application/pdf"));    

  // Set smtp.example.com to your relevant SMTP Host 
  SmtpClient smtp = new SmtpClient("smtp.example.com"); 
  smtp.Send(mm); 
} 
catch (System.Exception e) 
{ 
  // catch error and do something with it. 
}

Note: The ReportViewer control is only available as part of Visual Studio 2005 and is not a part of ASP.NET. As such, you’ll need Visual Studio installed to work with it.

IE7 “improvement” turns into a bug for us

This is an interesting one that I just came across today.

One of our users has downloaded the latest IE7 Beta release and has found that one of the features that she relies on no longer behaves the same way.

I got her to try to use that feature on another PC (which has IE6), and it worked.

The bug?

Our web app depends on the File upload Html Control, and it’s ability to grab the user selected file and directory. It then creates a link to that file (which is usually on a mapped network drive). Users can then click on the document link in their browser, and the document is opened in-browser.

It does seem a little bit of an overkill though to use the File upload control to simply create a link to a document.
But it worked fine at the time.

Here’s some C# code to illustrate what we had:


if (FileUplaoder.PostedFile.FileName != string.empty)
{
string fileName = FileUploader.PostedFile.FileName;
//...
}

In IE6, the fileName string would be something like: c:\docs\doc1.doc
Ie IE7 though, the fileName string would be: doc1.doc

Eric Lawrence, of the Internet Explorer team posted a response to this question on the IEBlog saying that it was a “by-design change for privacy reasons. IE7’s behavior matches that of other browsers”.

I’ll need to come up with a new solution to this problem. Although only 2 users of the system are using IE7, it’s better to come up with a solution to this problem than let it sit until everyone is about to upgrade to IE7.

The CodePlex

Just stumbled across this site called the CodePlex, via bink.nu

What is CodePlex?

CodePlex is an online software development environment for open and shared source developers to create, host and manage projects throughout the project lifecycle. It has been written from the ground up in C# using .NET 2.0 technology with Team Foundation Server on the back end. CodePlex is open to the public free of charge.

CodePlex includes the following features:

  • Release Management
  • Work Item Tracking
  • Source Code Dissemination
  • Wiki-based Project Team Communications
  • Project Forums
  • News Feed Aggregation

More…

Sounds like SourceForge and GotDotNet. Sounds interesting, oh, and it’s Microsoft site (and is currently in beta), so perhaps it is a GotDotNET replacement? (Just speculating here)

I’m sure we’ll be hearing a lot more about this sooner, rather than later. News spreads really fast in the blogosphere 🙂

Mobile Client Software Factory (aka Mobile Baseline Architecture Toolkit)

Just noticed that there’s a Compact Framework version of the Smart Client Baseline Architecture Toolkit called the Mobile Client Software Factory available now.

Francis K. Cheung has a series of posts on his blog about issues relating to porting to the Compact Framework.

On a side note, is anyone else heading to MEDC 2006 in Melbourne?

Edit: Looks like Nick Randolph has a series of posts on the Mobile Client Software Factory.

Only allow full synchronisations when a WindowsMobile device is cradled

When developing a Windows Mobile based application, you’re often presented with the “I want everything synchronised between my device and my PC” scenario.

While yes, it’d be great to have everything synchronised when you’re on the go (via GPRS, etc), and having little changes filtered through to the device.

The little changes is fine, but what about doing a full synchronisation? Surely, you wouldn’t want users to do a full sync whilst out on the road with data being charged per kilobyte (at a high rate)?

I’m not too sure how the data rates (for GPRS) are overseas, but over here depending upon your telco, it does get quite pricey after you go beyond your alloted monthly data allowance.

One way of disabling a full synchronisation whilst on GPRS is to use the SystemState.Changed event to detect when the device is cradled.

Here’s an example (using Windows Mobile 5.0’s managed API and C#):

public class Form1 : Form
{
Microsoft.WindowsMobile.Status.SystemState systemState;
public Form1()
{
systemState = new Microsoft.WindowsMobile.Status.SystemState(Microsoft.WindowsMobile.Status.SystemProperty.CradlePresent);
systemState.Changed += new Microsoft.WindowsMobile.Status.ChangeEventHandler(systemState_Changed);
}

protected void systemState_Changed(object sender, Microsoft.WindowsMobile.Status.ChangeEventArgs args)
{
// Show whether the cradle is present
MessageBox.Show("Is the cradle present? " + Microsoft.WindowsMobile.Status.SystemState.CradlePresent.ToString());
}
}

The above is just some sample code that you can modify to meet your requirements.

You could use the boolean value returned by SystemState.CradlePresent to toggle your full synchronisation option.

You should also check out the State and Notification Broker sample that comes with the Windows Mobile 5.0 SDK.

Why Compact Framework?

Most readers who’ve been following some of my past posts will know that my head is currently buried deep in the Compact Framework, and allowed to poke my head up on occassions to get back to the web stuff.

Some days I wonder around the web looking at .NET CF related articles (it’s all part of the learning process) and I generally get the impression that it’s a great area to develop in, but how do you convince everyone else?

The mobile phone is almost everywhere. It’s quite rare to walk into many places and not see someone with one. I remember while I was still at University that some people said they used more than one phone on a regular basis (they different phones on different networks). And I think as more cheaper MS Windows Mobile based devices come onto the market, the more of those people will be jumping onto that bandwagon. My guess is that the Windows Mobile based devices is a growing market, and is growing at an interesting rate. I don’t have figures on it, but it just seems to be. And there are definately a lot of new devices being released much more often.

Anyway, I came across this blog post by Mike Zintel (.NET CF Group Manager) tonight. First reaction? “Holy s*. They have .NET CF on an XBox now?”

Ok, I think more specifically it is something called the XNA Framework, which allows you to write games in managed code and run it on an XBox 360 and on Windows. Like SQL Server 2005, they are going to put a version of the CLR (Common Language Runtime) onto the XBox 360. (They have a March CTP available of the XNA Build. Seems like it is an “add on” for VS 2005.)

And suddenly you have this beast where .NET developers around the world who would like to write games in their own free time (if any) can do so easily. And who knows, maybe Microsoft will open up XBox Live a bit and allow these developers to sell their game(s). Yeah, I doubt the games will be to the level of the professional studio stuff. But it could potentially be something quite interesting. There’s definately a lot of .NET developers out there developing for various different platforms (Windows Mobile [Compact Framework .NET], Windows [.NET Framework] and for the Web [ASP.NET]).

Oh, and did anyone hear about that news on WPF/E (WPF/E == Windows Presentation Framework Everywhere. WPF was formerly known as Avalon, and to put it in even simpler terms, it allows you to build UIs using XML and then developers can handle the expected behaviour/interaction experiences in Managed code)? Some more specifics, and demos of it were shown recently at Mix 06. Sounds really promising. At Mix 06 they also said that Atlas has a go live license now.

Thinking In Mobile

One of the interesting issues with my transition from being a Web Developer (ASP.NET) to being a Windows Mobile Developer (CF.NET) is the issue of memory management.

We all know that the little devices don’t have nearly as much memory as our more basic computers sitting around the office.  So naturally, the memory management side of things will be slightly different.

Awhile ago I was able to consistantly produce an OutOfMemoryException on a form that retrieved about 30+ rows of data from a web service, and then insert it into a Sql Mobile 2005 DB.

Recently I came across a “Memory Problems FAQ” (written by CF.NET MVP Daniel Moth) which is dedicated to memory issues with regards to the Compact Framework. It’s a good read for those moving from the full Framework, to the reduced compact Framework. Oh, and if you’re going to develop for the Compact Framework, it’s always interesting to read about how the nuts and bolts work. Or in this case, how the CF.NET Garbage Collector works.

While i’m still Thinking in Mobile, I honestly believe the Microsoft Windows Mobile powered devices (Pocket PC Mobile Ed and Smart Phone) are getting better with each generation of both the OS, and the devices. And i’m sure it will keep on getting better.

And i’m sure they’ll get cheaper too. Right now there seems to be a new Windows Mobile powered device mentioned on E ngadget every month or so.  So my take on it would be, the more devices on the market, the more the prices will be driven downwards.

I think what we need to see more of though, is 3G capable devices. Especially in the Australian market, given that all four major carriers offer 3G services.

That, and the increased speed and bandwidth available through 3G services over GSM networks.

I know there are a few Windows Mobile devices that are capable of running on a 3G network, such as the i-mate JasJar.

There will come a day when the Telcos, either individually, or collectively just “turn off” the GSM network. Heck, the 3 network (Hutchison & Telecom) have stated that they plan to migrate people off their Orange CDMA network and onto the 3 service. There is not much point for Telcos to run more than one network if the other networks they run add up to a small percentage of their revenue.

I think, if you’re not developing for .NET CF devices today, you should seriously think about it. Mobile phones are everywhere today, and this is a boat you may want to jump aboard. Even if it’s just hacking together something for yourself and a few friends who also own Win Mobile devices. Hehe 😛

A few interesting Microsoft Windows Mobile Technical Articles

Just found a few Windows Mobile Technical articles in the Microsoft download center.

You can download them from here.

The articles touch on a few different areas including .NET CF apps using VS2005, introduction to the new Windows Mobile 5 Managed APIs. Mappoint Location Server, developing multithreaded apps using .NET CF 2. As well as a few others that touch on different areas of mobile development.

Oh, and do check out Rory’s TinyThings videos on Windows Mobile 5.0 and .NET CF 2.0.

My .NET CF 2.0 development has been going well, but I’m finding a lot of articles that are all about WM5/CF2. And not as much WM 2003/CF2. But that’s probably the nature of things at the moment. Everyone wants to target the latest and greatest. I’m not complaining though, at least I still get to utilise CF 2.0 in VS2005 (as opposed to CF 1.0/VS2003).

Guess that’s the reality of working life, as opposed to doing “theoretical work” (ie: stuff done in uni) where you can just target the latest and greatest, and as long as you can get it working in the emulator, it’s all fine. The other thing to remember is that a lot of the Telcos in Australia aren’t selling any WM5 devices (well, it’s not on our list for the plans we’ve selected).