Lesson 2: Using Data Sets on Web Forms
The purpose of this lesson is to detail the easiest ways to use the DataGrid and DataList controls, to bind data to other controls such as the DropDownList control and to briefly explore executing commands directly against a data connection.
- "The simplest way to display a data set on a Web form is through a DataGrid control using data binding."
The Visual Studio Web forms designer provides an interactive, design-time way to declare DataGrid display and editing functionality. Creating Template columns with Button columns, using the DataGrid Property Builder is the first step.
- "At run time, the DropDownList control displays a list... The limitation here is that you can include only one value for the list control's DataText and DataValue properties."
This is one example of how the Visual Studio .NET 1.x designer depends on code for "advanced" functionality.
- "...you can perform commands directly on a database connection."
These three methods are available from the database connection object: ExecuteScalar(), ExecuteNonQuery() and ExecuteReader().
- "Because any command that executes on a database has at least some potential to fail, you should always use exception handling... Calling the connection's Close method from a Finally/finally exception-handling clause ensures that the database connection is closed before the code continues."
For performance reasons, catching a "context sensitive" exception like InvalidOperationException or SqlException is best practice over dropping down to an Exception catcher.
-
"The ExecuteNonQuery method acts directly on the database connection---it does not go through a data adapter or a data set... you must update any data sets affected by those changes by calling the Fill method on the adapter."
-
"Using ExecuteReader to create data reader objects provides better performance than creating a data set from a data adapter object, but it doesn't provide you with much flexibility... A reader object locks the database connection while it is executing, so you should call the reader object's Close method when you have finished getting records..."