Friday, October 15, 2010

Create a PDF and attach it to an email message without saving it to disk

Okay, I must admit it -- I'm pretty proud of this one. This is cool!

The goal: Use WebSupergoo's ABCpdf .NET 7.0 to create a PDF file on the fly. Without saving the file to disk, attach it to an email message, and send the email.

Prerequisites:
  • Create a .NET 3.5 C# web site.

  • Configure the mailSettings section in web.config to point to your SMTP server. Don't have an SMTP server? See this handy tip.

  • Add a button to a form on your website. Place the code below in the button's Click event handler.

  • That's it! But if you're on 64-bit Windows and ABCpdf.NET is throwing an exception, try this.

Here's the code:

// Create a mail message, setting the "from" address, "to" address, subject and body
SmtpClient smptClient = new SmtpClient();
MailMessage mailMessage = new MailMessage();
mailMessage.From = new MailAddress("ssaporta@localupsolutions.com");
mailMessage.To.Add(new MailAddress("ssaporta@localupsolutions.com"));
mailMessage.Subject = "Test";
mailMessage.Body = "This is a test.";

// Use WebSupergoo's ABCpdf to create a PDF document
Doc theDoc = new Doc();
theDoc.AddHtml("<html><body>The time is now <b>" + DateTime.Now.ToString() + "</b> text.</body></html>");

// Save the document to a memory stream (rather than to a file on disk)
MemoryStream ms = new MemoryStream();
theDoc.Save(ms);

// Important: return to the beginning of the stream
ms.Position = 0;

// Set the MIME type and filename for the attachment
ContentType ct = new ContentType();
ct.MediaType = "application/pdf";
ct.Name = "mydoc" + DateTime.Now.ToString() + ".pdf";

// Create the attachment from the stream
Attachment attachment = new Attachment(ms, ct);
mailMessage.Attachments.Add(attachment);

// Send the email
smptClient.Send(mailMessage);

// Clean up
ms.Close();
theDoc.Clear();

Label1.Text = "Done";

Thursday, October 14, 2010

Cause of exception "Index and length must refer to a location within the string"

Every once in a while, my .NET 2.0 website would throw this exception: "Index and length must refer to a location within the string." I knew this because I could see the exception in the Windows application event log, but the log didn't give a line number or any other information that allowed me to narrow it down to less than a few hundred lines of code.

By adding a bunch of logging -- trapping exceptions with lots of try/catch blocks and writing them to disk -- I found the problem. This exception can occur when you call the .NET String.Substring() method and the string's length is not long enough.

Something like this would cause it:
string s = "abc";
string s2 = s.Substring(1,5);

In my case, I was parsing a credit card expiration date on the assumption that it would always be formatted as mm/yyyy, but it turned out sometimes the month was a single digit, as in 6/2013 rather than 06/2013.

How to test sending email on Windows 7

When you install IIS on Windows 7, it doesn't include an SMTP mail server. That's different than Windows XP, and is inconvenient when you're developing -- and would like to test -- a .NET application that sends email.

Of course you could install and configure a third-party SMTP server, and there are some free ones available. But I found a simpler alternative here.

In brief, put this in your web.config file:

<smtp deliverymethod="SpecifiedPickupDirectory">
<specifiedpickupdirectory pickupdirectorylocation="c:\Temp\">
</smtp>

In your .NET application, instantiate SmtpClient using the constructor that takes no arguments; this tells SmtpClient to use the SMTP settings in web.config. Then, when your application calls SmtpClient.Send(), an .EML file will be created in c:\temp. I was able to double-click the .EML file, and it opened in Windows Live Email.

Wednesday, October 13, 2010

Visual Studio debugger may run 32-bit on 64-bit Windows

I discovered something while testing a component called ABCpdf from WebSupergoo. I have 64-bit Windows 7 Home Premium, so I donwloaded the 64-bit version of ACBpdf. I created a .NET 3.5 web site and attempted to instantiate the component like this:

WebSupergoo.ABCpdf7.Doc doc = new WebSupergoo.ABCpdf7.Doc();

When I pressed F5 in Visual Studio 2008 to start debugging, a runtime error resulted:

The type initializer for 'WebSupergoo.ABCpdf7.Internal.ManagedInvoke' threw an exception.

I found the explanation on WebSupergoo's support site (although they describe a different symptom: "I see the exception "Unable to load DLL 'ABCpdfCE7.dll': The specified module could not be found.")

It turns out that Visual Studio's debugger may run as a 32-bit application, even on 64-bit Windows.

By creating an IIS 7 website that points to my source code folder, and running it that way, it ran 64-bit, and the problem was solved.

Interestingly, when I created a Windows Forms application and ran that using the Visual Studio debugger, that worked without error as well.

WebSupergoo suggests a good trick for determining whether your .NET website or appllication is running 32-bit: Output the value of IntPtr.Size. If it is 4, rather than 8, your application is running in a 32-bit process.

Thursday, October 7, 2010

Calling a method in the parent page upon clicking a button in a .NET user control

Lots of people probably know this already, but it was new to me.

Suppose you have a button (or other control) in an ASP.NET user control, and when the button is clicked, you'd like to call a method in the containing page, so that the page can take some appropriate action.

One way to do this is with InvokeMethod. You can even pass a value from the user control to the parent page.

A tip of the hat to Rajshree Mittal for this post that pointed the way.

My example consists of four files: Default.aspx, MyUserControl.ascx, Default.aspx.cs, and MyUserControl.ascx.cs. The two .CS files are provided here.

Default.aspx.cs

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = "before";
}

public void DisplayMessage(string message)
{
Label1.Text = message;
}
}


MyUserControl.aspx.cs

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class MyUserControl : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void btnOK_Click(object sender, EventArgs e)
{
Page.GetType().InvokeMember("DisplayMessage", System.Reflection.BindingFlags.InvokeMethod, null, Page, new object[] { TextBox1.Text });
}
}

Wednesday, October 6, 2010

Parsing a comma-separated list of values into a table in SQL

You might want to select records that match any of several values. For example, you might want to get all restaurants where restaurantID is 50, 51 or 60. That's no problem in a T-SQL select statement: select * from restaurants where restaurantID in (50,51,60).

However, suppose you'd like to pass the list of restaurant IDs as a parameter to a stored procedure. Let's suppose the stored proc accepts an nvarchar parameter named @restaurantID. In our example, @restaurantID = '50,51,60'.

T-SQL does not allow something like this: select * from restaurants where restaurantID in @restaurantID.

You must get the list of restaurant IDs into a temporary table and do something like this:

declare @r table (
restaurantid int
)
-- parse comma-separated list into table here...
select * from restaurants where restaurantid in (select restaurantid from @r)


So, how do you parse the comma-separate list into a table? Like this:

declare @index int
declare @leftPart varchar(255)
while len(@restaurantidlist) > 0
begin
set @index = charindex(',', @restaurantidlist)
if @index = 0
begin
set @index = len(@restaurantidlist) + 1
end
set @leftPart = substring(@restaurantidlist, 0, @index)
set @restaurantidlist = substring(@restaurantidlist, @index + 1, len(@restaurantidlist))
insert into @r (restaurantid) values (@leftPart)
end

Tuesday, October 5, 2010

ReportViewer quirks with browsers other than IE

I'm using the .NET ReportViewer control to display report output. Today I found this control fully supports Internet Explorer, but has some quirks when used with other browsers.

Microsoft has documented this here. Also a shout-out to this post on stackoverflow that set me on the right track.

I've examined several aspects of ReportViewer's behavior in four browsers, all running on Windows:
- Internet Explorer 8
- Firefox 3.6.10
- Google Chrome 6.0.472.63
- Safari 5.0.2

Here's a rundown of the quirks I encountered...

1. You can set the width of ReportViewer's output by setting its ZoomMode and/or ZoomPercent properties.
- Works perfectly in IE.
- Totally ignored by FF. Output is always displayed at 100%.
- Works perfectly in Chrome.
- Mostly works in Safari, but width is greater than it should be.

2. The ReportViewer control is supposed to display a dropdown list so you can change the width of the output interactively.
- Works perfectly in IE.
- Not displayed in FF, Chrome or Safari.

3. The ReportViewer control displays several controls at the top of the report for pagination, exporting the report, etc. The way these controls are laid out varies by browser.
- In IE, they're left-justified on a single line.
- Ditto in FF.
- In Chrome, they're bunched up vertically in a narrow column.
- In Safari, they're similarly bunched up.

Sunday, October 3, 2010

Welcome to ProgBlog

I've been the CTO of LocalUp Solutions, developing web-based software for the restaurant industy, since February, 2010. It's a small company, so I have the privilege and challenge of solving most technical problems myself. When I come across a software engineering problem or solution that I find interesting, I'll post it here on ProgBlog. If you find something here to be intriguing or useful, please join the conversation.