Kentico CMS 6.0 Developer's Guide

Displaying data from an external database

Displaying data from an external database

Previous topic Next topic Mail us feedback on this topic!  

Displaying data from an external database

Previous topic Next topic JavaScript is required for the print function Mail us feedback on this topic!  

Besides displaying Kentico CMS content, you can also display data from your external database or Web Service. In this case, you need to add custom code to your ASPX page that will use ADO.NET to retrieve the data or that will contact the Web Service and call its methods. Since you can place any custom code into the page (page template), you will simply use the standard ASP.NET code you would use when creating the website from scratch.

 

Example: Retrieving data from the sample Northwind database

 

In this simple example, you will see how to display data on the sample Corporate Site website from the Categories table of the Northwind database using ADO.NET. You may need to use some other database if you do not have the sample Northwind database on your server.

 

Open the web project in Visual Studio using the WebProject.sln file. Open the CMSTemplates/CorporateSite/HomeASPX.aspx page.

 

Drag and drop the standard ASP.NET GridView Data control onto the page and set its ID to GridView1.

 

Add the following line to the beginning of the code behind file:

 

[C#]

 

using System.Data;
using System.Data.SqlClient;

 

Add the following code to the Page_Load method:

 

[C#]

 

// create sql connection - you could use Oracle or OLEDB provider as well
SqlConnection cn = new SqlConnection("Persist Security Info=False;database=northwind;server=server1;user id=sa;password=psswd;Current Language=English;Connection Timeout=120;");      

 
// create data adapter
SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM categories", cn);
DataSet ds = new DataSet();
 
// fill the dataset with data from database
da.Fill(ds);
 
// bind data to the grid view
GridView1.DataSource = ds;
GridView1.DataBind();

 

Save all changes. Open the Corporate Site and look at the Examples -> Development Models -> ASPX page on the live website. You will see a grid with data from the external database:

 

devguide_clip0274

 

As you can see we used standard ASP.NET methods to display external data on the website.