Inserting Programmatically with ObjectDataSource in ASP.NET 2.0
Wednesday 7 June 2006 @ 1:21 pm

Inserting Programmatically with ObjectDataSource

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” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
 
<script runat=“server”>
protected void Button1_Click(object sender, EventArgs e)
{
  ObjectDataSource1.InsertParameters[“roleName”].DefaultValue = 
    TextBoxRole.Text;
  ObjectDataSource1.Insert();
}
</script>
 
<html xmlns=“http://www.w3.org/1999/xhtml”>
<head runat=“server”>
    <title>AddRoleWithODS.aspx</title>
</head>
<body>
<form id=“form1″ runat=“server”>
  <asp :ObjectDataSource ID=“ObjectDataSource1″ runat=“server” 
    InsertMethod=“Insert”
    SelectMethod=“GetRoles” 
    TypeName=“MembershipUtilities.RoleDataObject”>
    <insertparameters>
      <asp :Parameter Name=“roleName” Type=“String” />
    </insertparameters>
  </asp>
  <asp :Button ID=“Button1″ runat=“server” Text=“Add Role” 
    OnClick=“Button1_Click” />
  <asp :TextBox ID=“TextBoxRole” runat=“server”>
    </asp>
</form>
</body>
</html>

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)
  {
    RoleDataObject.Insert(TextBox1.Text);
  }
}




8 Responses to “Inserting Programmatically with ObjectDataSource in ASP.NET 2.0”

  1. alvaro Windows XP Mozilla Firefox 1.5.0.4 Says:

    good

  2. quanou Windows XP Internet Explorer 6.0 Says:

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

  3. Nilesh Surve Windows XP Internet Explorer 7.0 Says:

    Very nice method.It saves a lot of time.

  4. Peter Bromberg Windows Server 2003 Internet Explorer 7.0 Says:

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

  5. Mark F Windows XP Internet Explorer 6.0 Says:

    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.
    Mark

  6. Pierre Windows XP Internet Explorer 6.0 Says:

    Hi,

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

    Thanks

  7. Anas Ghanem Windows XP Mozilla Firefox 2.0.0.15 Says:

    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
    MembershipUtilities.RoleDataObject.Insert(….

    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.

  8. Peter Kellner Windows Vista Internet Explorer 7.0 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.

Leave a Reply