This row already belongs to another table

R

rmontgomery429

Hello all,
I am using strongly typed datasets for my data access. They work
fine except for when I try to insert a new row in a table to get the
next unique id. I have a table for emails. It has columns like
email_id, subject, etc... I have a data table associated with that
table and 95% of the time i have no problem. However sometimes when I
run the following code i get the 'This row already belongs to another
table' exception. Please help.

The error information is as follows:
--Exception information:

Exception type: System.ArgumentException
Exception message: This row already belongs to another table.

--Stack trace:
at System.Data.DataTable.InsertRow(DataRow row, Int32 proposedID,
Int32 pos, Boolean fireEvent)
at System.Data.DataRowCollection.Add(DataRow row)
at EmailDataSet.EmailDataTable.AddEmailRow(EmailRow row)
at Communication_SendEmail.get_EmailID()
at Communication_SendEmail.Page_Load(Object sender, EventArgs e)
at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp,
Object o, Object t, EventArgs e)
at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object
sender, EventArgs e)
at System.Web.UI.Control.OnLoad(EventArgs e)
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Page.ProcessRequestMain(Boolean
includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

--The code where it errors out:

int? emailID = (int?)Session["emailID"];

if (emailID == null)
{
// Get a new email row
EmailDataSet.EmailRow emailRow = EmailDT.NewEmailRow();
emailRow.DateSent = DateTime.Now;
emailRow.SentBy =
Profile.AppianUser.UserID.ToString().PadLeft(9, '0');
emailRow.SetSubjectNull();
emailRow.SetBodyNull();
EmailDT.AddEmailRow(emailRow); // ERRORS OUT HERE!!!

EmailTableAdapter emailTA = new EmailTableAdapter();
emailTA.Update(EmailDT);

// Get the new email id
emailID = emailRow.EmailID;

// Add the email id to session
Session["emailID"] = emailID;
}

return emailID;

--It errors out on the EmailDT.AddEmailRow(emailRow); line sometimes,
but sometimes not.

--The Email DataTable is as follows:

EmailDataSet.EmailDataTable emailDT =
(EmailDataSet.EmailDataTable)Cache["emailDT"];

if (emailDT == null)
{
emailDT = new EmailDataSet.EmailDataTable();
EmailTableAdapter emailTA = new EmailTableAdapter();
emailTA.Fill(emailDT);
Cache.Insert("emailDT", emailDT, null,
DateTime.Now.AddMinutes(30), TimeSpan.Zero);
}

return emailDT;

I can't figure out why it works fine most of the times however it
throws that exception every now and then...seems weird to me and I
can't put my finger on it. Please help!

Thank you.
 
B

Bill Block

This error normal occurs (as you might expect) when you attempt to add
an existing row from another DataTable into a new DataTable via the
AddRow() method.

i.e.

EmailDataSet.EmailRow existingRow = emailDT.Rows[0];
...
EmailDT.AddEmailRow(existingRow); // ERRORS OUT HERE!!!

Looking at your code sample posted, I don't see you doing that so the
behavior is strange.

One thing I would recommend is removing the Email DataTable from the
cache temporarily (instead just reload it everytime you need it).
You're probably caching for performance reasons, so you won't want to
move this code to production, but you can rule out any caching-related
issue that may be going on.

Bill
 
M

MonkeyG

Thank you for your post back. I thought as much about the issue of
caching the data. I will try to kill the cache and get a new set to see
what happens.
 
M

MonkeyG

So I think i figured it out. By removing the caching it actually made
things worse. Because Now I was getting a new DT every time I went to
get the EmailDT. So nothing actually got inserted to the Database. So I
kept the caching on and changed how I inserted the new row to the
following:
int? emailID = (int?)Session["emailID"];

if (emailID == null)
{
// Get a new email row
EmailDataSet.EmailRow emailRow = EmailDT.AddEmailRow("",
"", DateTime.Now, Profile.AppianUser.UserID.ToString().PadLeft(9,
'0'));

EmailTableAdapter emailTA = new EmailTableAdapter();
emailTA.Update(EmailDT);

// Get the new email id
emailID = emailRow.EmailID;

// Add the email id to session
Session["emailID"] = emailID;
}

return emailID;

The first difference is that I do not get a strongly typed EmaiRow
object back from the datatable any more. I instead use the overloaded
AddEmailRow method and pass in my information that way. This way I can
never use the same row twice because I am never editing another row to
begin with. This seems to be working thus far. Thank you for your help.
 

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

Top