Search
Author
ASP.NET Tutorials
Author
Sheo Narayan
Advertisements


Winners

Win Prizes

Social Presence
Like us on Facebook

Silverlight Tutorials | Report a Bug in the Tutorial
asp:ObjectDataSource control
ObjectDataSource enables user to define their own custom classes to bind data to the controls.
 
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.

Some Important Properties of ObjectDataSource Control
InsertMethod, InsertParameters Gets or sets the name of the method and its parameter to perform insert operation.
UpdateMethod, UpdateParameters Gets or sets the name of the method and its parameter to perform update operation.
DeleteMethod, DeleteParameters Gets or sets the name of the method and its parameter to perform delete operation.
SelectMethod, SelectParameters Gets or sets the name of the method and its parameter to perform select operation.
ConvertNullToDBNull true/false. Used to indicate whether null parameter passed to the method should be converted to System.DBNull.
DataObjectTypeName Gets or sets the class name that is used to perform insert, update, delete, and select operation.
EnablePaging true/false. True if paging is supported by the method.
FilterExpression, FilterParameters Gets or sets the filter expression and filter parameter to filter the select operation.
SelectCountMethod Gets or sets the method name that is used to count the selected records (selected by SelectMethod method).
SortParameterName Gets or sets the name of the input parameter used to sort selected record.
StartRowIndexParameterName 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.
ConvertNullToDBNull true/false. Used to indicate whether null parameter passed to the method should be converted to System.DBNull.
DEMO : ObjectDataSource Show Source Code
AutoIDNameAddressPhoneCity
17297kalaichennai987646693040chennai
17298yyyasdd1246add
Parameter of SelectMethod
Show record WHERE AutoID greater than
                    
// GridView Control ///////////////////////////////
<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>


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

// Parameter of SelectMethod ///////////////////////////////
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" />                       

    
    /// 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;
    }