Monday, 25 March 2013

ObjectDataSource in asp.net

ASP.NET 2.0 ships with five built-in data source controls – SqlDataSourceAccessDataSourceObjectDataSourceXmlDataSource, and SiteMapDataSource – although you can build your own custom data source controls, if needed. Since we have developed an architecture for our tutorial application, we'll be using the ObjectDataSource against our BLL classes.





Step 1: Adding and Configuring the ObjectDataSource Control

Start by opening the SimpleDisplay.aspx page in the BasicReporting folder, switch to Design view, and then drag an ObjectDataSource control from the Toolbox onto the page's design surface. The ObjectDataSource appears as a gray box on the design surface because it does not produce any markup; it simply accesses data by invoking a method from a specified object. The data returned by an ObjectDataSource can be displayed by a data Web control, such as the GridView, DetailsView, FormView, and so on.
Note   Alternatively, you may first add the data Web control to the page and then, from its smart tag, choose the <New data source> option from the drop-down list.
To specify the ObjectDataSource's underlying object and how that object's methods map to the ObjectDataSource's, click on the Configure Data Source link from the ObjectDataSource's smart tag.
By coding 

   <asp:GridView ID="gvMainGrp" runat="server" AutoGenerateColumns="False" AutoGenerateSelectButton="True"           
                        AllowPaging="True" PageSize="5" CssClass="GridviewTable" RowStyle-Height="25"
                        AutoGenerateEditButton="True"
                        AutoGenerateDeleteButton="True"
                        RowStyle-Width="30" RowStyle-HorizontalAlign="Center" 
                        DataSourceID="ObjectDataSource1" AllowSorting="True" CellPadding="4" 
                        ForeColor="#333333" GridLines="None">
                        <Columns>
                            <asp:BoundField DataField ="process_Cd" HeaderText="cd"/>
                            <asp:BoundField DataField="description" HeaderText="Desc" />
                        </Columns>
                  <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                        <RowStyle BackColor="#EFF3FB" HorizontalAlign="Left" />
                        <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                        <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                        <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                        <EditRowStyle BackColor="#2461BF" />
                        <AlternatingRowStyle BackColor="White" />
                        <RowStyle HorizontalAlign="Center" Height="25px" Width="30px"></RowStyle>
                    </asp:GridView>
                    <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" 
                         DeleteMethod ="deleteProcessDetails" UpdateMethod="updateProcessDetails"
                        SelectMethod="getProcessDetails" TypeName="BusinessLayer.Product.processBiz">
                        <DeleteParameters>
                            <asp:Parameter Name="process_cd" Type="Int32" />
                        </DeleteParameters>
                        <UpdateParameters>
                            <asp:Parameter Name="process_cd" Type="Int32" />
                            <asp:Parameter Name="description" Type="String" />
                        </UpdateParameters>
                    </asp:ObjectDataSource>
                </td>


public DataTable getProcessDetails()
        {
            DataTable dtDetail = new DataTable();
            sqlDataaccess = new DataLayer.SQLDataAccess();
            System.Text.StringBuilder strQuery = new System.Text.StringBuilder();
            strQuery.Append("SELECT [Process_Cd],[description] FROM [abc].[dbo].[Temp_Process]");
            return sqlDataaccess.getDataTable(strQuery.ToString());
        }

        public void deleteProcessDetails(int process_cd)
        {
            DataTable dtDetail = new DataTable();
            sqlDataaccess = new DataLayer.SQLDataAccess();
            System.Text.StringBuilder strQuery = new System.Text.StringBuilder();
            strQuery.Append("delete FROM [abc].[dbo].[Temp_Process] where [Process_Cd]='" + process_cd + "'");
            sqlDataaccess.ExecuteNonQuery(strQuery.ToString());
        }

        public void updateProcessDetails(int process_cd, string description)
        {
            DataTable dtDetail = new DataTable();
            sqlDataaccess = new DataLayer.SQLDataAccess();
            System.Text.StringBuilder strQuery = new System.Text.StringBuilder();
            strQuery.Append("update  [abc].[dbo].[Temp_Process] set [description]='" + description + "' where [Process_Cd]='" + process_cd + "'");
            sqlDataaccess.ExecuteNonQuery(strQuery.ToString());
        }



Enjoy... check it....





Configure the ObjectDataSource Manually

The ObjectDataSource's Configure Data Source wizard offers a quick way to specify the object it uses and to associate what methods of the object are invoked. You can, however, configure the ObjectDataSource through its properties, either through the Properties window or directly in the declarative markup. Simply set the TypeNameproperty to the type of the underlying object to be used, and the SelectMethod to the method to invoke when retrieving data.
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" 
        SelectMethod="GetProducts"
        TypeName="ProductsBLL">
</asp:ObjectDataSource>
Even if you prefer the Configure Data Source wizard there may be times when you need to manually configure the ObjectDataSource, as the wizard only lists developer-created classes. If you want to bind the ObjectDataSource to a class in the .NET Framework – such as the Membership class, to access user account information, or the Directory classto work with file system information – you'll need to manually set the ObjectDataSource's properties.

Step 2: Adding a Data Web Control and Binding it to the ObjectDataSource

Once the ObjectDataSource has been added to the page and configured, we're ready to add data Web controls to the page to display the data returned by the ObjectDataSource's Select method. Any data Web control can be bound to an ObjectDataSource; let's look at displaying the ObjectDataSource's data in a GridView, DetailsView, and FormView.

Binding a GridView to the ObjectDataSource

Add a GridView control from the Toolbox to SimpleDisplay.aspx's design surface. From the GridView's smart tag, choose the ObjectDataSource control we added in Step 1. This will automatically create a BoundField in the GridView for each property returned by the data from the ObjectDataSource's Select method (namely, the properties defined by the Products DataTable).






No comments:

Post a Comment