WebMatrix Tutorial – Working with Data

In this tutorial we will focus on working with the inbuilt database (SQL Server CE 4) in WebMatrix. If you are new to WebMatrix please check out Part 1 of the WebMatrix Tutorial which deals with installing WebMatrix and working with the new ASP.NET Pages code files.

Working with the Database in WebMatrix

SQL Server CE is deeply integrated into WebMatrix, select Databases on the menu at bottom left of the interface and if you do not currently have an associated database you will be confronted with the below screen:

WebMatrix SQL Server Database

Click Add a Database to your site , then right-click Tables and select New Table.

WebMatrix SQL Server Database

You will then be able to add columns to your table (just click New Column on the ribbon for add the additional columns). You can specify the column details in the Column Properties settings list:

WebMatrix Database

You can edit or add data by clicking Data in the ribbon, which will take you to the existing data in the database presented as an editable grid:

WebMatrix SQL Server Database

Of course you would not normally add data using this method but it is convenient for a quick edit or to add some dummy data.

Note that using WebMatrix does not mean that you are tied to using the inbuilt SQL Server CE, you can interact with any version of SQL Server or other databases supported by the .NET framework such as MySQL.

Accessing the Database using ASP.NET Pages in WebMatrix

The database can be easily accessed using the new database API – primarily using the Open, Query and Execute commands.

The Open command combined with the Query command can be used to access the data in the database:

WebMatrix Databases

You can use queries in parameters to generate dynamic pages. Note the syntax of the Query command of the database API, which can accept numbered parameters:

WebMatrix

Updating the Database using ASP.NET Pages in WebMatrix

The Execute command of the Database object can be used to insert, update or delete data from the database. The first paramater of the Execute command accepts the SQL string for the database operation and the subsequent parameters are for the parameters to be inserted into the SQL string.
Continues…

WebMatrix Tutorial – Getting Started

In this WebMatrix Tutorial we will look at getting started with using WebMatrix and look at the new ASP.NET Pages coding method.

Introduction to WebMatrix

WebMatrix is a fully integrated development environment for building ASP.NET web apps. Currently developing an ASP.NET web will require using Visual Studio, IIS and SQL Server Management Studio all of which complicated tools in their own right. WebMatrix is Microsoft’s attempt to integrate a coding tool, database development tool and web server admin tool in one simple application for rapid application development.

WebMatrix incorporates several new technologies and tools from Microsoft which should make development faster and more simple. First is IIS Express, Visual Studio ships with an inbuilt  web server which is very convenient for testing during development but it is not 100% compatible with IIS 7 or 7.5 and so issues often arise when running the app in production. IIS Express which is incorporated into WebMatrix is a fully compatible but scaled down version of IIS meaning that developers can safely deploy apps that are only tested using the development environment. SQL Server Compact Edition 4 is the database incorporated into WebMatrix, the major innovation in SQL Server CE 4 is that it does not require any database server to run, you  simply build the tables etc in WebMatrix and just ftp the binaries created and the database will run (even on shared servers without admin privileges). Finally the web coding  itself features a simpler interface and the new lean ASP.NET Web pages  ”Razor” syntax syntax. The Razor syntax allows you to easily embed anywhere code in your page using the @ character and is definitely leaner and quicker to work with that the previous model of strictly separating code and markup (although this may not be as suitable for very large projects).

Getting Started with WebMatrix

Currently WebMatrix is in Beta (although it is still robust for  developing apps) and can be downloaded here.

Installation is a simple process (although you will be prompted to download and install Web Platform Installer 3.0 and ASP.NET 4.0 if you do not already have them). On your first run WebMatrix will present you with the below page:

WebMatrix Tutorial

You are presented with four options :

  • My Sites – which is the sites you previously used on WebMatrix
  • Site From Web Gallery – this is a similar listing of open source web-app projects as provided in the Web Platform Installer. Selecting a project such as Umbraco will install it as well as create and configure any associated databases.
  • Site From Template – WebMatrix ships with a limited number of pre-built site templates, none of which are especially useful for any purposes other than training.
  • Site From Folder – Allows you to open sites which were not previously opened with WebMatrix.

It isn’t immediately obvious from this list how to create a new site, but click Site From Web Gallery, then select Empty Site and click OK:

WebMatrix Tutorial

Continues…