Click here to Skip to main content
15,891,513 members
Articles / Web Development / HTML

Permalinks for Fun and Profit

Rate me:
Please Sign up or sign in to vote.
4.57/5 (9 votes)
4 Sep 2008CPOL4 min read 26.6K   144   34  
Permalinks provide an easy way to redirect incoming traffic to specific pages, track hits and goals, and prevent external links from expiring.
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Text;

public partial class external : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //we want to generate a full URL, e.g. http://localhost/PermaLinks/?linkid=###
        //this is probably not the best way to do this, but it works ;-)
        string baseUrl = this.Request.Url.AbsoluteUri;
        baseUrl = baseUrl.Replace("/external.aspx", "/?linkid=");
        //generate a table of links using a data reader;
        //we could have used a repeater control instead
        SqlConnection conn = null;
        SqlCommand cmd = null;
        SqlDataReader rdr = null;
        StringBuilder sb = new StringBuilder();
        try
        {
            conn = new SqlConnection(ConfigurationManager.ConnectionStrings["db"].ConnectionString);
            cmd = new SqlCommand("select CampaignLinkId, CampaignLinkDescription from dbo.CampaignLink", conn);
            conn.Open();
            rdr = cmd.ExecuteReader(CommandBehavior.Default);
            sb.Append("<table border='0' align='center' width='90%'>");
            while (rdr.Read())
            {
                //generate a row in the table for each external link
                sb.Append("<tr><td><a href='");
                sb.Append(baseUrl);
                sb.Append(rdr.GetInt32(0));     //link id
                sb.Append("'>");
                sb.Append(rdr.GetString(1));    //campaign link description
                sb.Append("</a></td></tr>");
            }
            sb.Append("</table>");
            this.Literal1.Text = sb.ToString();
        }
        catch(Exception ex)
        {
            //in a real system, only catch SQL errors and report them somewhere...like to CALM ;-)
            this.Literal1.Text = "ERROR: " + ex.Message;
        }
        finally
        {
            if (rdr != null && !rdr.IsClosed)
            {
                rdr.Close();
                rdr.Dispose();
                rdr = null;
            }
            if (conn != null && conn.State != ConnectionState.Closed)
            {
                conn.Close();
                conn.Dispose();
                conn = null;
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead ThoughtWorks
United States United States
Steven A. Lowe is a consultant, software developer, inventor, entrepreneur, author, musician, and lover of puns. He ran an innovative custom software development company for nearly a decade before joining ThoughtWorks as a Principal Consultant in 2014. He admits to being the author of "From Burnout to Bonfire" and a willing participant in the band Noise in the Basement, but neither confirms nor denies being the science-fiction author Steven Ayel.

Comments and Discussions