Posts Tagged ‘ASP.NET’

How-to: 301 Permanent Redirect with ASP.NET 4 – Response.RedirectPermanent()

Friday, July 9th, 2010 by Brent

During the process of migrating development over to the .NET 4 Framework there have been noticeable improvements.

One of the newest improvements is used quite often, Response.RedirectPermanent(). This new feature does a permanent redirection from a requested URL to a specified URL.

For a quick flashback to how this was previously completed review the code below:

/*
 * Previous 301 Permanent Redirect
 * */
Response.Status = "301 Moved Permanently";
Response.AddHeader("Location", "NewPage.aspx");

How you implement the new 301 Permanent Redirect is simply Response.RedirectPermanent(“URL-path-goes-here”). Here is an example with the new way with fewer lines of code.

/*
 * .NET 4 301 Permanent Redirect
 * */
Response.RedirectPermanent("NewPage.aspx");

By implementing 301 redirects it is a good practice to inform search engines that content has moved to a new location. Here are examples where Force 5 is involved with 301 redirects:

  • a web page that have moved
  • a Web page that has been removed
  • web page content that has been consolidated with another web page
  • non-www permanent redirect to the www (or vice versa)

How-to: Adding META Keywords & META Description with ASP.NET 4

Tuesday, July 6th, 2010 by Brent

In a previous post I showed how-to add specific page keywords & descriptions while using ASP.NET with Master Pages. Compared with the latest ASP.NET 4 version, this technique was not coder friendly.

In ASP.NET 4 adding page keywords and descriptions is as easy as this shown below.

    protected void Page_Load(object sender, EventArgs e)
    {
        // page keywords
        Page.MetaKeywords = "keywords go here...";
        // page description
        Page.MetaDescription = "description go here...";
    }

Overall, page keywords & descriptions are still important for SEO success along with page titles and page content. If you need any help with your website search rankings please feel free to contact Force 5 for some guidance.

SQL Case Study – Convert data rows to columns

Friday, March 5th, 2010 by Force 5

We recently had a project that involved putting together a survey.  This survey was comprised of almost 150 questions.  As we brainstormed the best way to construct the data tables to store this information, the thought of a table with 150 columns made us cringe.  Time constraints also called for something we could put together relatively quickly.  We decided to create a table that stored each question as a row of data.  Then we made a table that referenced the primary key of the question table along with the user’s answer to that question.  So instead of having a table with 150 columns, we have one table with 150 rows and another table that stores a data row for each question answered on the survey.  Now if a question needs to be added or removed from the survey all that needs to happen is add or remove a row from the questions table.

It also made collecting the survey data through a ASP.NET Web Site a lot easier, but that can be a future blog topic.

All of that was a setup for displaying the following solution that we created.  In order to display the data correctly for reporting purposes we needed to be able to transform the 150 rows of data in the questions table into a table with that data as column names.  In simpler terms, we needed to convert a set of data rows in table columns in a temporary table. Then we needed to be able to populate that table with the data from the answers table.

Here is the solution we came up with using the power of a stored procedures in Microsoft SQL Server.

CREATE PROCEDURE [dbo].[Survey_Answers]
AS
BEGIN
	SET NOCOUNT ON;
 
	-- Declare variables
	DECLARE @QuestionID varchar(20), @sql varchar(MAX)
 
	-- Create empty temporary table with id column
	CREATE TABLE #tempTable (SurveyID int NULL)
 
	---- Insert Columns into pivot table ----
	-- Declare cursor to loop through table
	DECLARE curQuestions CURSOR FOR
	SELECT     QuestionID
	FROM         Survey_Questions
 
	OPEN curQuestions
 
	FETCH NEXT FROM curQuestions INTO @QuestionID
	WHILE @@FETCH_STATUS=0
	BEGIN
		-- Defines each column
		SET @sql = 'ALTER TABLE #tempTable ADD ' + @QuestionID + ' varchar(1024) NULL'
		-- Executes the command which creates the column in the temp table
		EXEC(@sql)
		FETCH NEXT FROM curQuestions INTO @QuestionID
	END
 
	-- Clean up cursor
	CLOSE curQuestions
	DEALLOCATE curQuestions
	---- End of Insert Columns section ----
 
	---- Insert id values into pivot table ----
	-- Create rows in temp table using IDs from Survey table
	INSERT INTO [#tempTable] (SurveyID)
		SELECT     SurveyID
		FROM         Survey
 
	---- Insert data into pivot table ----
	-- Loop through each row in Survey_Answers
	-- Update values in pivot table
 
	-- Declare variables
	DECLARE @SurveyID int, @QuestionID2 varchar(20), @Answer varchar(1024), @CurrentSurveyID int
 
	-- Initialize variables
	SET @CurrentSurveyID = -1
	SET @sql = ''
 
	-- Declare cursor to loop through table
	DECLARE curAnswers CURSOR FOR
	SELECT     Survey_Answers.SurveyID, Survey_Answers.QuestionID, Survey_Answers.Answer
	FROM         Survey_Answers INNER JOIN
						  Survey ON Survey_Answers.SurveyID = Survey.SurveyID
	ORDER BY Survey_Answers.SurveyID
 
	OPEN curAnswers
 
	FETCH NEXT FROM curAnswers INTO @SurveyID, @QuestionID2, @Answer
	WHILE @@FETCH_STATUS=0
	BEGIN
		IF @CurrentSurveyId<>@SurveyId
			BEGIN
				-- This will run at the end of a set of questions related to one survey
				-- And initializes variables for next set of questions
				IF @sql<>''
					BEGIN
						SET @sql = STUFF(@sql, LEN(@sql), 1, ' WHERE (SurveyID = ' + CONVERT(varchar, @CurrentSurveyId) + ');')
						EXEC(@sql)
					END
				SET @sql = 'UPDATE [#tempTable] SET'
				SET @CurrentSurveyId = @SurveyId
			END
 
		-- Update values in pivot table
		SET @sql = @sql + ' ' + @QuestionId2 + ' = ''' + @Answer + ''','
		FETCH NEXT FROM curAnswers INTO @SurveyID, @QuestionID2, @Answer
 
		-- This section takes care of the last row since it will not go through the IF @sql<>'' code above. Uses same code as that section
		IF @@FETCH_STATUS = -1
			BEGIN
				SET @sql = STUFF(@sql, LEN(@sql), 1, ' WHERE (SurveyID = ' + CONVERT(varchar, @CurrentSurveyId) + ');')
				EXEC(@sql)
			END
	END
 
	-- Clean up answers cursor
	CLOSE curAnswers
	DEALLOCATE curAnswers
 
	-- Select values from created table
	SELECT     [#tempTable].*, Survey.DateCreated
	FROM         Survey INNER JOIN
		[#tempTable] ON Survey.SurveyID = [#tempTable].SurveyID
	ORDER BY Survey.SurveyID 
 
	-- Clean up the pivot table
	DROP TABLE #tempTable
END

Let us know what you think about our approach or if you have any questions.

Tutorial: How to add specific page keywords & descriptions while using ASP.NET Master Pages

Friday, October 23rd, 2009 by Brent

One of the most common questions that gets asked when starting out with ASP.NET Master Pages is how to add page specific keywords & descriptions.

For those not familiar with ASP.NET and/or Master Pages is that while using Master Pages the normal HTML tags including META tags are not part of individual pages. The individual ASP.NET pages only refer to content areas called, content place holders. The advantage of using Master Pages is that your site HTML template gets referenced in one area so you are able to make site wide changes to the Master Page instead of making the same changes to every individual web page within the site.

So to add either keywords or a description to your individual page you will need to create an HtmlMeta object as show below.

/* how to code */
protected void Page_Load(object sender, EventArgs e)
{
    // variables
    String sKeywords = String.Empty;
    String sDescription = String.Empty;
 
    // page keywords
    sKeywords = “Place your page keywords and phrases here.”;
 
    // page description
    sDescription = “Place your page description here.”;
 
    // meta tag keywords
    HtmlMeta mKeywords = new HtmlMeta();
    mKeywords.Name = “keywords”;
    mKeywords.Content = sKeywords;
    Header.Controls.Add(mKeywords);
 
    // meta tag description
    HtmlMeta mDescription = new HtmlMeta();
    mDescription.Name = “description”;
    mDescription.Content = sDescription;
    Header.Controls.Add(mDescription);
}