This tutorial will show you how bind a DataBound control to a DataSource control. This process is called declarative databinding. This allows ASP.NET Framework to handle all the messy details of deciding when to retrieve the data items represented by a DataSource control.
Here is a snapshot of what the Web site will look like:
In this tutorial we will explore how every DataBound control has a DataSource property and a DataBind() method. This way you can programmatically associate a DataBound control with a data source. Below is an example of all the fonts being displayed that are installed on your computer.
First, we retrieve the fonts by the InstalledFontCollection class using System.Drawing.Text namespace. When you call the DataBind() method, the GridView control actually retrieves its data from the data source:
if (!Page.IsPostBack) { InstalledFontCollection fonts = new InstalledFontCollection(); grdGridView.DataSource = fonts.Families; grdGridView.DataBind(); } |
Be careful, if you neglect to call the DataBind() method it will display nothing. Then we display these results in a GridVeiw:
Now the list of fonts has been assigned to the GridView control’s DataSource property, thus the DataBind() method is called. A collection of fonts has now been assigned to the DataSource Property. In essence, you can assign any object that implements the IEnumberable interface to a DataSource property. You could assign collections, DataSets, arrays, DataReaders, DataViews, and enumerations to the DataSource property.
We moved our web sites to Server Intellect and have found them to be incredibly professional. Their setup is very easy and we were up and running in no time.
Now we will show you how to use a connection object that represents a connection to a data source. When instantiating a connection you must pass a connection string to the constructor. The connection string contains information about the location and security credentials that are required for connecting to a data source.
First off, you must open the connection before you execute any commands against the data source. After that you want to quickly close the connection. Simply because a data connection is such a valuable resource you want to open it as late as possible and close it as early as possible. Also, we will include some exception handling code to make sure that the connection gets closed.
Here is an example of the Using statement to force a connection to close in SQL Server even when an exception is raised:
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["PeopleConnectionString"].ConnectionString); SqlCommand cmd = new SqlCommand("SELECT * From Person", conn); cmd.CommandType = CommandType.Text; using (conn) { conn.Open(); lstPeople.DataSource = cmd.ExecuteReader(); lstPeople.DataTextField = ("Name"); lstPeople.DataBind(); } |
The using statement forces the connection to close, whether or not there is an error when a command is executed against the database. Also, the using statement disposes of the Connection Object, so if you need to use it again it will need to be reinitialized.
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!
Or you could use a try...catch statement to force the connection to close:
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["PeopleConnectionString"].ConnectionString); SqlCommand cmd = new SqlCommand("SELECT * From Person", conn); cmd.CommandType = CommandType.Text; try { Conn.Open(); cmd.executeNonQuery(); } finally { Conn.Close(); } |
Within this try..catch statement, finally forces the database connection to close regardless if there are errors or not.
Server Intellect assists companies of all sizes with their hosting needs by offering fully configured server solutions coupled with proactive server management services. Server Intellect specializes in providing complete internet-ready server solutions backed by their expert 24/365 proactive support team.
This can be displayed with a ListBox in your default.aspx page. For example:
The SqlDataReader calls the ExecuteReader method of the SqlCommand object, instead of directly using a constructor and then we just use a ListBox to display the data.
|