1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Data Sources in ASP.NET

Discussion in 'ASP.NET' started by MinalS, Aug 15, 2015.

  1. The data source control provides data to the controls without complex coding. The actions like insert, update, delete and sort is possible using the data control. There are various data source controls in ASP.NET. The model allows user to create user data source control which can interact with various data sources.

    The different data sources used in ASP.NET are as mentioned below.
    1. LinqDataSource: The Language Integrated Query in an ASP.NET web page is possible using the markup. The insert, update, select, delete commands are supported. Filtering, sorting and paging of the data are possible.
    2. ObjectDataSource: User can work with the objects using the object data source.
    3. SqlDataSource: User can work with Microsoft SQL Server, Oracle, OLEDB and Oracle databases. The sorting, filtering, paging of the data is possible using the data source.
    4. AccessDataSource: Working with Microsoft Access Database is possible using the data source.
    5. XmlDataSource: User can work with the XML file when the hierarchical structure needs to be used as data source.
    6. SiteMapDataSource: Site navigation data.

    Data Source views



    The Data Source view is the base class for all the source view classes. User can sort, filter, update and delete the data using the data source view.

    Properties of the Data source view control:
    1. CanInsert: A value stating the object is related with the current source object supports the ExecuteInsert operation.
    2. CanDelete: A value stating the object related with the current source object supports the ExecuteDelete operation.
    3. CanSort: State the sorted view for the data source.
    4. CanUpdate: A value stating that the object related with the current source object supports ExecuteUpdate operation.
    5. Events: The event handler delegates for the data source view are listed.
    6. Name: The name of the data source view is displayed.
    Some of the methods of the Data source view control are:
    1. Delete: The asynchronous delete operation on the data for the view object is performed.
    2. Finalize: The object can free the resources and the cleanup operation is performed.
    3. CanExecute: It decides whether the command can be executed
    4. ExecuteSelect: The data from the data source is retrieved
    5. Insert: An asynchronous insert operation is performed on the data represented by the view object
    6. MemberwiseClone: A shallow copy of the current object is created
    7. Update: An asynchronous update is performed on the list of data.
    8. GetHashCode: The default hash function is provided

    ObjectDataSource control



    The ObjectDataSource control is used for connecting the data bound controls to the logic. The control is used in combination with the data bound controls for modifying and displaying the data on a web page.

    User can easily access the data from the business objects using the ObjectDataSource control.

    Some of the properties of the ObjectDataSource control are as listed below:
    1. SelectMethod: The method or the function used for retrieving the data is assigned or accessed.
    2. TypeName: The name of the class is identified
    3. SelectParameters: The parameters collection consisting of the parameters used through the SelectMethod property.
    4. InsertMethod: The method or function is used for inserting the data.
    5. UpdateMethod: The method or function is used for updating the data.
    6. UpdateParameters: The parameters collection used by the UpdateMethod property
    7. DeleteMethod: The method or function is used for deleting the data
    8. DeleteParameters: The parameters collection is used by the DeleteMethod property
    Consider the following code snippet to demonstrate the use of ObjectDataSource control.

    Code:
    
    <asp:objectdatasource id="objectsource1" runat="server" selectmethod="GetResult" typename="StudentInfo">
        <SelectParameters>
            <asp:querystringparameter name="StudID" querystringfield="studid" />
        </SelectParameters>
    </asp:objectdatasource>
    
    In the above snippet, the SelectMethod is used to set the GetResult method. The Typename is used to define the class name. The <SelectParameters> element passes the studid as a parameter.

    SqlDataSource control



    The SqlDataSource control is used to access data from the SQL relational database. The control is used along with the data controls for accessing the data from the relational database. The data can be modified.

    Some of the properties of the SqlDataSource control are as listed below.
    1. ConnectionString: The connection string used for connecting the data source is assigned
    2. ProviderName: The name for the provider used for connecting the data source object is assigned
    3. SelectCommand: The SQL string used for accessing the information from the data source is defined
    4. InsertCommand: Used for inserting the data in the data source object
    5. DeleteCommand: The string used for deleting the data from the data source
    6. UpdateCommand: Used for updating the data in the data source
    7. FilterParameters: Used for filtering the string and parameters
    The following code shows the SqlDataSource control is used for manipulating the data source control.

    Code:
    
    <asp:SqlDataSource ID="SqlSource1" runat="server" 
    ConnectionString="<%$ConnectionStrings:LibraryConnectionString%>"
    SelectCommand="SELECT * FROM Library" 
    UpdateCommand="UPDATE Library SET BOOKNAME = @bname" 
    DeleteCommand = “DELETE FROM Library WHERE BOOKID = @bid" 
    </asp:SqlDataSource>
    
    

    AccessDataSource control



    The AccessDataSource control is used along with the Microsoft Access databases. The SQL queries are used for accessing the data. User can set the location of the Access file (.mdb) in the DataFile property. The database to be accessed must be placed in the App_Data directory. The relative path is used for referencing the data.

    Some of the properties of the AccessDataSource control are as mentioned.
    1. BindingContainer: The control consisting the data binding is defined
    2. CacheDuration: The time in seconds for the data source control is defined
    3. ConnectionString: The connection string used for connecting the Access database
    4. DeleteCommand: The string uses the delete data from the corresponding database.
    5. Events: The list of event handlers as the delegate for the control.
    6. ID: The identifier assigned to the control
    7. InsertCommand: The string used for adding data to the data source control
    8. ProviderName: The .NET data provider used for connecting the Access database
    9. UpdateCommand: The string used for updating the data in the database.
    10. ViewState: The state information used for saving the view state of a server through different requests for the similar page.
    Methods of AccessDataSource control
    1. ApplyStyleSheetSkin: The style properties are defined in the style sheet for the control is defined.
    2. ClearChildControlState: The control state information is removed for the server controls child control
    3. DataBind(): The data source is bounded to the server control.
    4. Delete: The delete operation is performed using the DeleteCommand.
    5. GetType: The type of the current instance is retrieved
    6. MapPathSecure: The physical path to be mapped is retrieved
    7. OpenFile: The Stream is used for reading the file
    Consider the following example to demonstrate the AccessDataSource control.

    Code:
    <asp:AccessDataSource ID="Access1" runat="server" DataFile="~/App_Data/Northwind.mdb" SelectCommand="SELECT StudID, StudName FROM Students" >
    </asp:AccessDataSource>
    

    XmlDataSource control



    The XMLDataSource control states the XML data to the data bound controls. It is used to show data in tabular or hierarchical type. The data is loaded from the XML file specified using the DataFile property.

    Some of the properties of the XmlDataSource control are as listed below.
    1. DataFile: The filename of the XML file used for bounding the data source control is defined.
    2. Controls: The ControlCollection object represents the child controls for the specified server control
    3. Data: The block of xml data used for bounding the XmlDataSource control in string format.
    4. Events: The list of event handler delegates for the control is defined
    5. Parent: The reference to the server controls parent control in the page control structure is defined
    6. TransformFile: The file name of Extensible Stylesheet Language files defining the XSLT transformation.
    7. XPath: An XPath expression to be applied to the XML data present in the Data property.
    Consider the following example to demonstrate the XMLDataSource control in ASP.NET.

    Code:
    
    <asp:XmlDataSource ID="xml1" runat="server" DataFile="student.xml" />
    
    
    In the above code, the datafile defines the XML file used in the control.

    SiteMapDataSource control



    The SiteMapDataSource control is used for navigating the data to the controls. The site map contains the navigation data. It contains data about title, description, URL, location and navigation hierarchy. The menu and tree view controls are used for binding the data.

    Properties of the SiteMapDataSource control are as listed below.
    1. ShowStartingNode: The value stating whether the starting node is accessed or displayed
    2. StartFromCurrentNode: The value states that the site map node tree is accessed from the current page.
    3. SiteMapProvider: The name of the provider is set
    4. StartingNodeUrl: A node in the site map node tree is accessed using the node to show the current page.
    5. StartingNodeOffset: The positive or negative integer offset from the starting node stating the root hierarchy is defined.
    The SiteMapDataSource control in an ASP.NET application is as mentioned below.

    Code:
    <asp:SiteMapDataSource ID="sitemap1" runat="server" SiteMapProvider="EmployeeSiteMap" />
    
     

Share This Page