( 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 <script runat="server"> protected void Button1_Click(object sender, EventArgs e) { ObjectDataSource1.InsertParameters["roleName"].DefaultValue = TextBoxRole.Text; ObjectDataSource1.Insert(); } </script> <html xmlns=""> <head runat="server"> <title>AddRoleWithODS.aspx</title> </head> <body> <form id="form1" runat="server"> <asp :ObjectDataSource <insertparameters> <asp :Parameter </insertparameters> </asp> <asp :Button <asp :TextBox </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); } }
I agree to your idea that you refer to. Here is the website, I think you may love it.
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.
Very good thanks!
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.
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.
Hi,
How can I do an InsertParametes[…] to a Int32 parameter?
Thanks
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
Peter,
you just got another plug in my latest article. Thanks for the great stuff you’ve been putting out.
Peter
Very nice method.It saves a lot of time.
Hi,
Thanks for this. It saved me a lot of time.
One question though, is it possible to use transactions w/ ObjectDataSource ?
Thanks