1. Home
  2. Computing & Technology
  3. Visual Basic

Learn ASP.NET 2.0 - Intro to the Grid Control
Coding the new page

By Dan Mabbutt, About.com

Apr 23 2008

To get started, first add another page to the website in VWD. Right-click on the website solution name in Solution Explorer and select Add New Item... Select the Web Form template and give it a unique name before clicking the Add button. I named mine disp_msgs.aspx.

--------
Click Here to display the illustration
Click the Back button on your browser to return
--------

The two new partial classes will be added to your project.

--------
Click Here to display the illustration
Click the Back button on your browser to return
--------

Next, we need a "datasource" control to actually get the rows from the database and feed them to the control we will add. The Toolbox has a great collection that will retrieve data from a variety of sources. In addition to the SqlDataSource control, the one we will use, you can also get data from Access, LINQ, Object, XML and SiteMap.

The GridView control includes a wizard that will add a datasource as one of the steps, but lets put one in separately to detail what happens. So double-click on the SqlDataSource control in the Toolbox to add it to the solution.

The datasource controls are added to the .aspx file, but they don't actually show up on the web page. This is similar to Windows forms controls like the Timer.

--------
Click Here to display the illustration
Click the Back button on your browser to return
--------

Before the SqlDataSource control can access the database, it has to be configured. This tells the control where the database is and how to access it. Fortunately, a wizard does the hard work for you.

--------
Click Here to display the illustration
Click the Back button on your browser to return
--------

One of the first things we need is a connection string. Remember that we got one before by copying the connection string from the properties of the AVBQuestions database. That's because we were doing it "old school" style. The wizard helps us out here. We simply select the connection we want from the dropdown and it's there. But since we haven't looked at the connection string in any detail previously, lets do that now.

In general, a connection string has to provide these pieces of information:

  • The database server
  • The database
  • The authentication security

The connection string that is generated is:

Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\AVBQuestions.mdf;Integrated Security=True;User Instance=True

Data Source identifies the "instance" of SQL Server that we're going to use. One of the strengths of SQL Server is that multiple "instances" can be running on a computer at the same time. ".\SQLEXPRESS" translates into the default installed instance of SQL Server Express on this computer. In high performance situations, this helps make SQL Server as fast as any database available. SQL Server 2005 Enterprise Edition supports 50 instances, and SQL Server 2005 Standard, Workgroup, and Express editions each support 16 instances.

AttachDbFilename is the path to the database. "|DataDirectory|" refers to the website's App_Data folder.

Integrated Security means Windows authentication. This is the security recommended by Microsoft.

User Instance=True refers to a new capability of SQL Server Express. Ordinarily, named instances of SQL Server are used, but SQL Server Express creates a new type of instance that behaves differently: User Instances. This trick makes deploying databases with applications easier by creating a database instance on demand. This works even if users don't have administrative rights.

The next step of the wizard lets you save the connection in the application configuration file, Web.Config. I named it AVBConnect where it was saved like this:

<connectionStrings>
   <add name="AVBConnect"
      connectionString=
      "Data Source=.\SQLEXPRESS;
      AttachDbFilename=|DataDirectory|\AVBQuestions.mdf;
      Integrated Security=True;User Instance=True"
    providerName="System.Data.SqlClient" />
</connectionStrings>

The next steps let you code a SQL statement to populate the control. Here's where you decide which columns you need and the order to receive them. The end result is shown in the illustration below, along with a test to prove that it works:

--------
Click Here to display the illustration
Click the Back button on your browser to return
--------

One more detail should be noticed. In an earlier part of this tutorial, I wrote, "The primary object used with disconnected access is the DataSet. This is an in-memory object that holds a copy of the data from the database that you can process much like it was the database itself." If you check the properties of the SqlDataSource object that was just created in VWD, you'll see that the DataSourceMode property is DataSet. This is why GridView can do the magic.

Explore Visual Basic
By Category
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Visual Basic
  4. Learn VB.NET
  5. Learn ASP.NET
  6. Learn ASP.NET 2.0 - Intro to the Grid Control

©2009 About.com, a part of The New York Times Company.

All rights reserved.