Posts Tagged ‘programming’

Simple WordPress Page Redirect Hack

May 23rd, 2011

I recently was looking for a way to setup a WordPress Page that actually was just a link/placeholder for an external site. This allows me to have my external page be automatically part of a theme’s navigation, etc. The solution I used is actually pretty simple and allows for an unlimited number of instances of these “redirect pages” without having to use any external plug-ins.

First you need to setup a simple PHP file that will serve as a template for your new page. Let’s say for example you have a club that you want to add as part of your navigation. Create a file called Club.php and put the following code in it:

<?php
/*
Template Name: Club
*/
?>
<?php header('Location: http://www.myclubsite.com');
      die();
?>

Then upload the file you created (Club.php) into /wp-content/themes/yourthemename/ where yourthemename is the name of the active theme for your site.

The final step is to create a new Page with the name that you want to show up in your navigation “My Club” based on the template that you just uploaded. You will see this template under the Template drop down menu in the page administration interface along with “Default Template”, etc.

Publish and you have a page that automatically redirects to whatever the URL you specified in your PHP template file. I create a separate one of these for any of these “redirect pages” that I want to setup.

It’s easy and it works!

Rename an email attachment before sending in .NET

April 21st, 2011

I recently had the need to send an email with a file attachment from disk. The problem I faced was that the filename was not very user friendly at all. What I didn’t want to do however was rename the file on disk, or create another copy of the file with a more generic name, that could create conflicts with other users in the system.

I was happy to find that you can simply change the attachment’s file name after you create it based on the file on disk. So my original file, 2073B129-6DFF-4088-A785-C9CD8B1AADBF.pdf, could appear as Agreement.pdf to the recipient of the email. Much better, don’t you think?

And here’s how I did it:

Attachment attachment;
attachment = new Attachment("2073B129-6DFF-4088-A785-C9CD8B1AADBF.pdf");
attachment.ContentDisposition.FileName = "Agreement.pdf";
MailMessage.Attachments.Add(attachment);

There’s obviously more to the code related to creating the message, etc. But this is what you need if you want to give the attachment a different file name.

Enjoy!

CMAP Main Meeting – Tuesday, December 7th – IIS Express, Razor, and WebMatrix, OH my!

December 7th, 2010
CMAP Main Meeting – Tuesday, December 7th – IIS Express, Razor, and WebMatrix, OH my! – G. Andrew Duthie

When: Tuesday, November 7th, 6:30 PM – 9:00 PM

Where: HCC Business Training Center, 6751 Columbia Gateway Drive, MD, 21046

Topic: IIS Express, Razor, and WebMatrix, OH my!

Does the announcement of so many new technologies make you wonder where the yellow brick road is that will lead you to the Oz of understanding? Well, there’s no denying that there’s a lot to keep up with these days if you’re a web developer. So let Microsoft Developer Evangelist G. Andrew Duthie give you an overview of Microsoft WebMatrix, ASP.NET Web Pages, the new Razor syntax, and IIS Express, and how they fit in with the existing offerings in Microsoft’s web stack.

Learn how and when you’d want to leverage these new technologies, and when existing technologies may be a better choice. Expect discussion and demo, and bring your questions, so we can make this an interactive and dynamic session!

Presenter: G. Andrew Duthie

G. Andrew Duthie, aka .net DEvHammer, is the Developer Evangelist for Microsoft’s Mid-Atlantic States district, where he provides support and education for developers working with the .net development platform. In addition to his work with Microsoft, Andrew is the author of several books on ASP.NET and web development, and has spoken at numerous industry conferences from VSLive! and ASP.NET Connections, to Microsoft’s Professional Developer Conference (PDC) and Tech-Ed. Andrew is also the creator and developer of Community Megaphone, a site designed for promoting and finding developer community events. Andrew can be reached through his blog at http://blogs.msdn.com/gduthie or on Twitter at @devhammer.

For more information about the meeting, please visit the CMAP website.

Forcing a user to read (or scroll through) all text before accepting terms

December 10th, 2009

If you’ve used a computer before you’ve undoubtedly scrolled through and agreed to some sort of agreement.  Most likely it was some sort of software license agreement that you didn’t read about some website you were signing up on or an application that you were installing.

Maybe, if you’ve installed enough software or been on enough websites you’ve come across an instance where they actually forced you to scroll all the way down to the bottom of the text before you were able to click “I Agree” or whatever acknowledgment they wanted you to use.

Well I was recently faced with creating this exact situation in a web application and ended up using jQuery to accomplish this in my ASP.NET application.  For my particular situation I ended up putting my content inside a scrollable div.  This can easily be done by using a textbox if you wanted without much effort.

Basically, here’s what you’ll need.

  1. Reference jQuery (I’m not going to go into that, you can easily find that out here)
  2. Put a DIV on your page containing your text that needs to scroll (obviously you’re putting more than a few sentences or you wouldn’t be in this boat)
  3. Put a button on your page that, once enabled, will log the user’s acceptance and redirect them accordingly
  4. Some simple JavaScript to tie the DIV’s scrolling event to your button

Here’s our DIV:

<div style="width: 400px; height: 400px; overflow: auto; id="Terms">
<p>Lots of text to read.</p>
<p>Lots more text to read</p>
</div>

Here’s our button:

<asp:Button ID="ContinueButton" runat="server" Text="Continue" />

Here’s our JavaScript:

<head runat="server">
<script type="text/javascript" src="js/jquery-1.3.1.min.js"></script>
<script type="text/javascript">
     $(document).ready(function() {
 
          // Initially disable the button
          $("#ContinueButton").attr("disabled", "disabled");
 
          // Map the function below to the scroll event of our Terms DIV
          $("#Terms").scroll(function() {
               if ($("#Terms").AtEnd()) {
                    // Enable the button once we reach the end of the DIV
                    $("#ContinueButton").removeAttr("disabled");
               }
           });
     });
 
     $.fn.AtEnd = function() {
         return this[0].scrollTop + this.height() >= this[0].scrollHeight;
     } 
</script>
</head>

And that’s it. This code is light weight and works in IE, Firefox, Chrome and Safari. Have any feedback or suggestions on how to make it better? Let me know.

CMD HTTP Request – command line HTTP request utility

December 9th, 2009

More and more I find that I need to setup some kind of job or scheduled task to accomplish something in .NET on a reoccurring basis.  Typically in the past I’ve written Windows Services to accomplish this.  While effective, these definitely take longer to write and are harder to debug than say a simple ASP.NET page.  What I’ve done lately is started to move these non-critical, non-security sensitive processes into ASP.NET pages that can be called on a specific schedule via Windows Task Scheduler.

When I started moving this way I realized that I wanted to find a small utility that I could run from a command line to initial a web page request.  It had to be something I could run from a scheduled task and something that I could use to save or log the results.  After doing my due diligence Googling I realized there wasn’t such a utility that I could easily run from within Windows without installing all kinds of libraries and non-Windows based tools.  So, like any good programmer, I made my own.  Enter CMD HTTP Request.

As I said, this utility is small, light weight and runs on Windows via the .NET Framework.  You don’t need any special commercial programs to run it and it will even check your pages for keywords you specify and save the request’s results to disk as a HTML file.  This, essentially, is your log of what happened during that request on that date and time.

I won’t go into too much more detail here.  I think you get the main idea.  You can  download the source code or executable from the project page on Codeplex and learn more about it.  As always, feel free to leave me any feedback or suggestions either here or via the project page on Codeplex.

CMD Email v2.0 released

November 14th, 2009

CMD Email, the email command line utility I developed (originally discussed here) has been updated to version 2.0.

Here are new features for v2.0:

  • Updated to target .NET 3.5 Framework
  • Added support for message body being loaded from a file
  • Added support for multiple file attachments
  • Added support for logging to the Windows Application Event Log

You can download the latest runtime or source from the project page on CodePlex.

Going for MCTS certification on Visual Studio 2008

April 23rd, 2009

I have been dragging my feet for the past few years in regards to finishing up my Microsoft development certification.  Actually, the last test I took was 70-229 Designing and Implementing Databases with Microsoft SQL Server 2000 Enterprise Edition back on Dec 08, 2003.  WOW, I had no idea it was that long until I just looked it up.

Anyway, what finally got me to get moving was a great voucer/coupon I got via email from Prometric, the testing company I used last time.  Not sure if it’s specific to me because I’m already a MCP (yes, I passed that test from years ago) or not but I’ll share it just in case it does work for others…

…Improving and validating your technical skills can help.

That is why Prometric is providing a limited offer to the first 4,000 individuals to help get your Microsoft Certified Professional status current or achieve an additional certification.

Offer available for customers who have taken their last certification exam prior to January 1, 2007.

Use this promo code ‘MCPBACK’ and get a $25 USD Certification (Normally priced at $125 USD). This offer is valid for any exam in the MCTS/MCPD/MCITP track. Does not include Microsoft Office or Windows end-user (non-IT) focused exams.

Go to: www.Prometric.com/microsoft to sign-up for your next exam.

But you better hurry!
You must take your exam by June 30, 2009.

Offer valid in US and Canada only.

My plan is to obtain the MCTS: .NET Framework 3.5, ASP.NET Applications certification related to Visual Studio 2008.  What I’m going to do is take the C# test rather than VB.NET, which is what I mainly use here at work.  I can work in C# and have written a few things in it, but my main language since I’ve been working in .NET has been VB.NET.

I figure this will be a good way to become more familiar with C# and will force me to learn the language.  Once you’ve been working with .NET for a while and looking through code samples on Google you’ll quickly realize that the majority of the code samples out there are done in C#.  So, as you can imagine, converting those to VB.NET can be frustrating after a while.

So, we’ll see how it goes.  I only spent $25 to book the test and another $44 to upgrade my copy of MCTS Self-Paced Training Kit (Exam 70-536): Microsoft® .NET Framework Application Development Foundation to the second edition.  I picked up the first edition a year or two ago when I first decided I wanted to get the MCTS and only got about 1/2 way through it.

My test is set for June 17, wish me luck!

FredNUG tonight

April 22nd, 2009

I’ll be attending the Frederick .NET user group tonight.  Tonight’s topics will be SQL Server Performance and Coding for Fun and Profit.  More information can be found here.

Testing email when working with no SMTP server

January 9th, 2008

Happy New Year!

While this tip isn’t my own, it still seems as though it will be very helpful.  I know that I do a lot of development locally where I don’t have an SMTP server setup.  This tip, courtsey of .NET Tip of the Day, really will eliminate that problem and allow you to work with email without the headaches.

Testing code that sends email has always been a pain. You had to set up a SMTP service just to test that your .NET application sends the e-mail correctly.

However, there is a way to send e-mails with no SMTP server set up. Just configure your .NET application to drop e-mails into a specified folder instead of sending them via SMTP server:

<system.net>

<mailSettings>

<smtp deliveryMethod=”SpecifiedPickupDirectory”>

<specifiedPickupDirectory pickupDirectoryLocation=”c:\Test\” />

</smtp>

</mailSettings>

</system.net>

This will instruct SmtpClient class to generate mail message, save it as .eml file and drop it into c:\Test\ folder.

VB.NET RC4 Encryption for database storage

November 28th, 2007

I recently had to upgrade some Classic ASP code to .NET for some data encryption.  The routines use RC4 encryption and make the result database friendly.  The following class can easily be dropped into your project for use with little effort.  The sample code shows the encryption and decryption methods.  You just provide the message and the key for either instance.  From there you can drop it in your database or do whatever you want!

Sample Usage:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

Dim plainText As String = “I’m exposed!”
Dim passkey As String = “keep me safe”

Dim safeText As String
safeText = Common.Encryption.Encrypt(plainText, passkey)

Response.Write(safeText)

Dim decrypted As String
decrypted = Common.Encryption.Decrypt(safeText, passkey)

Response.Write(decrypted)

End Sub

You can view the entire class here.  If you’re looking for some quick and easy encryption this will do the trick.