Skip to main content

Umbraco Razor, How to Connect and Query to a SQL Database and Loop around the Results, and use a Helper to display the results

1. Connect to the Database (the connection string must be in the Web config file)
2. Loop around the results (this code loops around a database that stores vistits to FAQ pages, and stores the Page ID)
3. Use a helper to display the results

Field names in Database 

@inherits umbraco.MacroEngines.DynamicNodeContext
@using WebMatrix.Data

// 1 Contect to the Database and Select the items
        var DB = Database.Open("myAnalyticsConnection");
var FAQs = DB.Query("SELECT * FROM Analytics WHERE docType='FAQ'  ORDER BY numberOfClicks DESC").Take(5);


// 2For Each of the Items(FAQs) loop arround
foreach (var FAQ in FAQs)

@helper getPageLink(int thisPage)
//3.  A Helper to get the node URL, as this is not stored in the database
try {

var thisPageId = @Model.NodeById(@thisPage);

<li style="text-align:left;background-image: url(/images/arrow-white.png);">
<a href="@thisPageId.Url">
<span style="color:#ffffff">@thisPageId.Name</span>




  1. Hello, i dont understand this: the connection string must be in the Web config file,,, thank you

  2. Thanks for the always useful information. This is great information to help garage type SEO people like me.RazorSQL 7.3.2

  3. Very informative and It was an awesome post. I love reading your fantastic content. Thanks for sharing it with us. We are so greatful to your sharing. Download Razorsql


Post a Comment

Popular posts from this blog

Create a .NET Contact Form that Gets the Last Url Visited in C# Can also be Used in Umbraco

This is a Simple Contact From done in C# .Net and also can be used in Umbraco.  Paste the code in to Visual Web Developer for a better view. The important bits are coment in the second file.  One of the Commented out lines highlights how to get the mailto address from an Umbraco field. The code Below includes the myContactForm.ascx.cs codebehind file and the myContactForm.ascx file. 1) The myContactForm.ascx file:  <%@ Control Language="C#" AutoEventWireup="true" CodeFile="ContactFormforPublications.ascx.cs" Inherits="usercontrols_ContactFormPublications" %> <div class="contact-form" id="contactForm" runat="server">     <h3>Request a Publication</h3>     <div class='int'>         <form runat="server">         <asp:Panel DefaultButton="contactFormSubmit" runat="server">         <fieldset>             <div class='

Umbraco 7 - C# Razor - Passing a parameter variable from a Template to a Partial View.

Passing a parameter variable from a Template to a Partial View Example - Passing a Page Id to a Partial View. In the example below we are passing 3 page Ids (1090. 1091, 1092) to a partial view.  The Partial View will display the details for each of these nodes on a home page.  See final output below. 1) In your Template: @Html.Partial("HomePageLinksToLandingPages", new ViewDataDictionary{{ "pageId", 1090}}) @Html.Partial("HomePageLinksToLandingPages", new ViewDataDictionary{{ "pageId", 1091}}) @Html.Partial("HomePageLinksToLandingPages", new ViewDataDictionary{{ "pageId", 1092}}) 2) The Partial View: @inherits Umbraco.Web.Mvc.UmbracoTemplatePage @{ var thisPageId = 1090; var stringPage = @ViewData["pageId"]; thisPageId = Convert.ToInt32(stringPage); } <div class="col-md-4"> <arti