Inserting Programmatically with ObjectDataSource in ASP.NET 2.0

( ObjectDataSource1.Insert(); )

ObjectDataSource’s are great for building your own middle tier between your aspx web page and your database (or any other datastore for that matter). By binding the datasource to a databound server control like detailsview, you auto-magically get the insert behavior you are looking for. If however, you just just want to insert to the ObjectDataSource you have included on your aspx page without using a databound control, you don’t have a lot of fancy footwork to do. All you have to do is reference the insert parameter by name (or index offset) and assign it directly.

Below is an example of how to insert a Role into Membership using the ObjectDataSource developed in a my previous MSDN article Microsoft ASP.NET 2.0 Member/Role Management with IIS
Part 2: Implementation

< %@ Page Language="C#" %>
< !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<script runat="server">
protected void Button1_Click(object sender, EventArgs e)
  ObjectDataSource1.InsertParameters["roleName"].DefaultValue =
<html xmlns="">
<head runat="server">
<form id="form1" runat="server">
  <asp :ObjectDataSource ID="ObjectDataSource1" runat="server"
      <asp :Parameter Name="roleName" Type="String" />
  <asp :Button ID="Button1" runat="server" Text="Add Role"
    OnClick="Button1_Click" />
  <asp :TextBox ID="TextBoxRole" runat="server">

If you do not want to declaratively define the ObjectDataSource and just want to use it directly, you can do that also. You simply reference the ObjectDataSource by Type and call its insert method. An example of that is below.

using System;
using System.Web.UI;
using MembershipUtilities;
public partial class Default3 : Page
  protected void Button1_Click(object sender, EventArgs e)
About Peter Kellner

Follow me:


  1. I agree to your idea that you refer to. Here is the website, I think you may love it.

  2. have been searching for some information about it almost three hours. You helped me a lot indeed and reading this your article I have found many new and useful information about this subject.

  3. Very good thanks!

  4. Peter Kellner says:

    Thanks for the comment Anas, You make a good point. There are lots of ways to do this. If you are a wizard with ObjectDataSource, it may be easier to use then other methods. it’s nice to have alternatives.

  5. I believe its not requires at all.
    why someone needs to use the object data source without the data bound controls ? actually its easier and better to directly call the class methods without object data source like this

    I would never use the object data source separately , its useful with the gridview, detailsview … but not in this case … it makes things more complex to use the object data source here.

  6. Hi,

    How can I do an InsertParametes[…] to a Int32 parameter?


  7. Peter, thanks a bunch!! You are a genius. I have been pounding on this for hours but couldn’t get the data from a gridview into my insert statement until now.
    Take care.

  8. Peter,
    you just got another plug in my latest article. Thanks for the great stuff you’ve been putting out.

  9. Nilesh Surve says:

    Very nice method.It saves a lot of time.

  10. Hi,
    Thanks for this. It saved me a lot of time.
    One question though, is it possible to use transactions w/ ObjectDataSource ?


Get every new post delivered to your Inbox

Join other followers: