Odd ObjectDataSource Issue with Generic List of objects

  • Thread starter Thread starter fig000
  • Start date Start date
F

fig000

Hi,

I am new to C# and ObjectDataSources and have encountered what I
think is a strange problem. I have a web page that has a formview on
it. The formview is set to insert mode in the page load. The
ObjectDataSource for the form has a procedure for inserting. Ths
procedure adds the new record to the database using a tableadapter but
it also adds the record to a generic list of business objects which
has been placed into a session variable earlier in the life of the
program.

What is happening is that when I add the new record to the list it
fires the ObjectDataSource's inserted event. This messes everything
else up that comes after it; the program returns to the insert proc
and the new business object I've that I tried to add to the list no
longer is instantiated and has no value.

If I take out the add to the list everything is fine and I can
retreive values from OutputParameters and returnvalue. My question is:
Why would adding a record to a generic list fire the
ObjectDataSource's inserted event? The select procedure (which I
assume is not fired in this case) binds a datatable converted from the
same generic list of business objects (not the list itself) so I can't
see how the changing this generic list would have any connection to
the ObjectDataSource.

Please forgive the redunancy of passing the new record id back
through both OutputParameters and returnvalue; this was part of my
experimentation.

Anyway, any help would be appreciated. Here is the Insert Proc:

public int InsertDevIssue(DateTime EntryDate, int EnteredBy, string
Title, string RequestedBy, int Type, int Area, int Priority, int
Status, int ModifiedBy, DateTime ModifiedDate, int AssignedTo, out int
NewID)
{

DevissuesTableAdapters.devissueTableAdapter DevIssuesTblAdp =
new DevissuesTableAdapters.devissueTableAdapter();
Devissues.devissueDataTable DevIssuesTbl = new
Devissues.devissueDataTable();
Devissues.devissueRow dr = DevIssuesTbl.NewdevissueRow();
dr.EntryDate = EntryDate;
dr.EnteredBy = EnteredBy;
dr.Title = Title;
dr.RequestedBy = RequestedBy;
dr.Type = Type;
dr.Area = Area;
dr.Priority = Priority;
dr.Status = Status;
dr.ModifiedBy = ModifiedBy;
dr.ModifiedDate = ModifiedDate;
dr.AssignedTo = AssignedTo;
DevIssuesTbl.AdddevissueRow(dr);

DevIssuesTblAdp.Update(DevIssuesTbl);
NewID = dr.IssueID;


List<clsDevIssues> DevissuesList =
(List<clsDevIssues>)System.Web.HttpContext.Current.Session["clsDevIssues"];
BusinessObjectsGeneric<clsDevIssues> BOG = new
BusinessObjectsGeneric<clsDevIssues>();
clsDevIssues newDevIssues = new clsDevIssues();

BOG.UpdateObjectFromDB(newDevIssues, dr); //This fills the new
object with the data
DevissuesList.Add(newDevIssues); // This fires the ODS
inserted event prematurely

System.Web.HttpContext.Current.Session["clsDevIssues"] =
DevissuesList;


return dr.IssueID;

}

Thanks,
Fig000
 
What is happening is that when I add the new record to the list it
fires the ObjectDataSource's inserted event.

You know when you are adding a new record. So you create a global bool flag
like bOnAdd and set the flag to true when you add a new record.

In the event, you check the bOnAdd.

if (!bOnAdd)
{
populate data or whatever
)

You set the bOnAdd to false when you're finished with the add.

That way, when the event is fired, it won't execute code when doing the add,
because you have set bOnAdd to true. The event only executes code when
bOnAdd is false.
 
Mr. Arnold,

Thanks for answering so soon. This is a weird problem. I tried your
idea but what is happening is that the event is being fired before it
should, set off by the line:

DevissuesList.Add(newDevIssues);

As a result the object newDevIssues, which is filled with data
before the call (I checked in the debugger) is null when the code
returns from the prematurely fired inserted event. That causes an
error because it thinks I'm trying to add a null object to the list.
This ends processing and the code after that line never gets executed.
I tried removing the line and everything was fine but that meant I
couldn't add the new record to the generic list. I don't know how but
somehow the addition of an instiated business object to a generic list
seems to be setting off the insert event of the objectdatasource. That
event, in turn, seems to be interfering with the addition of the
object because the object seems to lose state when the code returns
from the inserted event to the objectdatasource's insert procedure.

Any ideas anyone? The code is in my first post in this thread (I
don't want to bury anyone with additional text).

Thanks,
Fig000
 
For anyone who has run into this problem: I wasn't able to find out
why it was happening but I did find a workaround. At first I created a
new empty list in the same block of code and added a new record to it
instead of the list taken from the session variable. I did not get
sent to the inserted event of the objectdatasource as I did before. So
there was some connection between the objectdatasource and the generic
list I kept in session state. This is for greater minds than mine to
comment on. :-)

My workaround was to take all code having to with the generic list out
of the insert procedure used by the objectdatasource (code in first
part of this thread). Instead I had that procedure pass the new
record as an instance of the object to the objectdatasource inserted
event using the outputparmeters. Once in the inserted event I was able
to add the record to the generic list that was being kept in a session
variable. No problems.

Of course now I'm curious as to why I had that problem...

Code follows:

public void InsertDevIssue(DateTime EntryDate, int EnteredBy, string
Title, string RequestedBy, int Type, int Area, int Priority, int
Status, int ModifiedBy, DateTime ModifiedDate, int AssignedTo, out int
NewID, out clsDevIssues InsertedObject)
{
//List<clsDevIssues> DevissuesList= new List<clsDevIssues>();

DevissuesTableAdapters.devissueTableAdapter DevIssuesTblAdp =
new DevissuesTableAdapters.devissueTableAdapter();
Devissues.devissueDataTable DevIssuesTbl = new
Devissues.devissueDataTable();
Devissues.devissueRow dr = DevIssuesTbl.NewdevissueRow();
dr.EntryDate = EntryDate;
dr.EnteredBy = EnteredBy;
dr.Title = Title;
dr.RequestedBy = RequestedBy;
dr.Type = Type;
dr.Area = Area;
dr.Priority = Priority;
dr.Status = Status;
dr.ModifiedBy = ModifiedBy;
dr.ModifiedDate = ModifiedDate;
dr.AssignedTo = AssignedTo;
DevIssuesTbl.AdddevissueRow(dr);

DevIssuesTblAdp.Update(DevIssuesTbl);
NewID = dr.IssueID;

// List<clsDevIssues> DevissuesList= new List<clsDevIssues>();
BusinessObjectsGeneric<clsDevIssues> BOG = new
BusinessObjectsGeneric<clsDevIssues>();
clsDevIssues newDevIssues = new clsDevIssues();
BOG.UpdateObjectFromDB(newDevIssues, dr);
//List<clsDevIssues> DevissuesList =
(List<clsDevIssues>)System.Web.HttpContext.Current.Session["clsDevIssues"];
//DevissuesList.Add(newDevIssues);
//System.Web.HttpContext.Current.Session["clsDevIssues"] =
DevissuesList;
InsertedObject= newDevIssues;

}


protected void ObjectDataSource1_Inserted(object sender,
ObjectDataSourceStatusEventArgs e)
{
List<clsDevIssues> DevissuesList =
(List<clsDevIssues>)System.Web.HttpContext.Current.Session["clsDevIssues"];

DevissuesList.Add((clsDevIssues)e.OutputParameters["InsertedObject"]);
System.Web.HttpContext.Current.Session["clsDevIssues"] =
DevissuesList;
Response.Redirect("DevIssueEdit.aspx?IssueID=" +
e.OutputParameters["NewID"])

}

Fig000
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top