Monday 10 September 2007

Reusing command parameters

A while ago, at that hobby I practise during the day (some call it work. I resent that. Work is what I do when I come home), I was writing a piece of VB.NET code (I know... I know...) that would fire off a bunch of stored procedures, all with the exact same parameters.

So I wondered if I really had to define the parameters once for every time I called the procedures, or whether I could actually just change the CommandText.

I created a little test app, and went away. Today, I rewrote the code to C#, and decided to put it up here, in order to show that reusing command parameters is perfectly legal in .NET (and to me, it actually sounds logical too, as it prevents the coder from having to rewrite the same code over and over again).


I created a page with one single button, and two labels (I left their names unchanged, as I was doing this during worktime, and I wanted to have the test take as little time as possible.

I added the code, with a few comments thrown in for clarity, and used the labels to post the ID's of the first records I got back from my stored procedures.

Next I fired off the code, while keeping an eye on my profiler, and sure enough, both the stored procedures were executed. I then changed the code a little and increased the value for the second stored procedure, in order to see if that would still work. It did:



Click here to download the sample project and test it for yourself. The project assumes you have the Adventureworks database installed. you will have to create the two stored procedures usp_Test1, and usp_Test2 as well (they're also included in the ZIP file).


Read more!

Wednesday 5 September 2007

I won something...

After I finished up my CRUD script, I submitted it to a competition I stumbled upon while reading up at www.sql-server-performance.com. Mostly in the hope of trying to gain some attention for the script. Besides, when do I ever win anything?

Well, this time I did! I just received an E-mail stating I won a full-conference pass to the PASS summit in Denver, Colorado.

I won't be able to attend, but I definitely am proud at the moment :)


Read more!

CreateUserWizard - SendingMail event

After following the examples in the book, I figured I wanted to add some extra functionality to the Membership and UserProfiling section.

While it allows your users to signup smoothly and simply, using a CreateUserWizard, I wanted to add an option where my site sends the user a link with a GUID embedded in it, in order to verify that the user didn't only fill in a properly formatted E-mail address, but actually does own the mail address that was entered.

I went off on my merry way, using a code snippet from my local installation of MSDN as a basis:



e.Message.Body.Replace("<%PasswordQuestion%>", Createuserwizard1.Question);
e.Message.Body.Replace("<%PasswordAnswer%>", Createuserwizard1.Answer);


So my code became:



protected void SendMail(object sender, MailMessageEventArgs e)
{
// Generate a GUID
string guidResult = System.Guid.NewGuid().ToString();

// Code to paste into a URL, which I cannot paste cause Blogger throws a fit.

e.Message.IsBodyHtml = true;
e.Message.Body.Replace("<%Link%>", url);
}


After running the code, nothing happened. I set a breakpoint and tried again. Nothing. I commented out several options that the book carried as extra steps that I thought might interfere. Still nothing.

I then tried hooking the UserCreated event to the SendMail event that I defined, but ran into trouble when trying to create EmailEventargs to pass as a parameter.

And luckily, then I googled on "CreateUserWizard MailMessageEventArgs", and came across Bryant's blog.

The last line of code now reads:



e.Message.Body = e.Message.Body.Replace("<%Link%>", url);


And lo and behold, problem solved, and I find a neat link included in my mail :)

Bryant, I owe you a beer!

And to those still unconvinced by the Internet, I predict that some day it will be a big hit!


Read more!

Monday 3 September 2007

E-mailing from ASP.NET

I've been working on a website for a while now. As a base, I use the book ASP.NET 2.0 Website Programming: Problem - Design - Solution (Programmer to Programmer). Now using that, you actually add code to have E-mail sent from within your application using SMTP.

However, I couldn't get it to work. My E-mail address seemed valid enough, as did the code in my web.config:




Since I was planning on adding a new feature to the existing code to E-mail a new member a link they have to click before having their account activated, it seemed like a good idea to first sort out the whole mail thing.

I fiddled with a few things, but kept getting a: "5.7.1 Unable to relay for " error.


The solution is laughably simple (thankfully). Go to Internet Information Services (under Administrative Tools), expand your IIS server, and select right-click the Default virtual SMTP server. Select Properties, and go to the Access tab.

Under the "Relay restrictions" section, click the "relay" button. There, ensure the "Only the list below" option is selected, and press the "Add" button.

Under "Single computer", fill in 127.0.0.1, and click OK. That's it!

For security's sake, I chose to uncheck the checkbox named "Allow all computers which succesfully authenticate to relay".


Read more!