Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pre-populating an ObjectDataSource object before insert

Status
Not open for further replies.

JanVolley

Programmer
Sep 8, 2008
14
US
I'm using asp.net 2.0 and C#. I have a data entry screen. When the user clicks the 'Save' button, a record is saved to the database. I'm using an ObjectDataSource in my form as follows:

<asp:ObjectDataSource ID="CourseProfileDataSource" runat="server" DataObjectTypeName="CourseProfile" InsertMethod="InsertCourseProfile" SelectMethod="GetCourseProfile" TypeName="ISDFormsDBManager" OnObjectCreating="CourseProfileDataSource_ObjectCreating" OnInserted="CourseProfileDataSource_Inserted">

<InsertParameters>

<asp:parameter Direction="ReturnValue" Name="CourseProfileID" Type="Int32" />

</InsertParameters>

</asp:ObjectDataSource>

The InsertMethod takes the CourseProfile object and saves it to the database via a stored procedure. What I'm trying to do is to pre-populate some of the CourseProfile attributes prior to the InsertMethod execution, so I've created an OnObjectCreating method as follows to create and initialize the CourseProfile object:

protected void CourseProfileDataSource_ObjectCreating(object sender, ObjectDataSourceEventArgs e)

{

CourseProfile courseProfile = new CourseProfile();

CheckBoxList cb = (CheckBoxList)CourseProfileFormView.FindControl("TrainingLocationCheckBoxList");
List<int> locationList = new List<int>();

for (int i = 0; i < cb.Items.Count; i++)
{

if (cb.Items.Selected)
{

int j = Int32.Parse(cb.Items.Value);
locationList.Add(j);

locationList.Add(Int32.Parse(cb.Items.Value));
courseProfile.CourseLocationList = locationList;

}

}

e.ObjectInstance = courseProfile;

}

When I run the app, the CourseProfileDataSource_ObjectCreating method executes first and creates my CourseProfile object, but the InsertMethod never executes for some reason. Instead, that seems to be bypassed and it goes straight to the CourseProfileDataSource_Inserted method instead.

If I comment out the line:

e.ObjectInstance = courseProfile;

in the CourseProfileDataSource_ObjectCreating method, the InsertMethod executes and saves a record fine without my pre-initialization values.

What am I doing wrong? Do I need to execute the InsertMethod myself manually somehow?



Thanks for any help,
Jan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top