New to Telerik UI for ASP.NET AJAX? Start a free 30-day trial
Configure ObjectDataSource for CRUD operations
ObjectDataSource Configuration
Add an ObjectDataSource component to the Page and configure the following properties:
TypeName
- Set the Fully Qualified Name of the Type of the Custom object containing the methods for Selecting, Inserting, Updating, Deleting.SelectMethod
- Set the name of the method to be called for selecting records.InsertMethod
- Set the name of the method to be called for inserting records.UpdateMethod
- Set the name of the method to be called for updating records.DeleteMethod
- Set the name of the method to be called for deleting records.
For security purposes, it is recommended to add parameters for every method that takes
@parameters
.
SelectParameters
InsertParameters
UpdateParameters
DeleteParameters
Example
ASP.NET
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
TypeName="MyNamespace.MyObject"
SelectMethod="GetCustomers"
InsertMethod="InsertCustomer"
UpdateMethod="UpdateCustomer"
DeleteMethod="DeleteCustomer">
<InsertParameters>
<asp:Parameter Name="ContactName" Type="String" />
<asp:Parameter Name="Country" Type="String" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="CustomerID" Type="Int32" />
<asp:Parameter Name="ContactName" Type="String" />
<asp:Parameter Name="Country" Type="String" />
</UpdateParameters>
<DeleteParameters>
<asp:Parameter Name="CustomerID" Type="Int32" />
</DeleteParameters>
</asp:ObjectDataSource>
MyObject Class
C#
namespace MyNamespace
{
public class MyObject
{
private const string SESSION_KEY = "MySessionKey";
System.Web.SessionState.HttpSessionState Session
{
get
{
return System.Web.HttpContext.Current.Session;
}
}
private List<Customer> _customers
{
get
{
List<Customer> customersList = Session[SESSION_KEY] as List<Customer>;
if (customersList == null)
{
customersList = new List<Customer>()
{
new Customer(){ CustomerID = 1, ContactName = "John", Country="USA"},
new Customer(){ CustomerID = 2, ContactName = "Dane", Country="USA"},
new Customer(){ CustomerID = 3, ContactName = "Rajesh", Country="India"},
new Customer(){ CustomerID = 4, ContactName = "Brooke", Country="Canada"},
};
Session[SESSION_KEY] = customersList;
}
return customersList;
}
set
{
Session[SESSION_KEY] = value;
}
}
public List<Customer> GetCustomers()
{
return _customers;
}
public void InsertCustomer(string ContactName, string Country)
{
int CustomerID = _customers.Count == 0 ? 1 : _customers.Max(c => c.CustomerID) + 1;
_customers.Add(new Customer()
{
CustomerID = CustomerID,
ContactName = ContactName,
Country = Country
});
}
public void UpdateCustomer(int CustomerID, string ContactName, string Country)
{
Customer customerToUpdate = _customers.Where(c => c.CustomerID == CustomerID).FirstOrDefault();
if (customerToUpdate != null)
{
customerToUpdate.ContactName = ContactName;
customerToUpdate.Country = Country;
}
}
public void DeleteCustomer(int CustomerID)
{
Customer customerToDelete = _customers.Where(c => c.CustomerID == CustomerID).FirstOrDefault();
if (customerToDelete != null)
{
_customers.Remove(customerToDelete);
}
}
}
public class Customer
{
public int CustomerID { get; set; }
public string ContactName { get; set; }
public string Country { get; set; }
}
}