In this blog you will find ASP.NET 3.5 articles and some code samples. These code samples are created by me and should be working. Thanks for visiting my blog! Happy Dot Netting

Thursday, September 11, 2008

SQLDataSource Control


1. The SqlDataSource control is built on top of ADO.NET. Under the covers, the SqlDataSource uses ADO.NET objectssuch as the DataSet, DataReader, and Command objects.

2. The SqlDataSource control is not an appropriate control to use when building more complicated multi-tier applications. The SqlDataSource control forces you to mix your data access layer with your user interface layer. If you want to build a more cleanly architected multi-tier application, then you should use the ObjectDataSource controlto represent your database data.

3. Accessing through connection String  
asp:SqlDataSource id="srcMovies" SelectCommand="SELECT * FROM Movies"  ConnectionString="" Runat="server" /
The expression is used to represent the connection string.

4. The SqlDataSource control can represent the data that it retrieves in two different ways. It can represent the data using either an ADO.NET DataSet or an ADO.NET DataReader.The advantage of using a DataReader is that it offers significantly better performance than the DataSet object.

5. Handling SQL Command Execution Errors

Whenever you build a software application you need to plan for failure. Databases go down, users enter unexpected values in form fields, and networks get clogged. It is miraculous that the Internet works at all. You can handle errors thrown by the SqlDataSource control by handling any or all of the following four events:

Deleted—Happens immediately after the SqlDataSource executes its delete command.
Inserted—Happens immediately after the SqlDataSource executes its insert command.
Selected—Happens immediately after the SqlDataSource executes its select command.
Updated—Happens immediately after the SqlDataSource executes its delete command.


protected void srcMovies_Selected(object sender, SqlDataSourceStatusEventArgs e)
{
if (e.Exception != null)
{
lblError.Text = e.Exception.Message;
e.ExceptionHandled = true;
}
}

6. Canceling Command Execution

You can cancel SqlDataSource commands when some criterion is not met. For example, you might want to validate the parameters that you are using with the command before executing the command. You can cancel a command by handling any of the following events exposed by the SqlDataSource control:

Deleting—Happens immediately before the SqlDataSource executes its delete command.
Filtering—Happens immediately before the SqlDataSource filters its data.
Inserting—Happens immediately before the SqlDataSource executes its insert command.
Selecting—Happens immediately before the SqlDataSource executes its select command.
Updating—Happens immediately before the SqlDataSource executes its delete command.

No comments: