Implement a DataSource Control
Implement a DataSource control
We’ll look into the details of the following DataSource controls:
These controls lets you easily bind data to your data-bound controls. You can query, update, delete data sources declaratively with the data source controls. Now let’s take a closer look to these controls:
This data source control is designed to work with Microsoft Access databases. To connect to an Access database, all you need to do is to specify the absolute or relative path to the given .mdb file. No connection strings are required (neither available). The drawback is that you cannot connect to databases protected with passwords, because you have no place to include them. So when you need to connect to an Access database protected with password, use SqlDataSource instead.
Because AccessDataSource always use the predefined OleDb provider, the order of parameters must match their order in the query. AccessDataSource, as a child control of SqlDataSource also supports caching data, with the exact same properties as SqlDataSource.
The following data operations work with the AccessDataSource:
|Sorting||Works when you set the DataSourceMode property to DataSet|
|Filtering||Use the FilterExpression property|
|Paging||Paging isn’t supported directly. However, when you set DataSourceMode to DataSet, you can use paging behavior with data-bound controls which support it.|
|Updating||Set the UpdateCommand|
|Deleting||Set the DeleteCommand|
|Inserting||Set the InsertCommand|
|Caching||Set the EnableCaching property to true.|
LinqDataSource enables you to query an ORM data object (generated for example with sqlmetal.exe) without writing SQL commands. To connect to an ORM object, you need to specify two properties: ContextTypeName to the class that contains the data collection and the TableName to a field or property that returns the collection.
To enable data modification, you set the appropriate EnableDelete /EnableInsert /EnableUpdate properties to true or false, no custom SQL command is required.
With ObjectDataSource you can connect to a middle-tier object or a data interface in multitier applications. To do so, you need to specify the name of your data object to the TypeName property. It is a good habit to mark these objects with the [DataObject] attribute (in System.ComponentModel), so Visual Studio can recognize them. Selecting of data is working with the SelectMethod. Here you should define the name of a property or method in your class which returns IEnumerable data or a DataSet. Also don’t forget to mark this property/method with a [DataObjectMethod] attribute.
You can also specify parameters for parameterized methods, either declaratively or programmatically. When you are sorting data, you should use the SortParameterName to specify the parameter which deals with sorting in your select method.
You can set a FilterExpression and its FilterParameters to filter data. In this case, your method must return IEnumerable or DataSet.
SqlDataSource can connect to relational databases. You set up a connection string to the given database, and write the SQL commands appropriately. Stored procedures can also be used with SqlDataSource.
By default when one of your select parameters returns a null value, nothing will be selected from the database and no exception will be thrown. To overcome this issue, you have two scenarios:
- Set the given parameter’s ConvertEmptyStringToNull property to false.
- Set the SqlDataSource’s CancelSelectOnNullParameter property to false.
XmlDataSource is typically used to display read-only hierarchical data. There are two ways to bind XML data to this control: set the DataFile property to the path of the XML file, or write the XML inside the control’s Data template. You can use XSLT files on the given XML by the exact same way: specify the TransformFile property, or the Transform template.
You may use XmlDataSource to edit XML documents, but only programmatically, by calling the GetXmlDocument method, then the Save method. In this case, you cannot use the templates to load XML or XSLT data.
When you want to filter your data, you should use the XPath property with a valid XPath syntax for filtering. Sorting is not available, but there’s caching by default.