Server Intellect
 
Home   Asp.Net Tutorials   What's New   Newsletter   More Resources
 
 
  Categories
Advanced Technologies
AJAX
Internet Browsers
Controls
Database
Email
Error Handling
File
Graphics
Website Navigation
Network
Performance
User Interface and Themes
Validation
Visual Web Developer
Web Services
XML
Suggest Tutorial


Navigator: Home - Tutorials - Database - Displaying Data using ASP.NET 2.0 GridView and C#
Displaying Data using ASP.NET 2.0 GridView and C#


ASP.NET Database Tutorial

This tutorial will show you how to display data using the .NET GridView Control, ASP.NET 2.0 and C#.NET

The GridView control is a powerful tool and is simple to implement.

First, you will need to import the System.Data.SqlClient namespace.

The System.Data.SqlClient namespace contains the SqlCommand and SqlConnection classes that we need in order to connect to our database and to send an SQL command to it.

using System.Data.SqlClient;

Try Server Intellect for Windows Server Hosting. Quality and Quantity!

We'll put our code in the Page_Load() event.

When the Page_Load() event fires, a new SqlCommand object is instantiated with our connection string and our command.

Afterwards, we will attempt to connect using the Open() method of our cmd.Connection object. Once it is connected we will attempt to execute the command we specified earlier (in this example "SELECT TOP 5 firstname,lastname,hiredate FROM EMPLOYEES" in the Northwind db).

If all goes well, we will have the results of our SQL query assigned to the gvwExample's DataSource property. Now all we have to do is call the DataBind() method of our gvwExample to bind the data to the control. The data is now ready to be displayed.

protected void Page_Load(object sender, EventArgs e)
{
SqlCommand cmd = new SqlCommand("SELECT TOP 5 firstname,lastname,hiredate FROM EMPLOYEES", new SqlConnection("Server=localhost;Database=Northwind;Trusted_Connection=True;"));

try
{
cmd.Connection.Open();

gvwExample.DataSource = cmd.ExecuteReader();

gvwExample.DataBind();

cmd.Connection.Close();
cmd.Connection.Dispose();
}
catch (Exception ex)
{
lblStatus.Text = ex.Message;
}
}

If you're looking for a really good web host, try Server Intellect - we found the setup procedure and control panel, very easy to adapt to and their IT team is awesome!

We have to add a few tags on the front end of the .aspx page to place where we want the GridView control to display its bound data. We also specify what part of the data from the data set we would like to display (in this case, the DataItems firstname, lastname, and hiredate") as well as the title we would like to give each data item. The front end .aspx page looks something like this:

<tr>
<td width="100" align="right" bgcolor="#eeeeee" class="header1"> Employee Data Using the GridView Control:</td>
<td align="center" bgcolor="#FFFFFF">
<asp:GridView ID="gvwExample" runat="server" AutoGenerateColumns="False" CssClass="basix" >
<columns>
<asp:BoundField DataField="firstname" HeaderText="First Name" />
<asp:BoundField DataField="lastname" HeaderText="Last Name" />
<asp:BoundField DataField="hiredate" HeaderText="Date Hired" />
</columns>
</asp:GridView>
<asp:label ID="lblStatus" runat="server"></asp:label></td>
</tr>

Need help with Windows Dedicated Hosting? Try Server Intellect. I'm a happy customer!

The flow for the code behind page is as follows.

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SqlCommand cmd = new SqlCommand("SELECT TOP 5 firstname,lastname,hiredate FROM EMPLOYEES", new SqlConnection("Server=localhost;Database=Northwind;Trusted_Connection=True;"));

try
{
cmd.Connection.Open();

gvwExample.DataSource = cmd.ExecuteReader();

gvwExample.DataBind();

cmd.Connection.Close();
cmd.Connection.Dispose();
}
catch (Exception ex)
{
lblStatus.Text = ex.Message;
}
}
}

Download the Full Working Version of this Project written with Visual Studio.NET VB.NET 2005 Here!

Looking for the VB.NET 2005 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!





 
  Developer Resources







Server Intellect Rocks