ASPNetFAQ.com: What is ASP.NET?

Technology posts on ASP.NET, IIS, Windows (+ a little Linux), Cloud Servers, Hosting, and more!
  • Blog Home
Search the site...

Loop Through Database Records (C#, Razor)

Tweet
Share
0 Shares

Here’s a code sample showing a quick and easy way to loop through the results of a SQL query and display them on a web page in a list.

@{
    var db = Database.Open("YourDatabase");
    var ListDataItems = db.Query("SELECT SomeField FROM SomeTable");
}
<!DOCTYPE html>
<html lang="en">
<meta charset="utf-8" />
    <head>
        <meta charset="utf-8" />
        <title>ForEach Query Test Page</title>
    </head>
    <body>
        <ul>
        @foreach(var ListItem in ListDataItems){
	<li>@ListItem.SomeField</li>
        }
        </ul>
    </body>
</html>

This is pretty basic but let’s break it down.

In this example I have an existing database in my project’s /App_Data/ folder named “YourDatabase.sdf” (it’s a SQL CE database). I reference that database in line 2 and then run my query in line 3. The results of that query are placed into a recordset stored in the ListDataItems variable also defined on line 3.

Most of the rest is basic HTML but on line 14 I start a look that iterates through each item in ListDataItems and puts the rows – one at a time – into the ListItem variable.

On line 15 I reference the row (using ListItem) and also the specific field I want to display – SomeField in this case. I could have easily had multiple items returned per row in my query (line 3) and in that case I would have been able to reference whichever, or multiples, using their specific field name.

Line 16 just closes out the code block for the loop.

Brad on Google+

More from my site

  • ASP.NET C# and VB web development vs PHPASP.NET C# and VB web development vs PHP
  • When Was The Last Time You Checked For Broken Site Links?When Was The Last Time You Checked For Broken Site Links?
  • Great Pointers For Analyzing Memory Crash DumpsGreat Pointers For Analyzing Memory Crash Dumps
  • Auto-Format VS2010 Source CodeAuto-Format VS2010 Source Code
  • Need to take ownership of multiple files or folders in Windows Server 2008?Need to take ownership of multiple files or folders in Windows Server 2008?
Tweet
Share
0 Shares

Comments are closed.

Proverbs 19:20

"Get all the advice and instruction you can, so you will be wise the rest of your life."

A Note On WordPress Hosting

Our main focus is of course .NET, but with a mix of Linux, virtualization, and other technologies. But if you're really looking for the best WordPress hosting specifically, read my WordPress host review to save yourself hassle AND money!




Recent Posts

  • What makes good web hosting?
  • jQuery Mobile C# ASP.NET and N5 Networks Software Repository
  • Open Source Bug Tracking Software and the Orchard Project
  • ASP.NET Development with Dreamweaver MX: Visual QuickPro Guide
  • Kendo UI Sample, ASP.NET Ajax Tutorial & More

Tags

ASP.NET Automation centos CMS css cytanium Development/Coding Email gmail Hosting htaccess http https IIS javascript Learning Linux logparser MySQL nginx openssl OrcsWeb performance PowerShell redirect RHEL security server SherWeb smtp SQL/Databases ssl System Administration telnet terminal tip Troubleshooting Ubuntu virtualization Visual Studio web farm web hosting Windows windows server Wordpress

Categories

  • ASP.net development
  • Development/Coding
  • Hosting
  • IIS (Internet Information Services)
  • SQL/Databases
  • System Administration
  • Virtualization
(c) ASPNETFAQ.com