Monday, January 09, 2006
« .NET Symmetric Encryption | Main | Emailing Exceptions from ASP.NET »

I came up with a slightly different way of handling the insert code in my previous "Insert Rows with a GridView" posting. Instead of calling my data layer directly from my code behind page, I pass the values to my ObjectDataSource control and allow it to handle the inserts. Not a major change but a bit more consistent in that the ODS handles all of my CRUD logic and I never call my data layer directly.

My previous post:
http://blog.binaryocean.com/PermaLink,guid,eca3c5a1-08c1-4226-bfb5-d36fddaef93b.aspx

The complete Solution for this modified version:
http://download.binaryocean.com/GridViewInsertSolution2.zip

Make sure to wire up the OnInserting event in the ODS control:
OnInserting="ObjectDataSourceMain_Inserting"

protected void GridViewMain_RowCommand(object sender, GridViewCommandEventArgs e)

{

    //  handle the save button on the footer row.

    if (e.CommandName == "Save")

    {

        ObjectDataSourceMain.Insert();      

    }

}

 

protected void ObjectDataSourceMain_Inserting(object sender, ObjectDataSourceMethodEventArgs e)

{

    //  on insert, pass the values from the footer controls.

    e.InputParameters["LocationCode"] = ((TextBox)GridViewMain.FooterRow.FindControl("TextBoxCode")).Text;

    e.InputParameters["LocationDescription"] = ((TextBox)GridViewMain.FooterRow.FindControl("TextBoxDescription")).Text;

    e.InputParameters["LocationIsActive"] = ((CheckBox)GridViewMain.FooterRow.FindControl("CheckBoxIsActive")).Checked;

}

 

kick it on DotNetKicks.com   Monday, January 09, 2006 8:42:19 AM (Pacific Standard Time, UTC-08:00)  #    Disclaimer  |  Comments [6]  | 
Tuesday, July 25, 2006 2:36:35 PM (Pacific Standard Time, UTC-08:00)
thanks for this. Well done.
Thursday, July 12, 2007 9:06:05 AM (Pacific Standard Time, UTC-08:00)
This is super and works well. But how could this be used to just refresh the gridview but not actually submit to the database until the submit button for the page is pressed?
Sherri Scalish
Wednesday, August 01, 2007 12:40:36 PM (Pacific Standard Time, UTC-08:00)
Thanks for this,

Have a question though, I'm trying to modify your solution so that I can pass a connectstring to it instead of having it hardcoded. Any ideas on how to do this?
Thai
Wednesday, February 20, 2008 11:10:02 AM (Pacific Standard Time, UTC-08:00)
This was great -- THANKS!!

One thing... I'm using a combination of BAL/DAL, and am typically binding my ObjectDataSource to methods that accept whole, populated objects, not individual parameters for each column being saved. Your code describes assigning the InputParameters right before the insertion, but this doesn't seem to work for whole objects.

I've modified your code a bit as follows, in case someone else has the same issue...

protected void GridViewMain_RowCommand(object sender, GridViewCommandEventArgs e)
{
// handle the save button on the footer row
if (e.CommandName.ToLower() == "save")
{
LocationData newObj = new LocationData();
newObj.LocationCode =
((TextBox)GridViewMain.FooterRow.FindControl("TextBoxCode")).Text;
newObj.LocationDescription =
((TextBox)GridViewMain.FooterRow.FindControl("TextBoxDescription")).Text;
newObj.LocationIsActive =
((CheckBox)GridViewMain.FooterRow.FindControl("CheckBoxIsActive")).Text;

try
{
newObj.InsertLocationData(newObj);
}
catch(System.Data.SqlClient.SqlException se)
{
// perhaps primary key violation?, display error to user

Server.ClearError();
}
catch(Exception ex)
{
throw ex;
}

GridViewMain.DataBind(); //rebind to show the insertion
}
}

{also, no longer need the ObjectDataSourceMain_Inserting() method, or references to the "Insert..." attributes on the ObjectDataSource object}
Friday, April 03, 2009 1:55:09 PM (Pacific Standard Time, UTC-08:00)
This is exactly what I'm looking for, but I'm using ASP.NET-VB, so I have a problem when placing LocationData.cs & SqlData.ca in the App_Code folder. I tried creating an App_LocalResources folder within the "time" folder where I'm testing this -> my plan is to use the grid for an online timesheet.

Any suggestions on how I can alter the setup to use your solution? I'm afraid I don't yet know enough to debug myself.

Thanks.

Janice
JC
Friday, May 29, 2009 12:42:10 AM (Pacific Standard Time, UTC-08:00)
I am also looking for this handling for a long time. But I am using ASP.NET -VB and datasource is SQLDatatsource. Can I stick with SQLDatabase, not changing to ObjectDataSource. It is great that if
you can create another version using SQLDataSource and VB.NET

Thanks in advance

Simon
Simon
Name
E-mail
Home page

Comment (Some html is allowed: a@href@title, strike) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.  

Enter the code shown (prevents robots):

Live Comment Preview