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
17678    
17679    
17680etwettetew897894654surat
17650ewrrPhilippinesewrwrwew
17675ffff878555555yyy
17652HasimKadi8989909012Kadi
17673hkhkhkhkhkhkhkhk
17637John Do111 Clarion dr.213-333-3333Torrancd
17657jujtryrtyrrtyty
17664kemmy kalariaGol residency ,nana muva ,main road rajkot09426977487Rajkot
17647Kong Yoon Thim23, Jalan SS8/8+60122610084Petaling Jaya
17648Kong Yoon Thim23, Jalan SS8/8+60122610084Petaling Jaya
17668lukasz Michalak1 maja 59/40533792647lubartów
17671NameAddressPhoneCity
17672NameAddressPhoneCity
17642sdsdf2422442wer
17658ssseee4555nagpur
17645tt4453t
17638testtesttesttest
17639testtest9999999Singapore
17662Test 1002 Central Parkway South210-444-5655San Diego
17670Try1#102, Above GK Provision Store9972412313Bangalore
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;
    }