Click here to Skip to main content
15,885,914 members
Articles / Web Development / CSS

Building a Web Message Board using Visual Studio 2008, Part I - The Basic Message Board

Rate me:
Please Sign up or sign in to vote.
4.90/5 (83 votes)
30 Dec 2007CPOL47 min read 375.2K   3.7K   333  
This article builds a web based message board and uses several new technologies introduced with Visual Studio 2008 such as LINQ, WCF Web Programming, WCF Syndication, ASP.NET ListView, ASP.NET DataPager etc.
<%@ WebHandler Language="C#" Class="InstallDatabase" %>

using System;
using System.Web;
using System.Web.Management;
using System.Data.Common;
using MessageBoard.DataAccess.Linq;
using System.Web.Configuration;
using System.IO;

/// <summary>
/// This handler installs the database if the database does not already exists.
/// This should not be used on production machines
/// </summary>
public class InstallDatabase : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        HttpResponse response = context.Response;

        response.Buffer = false;
        response.ContentType = "text/html";
        response.Write("<html><head><title>Creating Database</title></head><body><div>");
        response.Write("Please wait while the database is being created....<br />");
        response.Flush();

        string connectionString = WebConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString;

        string dataDirectory = AppDomain.CurrentDomain.GetData("DataDirectory") as String;

        if (!dataDirectory.EndsWith(System.IO.Path.DirectorySeparatorChar.ToString()))
            dataDirectory += System.IO.Path.DirectorySeparatorChar;

        connectionString = connectionString.Replace("|DataDirectory|", dataDirectory);

        MessageBoardDataContext dataContext = new MessageBoardDataContext(connectionString);

        if (!dataContext.DatabaseExists())
        {
            dataContext.CreateDatabase();

            response.Write("Adding ASP.NET Services....<br />");
            response.Flush();

            //Now add ASP.NET features
            SqlServices.Install(dataContext.Connection.Database, SqlFeatures.All, connectionString);

            response.Write("Installing sample data....<br />");
            response.Flush();
            
            string sampleDataSqlFile = context.Request.MapPath("~/Install/InstallSampleData.sql");
            dataContext.ExecuteCommand(File.ReadAllText(sampleDataSqlFile));

            response.Write("Database created successfully!<br />");
        }
        else
        {
            response.Write("Database already exists<br />");
        }
        
        response.Write("<script type='text/javascript'>window.setTimeout(function() { window.location.href = '../default.aspx'; }, 3000);</script>");
        response.Write("If you are not automatically redirected, <a href='../default.aspx'>Click here</a> to go to the home page.<br />");
        response.Write("</div></body></html>");
        response.Flush();
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}

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
Architect
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions