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