Source view of OBJECTDATASOURCE.ASPX
DotNet Funda: Code Viewer
objectdatasource.aspx | objectdatasource.aspx.cs
Close Window  
<asp:Content ID="Content1" ContentPlaceHolderID="PlaceHolderHeader" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="PlaceHolderForTitleAndIntro" Runat="Server">
    <table width="100%" cellpadding="2" cellspacing="0">
        <tr valign="top" class="ArticleTitle">
            <td style="padding-left:10px;" valign="middle">
                asp:ObjectDataSource control</td>
        </tr>
        <tr>
            <td class="ArticleContents">
                ObjectDataSource enables user to define their own custom classes to bind data to the controls.
            </td>
        </tr>
        <tr>
            <td colspan="2">&nbsp;</td>
        </tr>
    </table>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="PlaceHolderForContents" Runat="Server">
    <div class="ArticleContents">
        ObjectDataSource enables user to define their own custom classes to bind data to the controls. 
        To use a class as an ObjectDataSource that class must have their own Insert, Delete, Update, and Select method. One important point that must be noted that ObjectDataSource doesn't support batch updation of the data, so the update method must support updation of the record one by one.
        
        <br /><br />        
        <table width="100%" class="TutoPropPlaceHolder" border="1" cellpadding="2" cellspacing="1">
            <tr>
                <th colspan="2" align="left">
                   Some Important Properties of ObjectDataSource Control
                </th>
            </tr>
            <tr>
                <td class="DemoCP">InsertMethod, InsertParameters</td>
                <td>
                    Gets or sets the name of the method and its parameter to perform insert operation.
                </td>
            </tr>
            <tr>
                <td class="DemoCP">UpdateMethod, UpdateParameters</td>
                <td>
                    Gets or sets the name of the method and its parameter to perform update operation.
                </td>
            </tr>
            <tr>
                <td class="DemoCP">DeleteMethod, DeleteParameters</td>
                <td>
                    Gets or sets the name of the method and its parameter to perform delete operation.
                </td>
            </tr>
            <tr>
                <td class="DemoCP">SelectMethod, SelectParameters</td>
                <td>
                    Gets or sets the name of the method and its parameter to perform select operation.
                </td>
            </tr>
            <tr>
                <td class="DemoCP">ConvertNullToDBNull</td>
                <td>
                    true/false. Used to indicate whether null parameter passed to the method should be converted to System.DBNull.
                </td>
            </tr>
            <tr>
                <td class="DemoCP">DataObjectTypeName</td>
                <td>
                    Gets or sets the class name that is used to perform insert, update, delete, and select operation.
                </td>
            </tr>
            <tr>
                <td class="DemoCP">EnablePaging</td>
                <td>
                    true/false. True if paging is supported by the method.
                </td>
            </tr>
            <tr>
                <td class="DemoCP">FilterExpression, FilterParameters</td>
                <td>
                    Gets or sets the filter expression and filter parameter to filter the select operation.
                </td>
            </tr>
            <tr>
                <td class="DemoCP">SelectCountMethod</td>
                <td>
                    Gets or sets the method name that is used to count the selected records (selected by SelectMethod method).
                </td>
            </tr>
            <tr>
                <td class="DemoCP">SortParameterName</td>
                <td>
                    Gets or sets the name of the input parameter used to sort selected record.
                </td>
            </tr>
            <tr>
                <td class="DemoCP">StartRowIndexParameterName</td>
                <td>
                    Works only if EnablePaging=true. Gets or sets the name of the parameter of select method that is used as starting record from where to retrieve records.
                </td>
            </tr>
            <tr>
                <td class="DemoCP">ConvertNullToDBNull</td>
                <td>
                    true/false. Used to indicate whether null parameter passed to the method should be converted to System.DBNull.
                </td>
            </tr>
        </table>
        <!-- START - Demo Section -->
        <table class="DemoPlaceHolder" border="1" cellpadding="2" cellspacing="4">
            <tr>
                <td class="DemoTitle">
                    DEMO : ObjectDataSource
                </td>
                <td align="right">
                    <a class="DemoShowSource" href="../../misc/codeviewer/default.aspx?pagename=~/tutorials/controls/objectdatasource.aspx" target="_blank">Show Source Code</a>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:GridView ID="GridView1" DataSourceID="ObjectDataSource1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None"
                     PageSize="10">
                        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                        <EditRowStyle BackColor="#999999" />
                        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                        <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                        <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                    </asp:GridView>
                    
                    <asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
                      TypeName="ObjectDataSourceSample" SelectMethod="Load">
                      <SelectParameters>
                        <asp:ControlParameter ControlID="txtGreater" PropertyName="Text" Name="greaterThan" Type="int32" />
                      </SelectParameters>
                    </asp:ObjectDataSource>
                    
                </td>
                <td>
                    <b>Parameter of SelectMethod</b> <br />
                    Show record WHERE AutoID greater than <br />
                    <asp:TextBox ID="txtGreater" runat="Server" Text="3" Columns="5"></asp:TextBox>
                    <asp:CompareValidator ID="Cm" runat="server" ControlToValidate="txtGreater" Text="Numeric only" Display="Dynamic" Operator="DataTypeCheck" Type="integer"></asp:CompareValidator>
                    <asp:Button ID="btnSubmit" runat="Server" Text="Submit" />
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <!-- START - Server Side Code -->
                    <pre>                    
// GridView Control ///////////////////////////////
&lt;asp:GridView ID="GridView1" DataSourceID="ObjectDataSource1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None"
 PageSize="10"&gt;
    &lt;FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /&gt;
    &lt;RowStyle BackColor="#F7F6F3" ForeColor="#333333" /&gt;
    &lt;EditRowStyle BackColor="#999999" /&gt;
    &lt;SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" /&gt;
    &lt;PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" /&gt;
    &lt;HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /&gt;
    &lt;AlternatingRowStyle BackColor="White" ForeColor="#284775" /&gt;
&lt;/asp:GridView&gt;


// ObjectDataSource Control ///////////////////////////////
&lt;asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
  TypeName="ObjectDataSourceSample" SelectMethod="Load"&gt;
  &lt;SelectParameters&gt;
    &lt;asp:ControlParameter ControlID="txtGreater" PropertyName="Text" Name="greaterThan" Type="int32" /&gt;
  &lt;/SelectParameters&gt;
&lt;/asp:ObjectDataSource&gt;    
                       

// Parameter of SelectMethod ///////////////////////////////
Show record WHERE AutoID greater than &lt;br /&gt;
&lt;asp:TextBox ID="txtGreater" runat="Server" Text="3" Columns="5"&gt;&lt;/asp:TextBox&gt;
&lt;asp:CompareValidator ID="Cm" runat="server" ControlToValidate="txtGreater" Text="Numeric only" Display="Dynamic" Operator="DataTypeCheck" Type="integer"&gt;&lt;/asp:CompareValidator&gt;
&lt;asp:Button ID="btnSubmit" runat="Server" Text="Submit" /&gt;                       

    
    /// Load records ///////////////////////////////
    public DataSet Load(int greaterThan)
    {
        DataSet dSet = new DataSet();
        string connStr = ConfigurationManager.ConnectionStrings["ConectionString"].ToString();
        using (SqlConnection conn = new SqlConnection(connStr))
        {
            conn.Open();
            
            SqlDataAdapter dAd = new SqlDataAdapter("SELECT * FROM Sample WHERE AutoID > "+ greaterThan +" ORDER BY Name", conn);
            dAd.Fill(dSet, "SampleTable");
        }
        return dSet;
    }            
                    </pre> 
                    <!-- END - Server Side Code -->
                </td>
            </tr>
        </table>
        <!-- END - Demo Section -->
        </div>
    <br />
</asp:Content>
<asp:Content ID="Content4" ContentPlaceHolderID="PlaceHolderFooter" Runat="Server">
</asp:Content>

Go Top