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











July 24th, 2006 at 8:23 am
good
July 25th, 2006 at 9:24 am
Hi,
Thanks for this. It saved me a lot of time.
One question though, is it possible to use transactions w/ ObjectDataSource ?
Thanks
October 12th, 2006 at 11:21 pm
Very nice method.It saves a lot of time.
November 9th, 2006 at 5:22 am
Peter,
you just got another plug in my latest article. Thanks for the great stuff you’ve been putting out.
Peter
February 12th, 2007 at 11:53 am
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
June 29th, 2007 at 1:18 pm
Hi,
How can I do an InsertParametes[...] to a Int32 parameter?
Thanks
July 11th, 2008 at 3:16 pm
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.
July 12th, 2008 at 3:40 am
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.