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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ArgumentException - help me...

Status
Not open for further replies.

Arkadow

Technical User
Nov 29, 2003
2
PL
Hi!

First - be patient my english isn't good ;)

I've got problem in code listed below... While compilation there's no errors, but while execution JITter throw Argument Exception: This row already belongs to another table.

Please look at this code (I marked line where the exception occurs):
--------------------------------------------------------
private static String GetPathInfo(Int32 start, Int32 end, Int32 changeCount, DataTable dtMatrixToDB)
{
DataTable dtStops = new DataTable("Przystanki"),
dtTmp = new DataTable();

Int32 i ,j, c, stop, next;

DataColumn dcStop;
DataRow drPath;

String Path = "";


for(i = 0 ; i <= changeCount; i++)
{
dcStop = new DataColumn(i.ToString(),System.Type.GetType(&quot;System.Int32&quot;));
dcStop.AllowDBNull = true;
dcStop.DefaultValue = null;
dtStops.Columns.Add(dcStop);
}

Path += &quot;\nDodano kolumny&quot;;

drPath = dtStops.NewRow();
drPath[&quot;0&quot;] = start;
dtStops.Rows.Add(drPath);

dtTmp = dtStops.Clone();

dtStops.AcceptChanges();
dtTmp.AcceptChanges();


for(i = 0; i <= changeCount; i++)
{
Path += &quot;\n for i = &quot; + i.ToString() + &quot;: &quot;;
foreach(DataRow drStops in dtStops.Rows)
{
if(drStops[i.ToString()] != null)
{
stop = Convert.ToInt32(drStops[i.ToString()]);

Path += stop.ToString() + &quot; (&quot;;
/*******************************************************/
/*******************************************************/

for(c = 0; c < count; c++)

/*******************************************************/
/*******************************************************/

{

if(mp[stop,pol[c]])
{
Path += pol[c].ToString() + &quot; &quot;;
drPath = dtStops.NewRow();

for(j = 0; j < i; j++)
{
drPath[j.ToString()] = drStops[j.ToString()];
}

drPath[i.ToString()] = pol[c];
dtTmp.Rows.Add(drPath);
}//if(mp[stop,next])
}//for(c = 0; c < count; c++)

Path+= &quot;) &quot;;
}//if(drStops[i.ToString()] != null)
}//foreach(DataRow drStops in dtStops.Rows)
dtStops.Clear();
dtStops = dtTmp.Copy();
dtTmp.Clear();
dtStops.AcceptChanges();
dtTmp.AcceptChanges();

}//for(i = 0; i <= changecount; i++)
foreach(DataRow drRigthPath in dtStops.Rows)
{
next = Convert.ToInt32(drRigthPath[changeCount.ToString()]);
if(next != end)
dtStops.Rows.Remove(drRigthPath);
}//foreach(DataRow drRigthPath in dtStops.Rows)

dtStops.AcceptChanges();

Path += &quot;\n&quot;;

foreach(DataRow dr in dtStops.Rows)
{
foreach(DataColumn dc in dtStops.Columns)
Path += Convert.ToString(dr[dc]) + &quot; - &quot;;
Path += &quot;\n&quot;;
}//foreach(DataRow dr in dtStops.Rows)

return Path;
}
-------------------------------------------

Greetings
Arek
 
Hi,
A function fails because the passed argument. Could be Convert or another.
Use try-catch bloc to capture exactly the error and execute this function under debugger to find out the line.

Code:
private static String GetPathInfo(Int32 start, Int32 end, Int32 changeCount, DataTable dtMatrixToDB)
{
DataTable dtStops = new DataTable(&quot;Przystanki&quot;),
 dtTmp = new DataTable();

Int32 i ,j, c, stop, next;

DataColumn dcStop;
DataRow drPath;

String Path = &quot;&quot;;

try
{
// all remaining code
}
catch (Exception e)
{
  string sErr = e.GetType + e.Message;
  
}
 return Path;
}

-obislavu-
 
Hi

I used try-catch block and problem is in line:

dtTmp.Rows.Add(drPath);

I don't know why. I use DataTable.NewRow() method to create new disassociated datarow.
Anyway I fixed this problem by using Datatable.Rows.Add(obejct[]) method.

Arkadow
 
Okay, if you have two DataTables dtStops and dtTmp:
DataRow dr1 = dtStops.NewRow();
DataRow dr2 = dtTmp.NewRow();
then
dtStops.Rows.Add(dr1);
dtTmp.Rows.Add(dr2);
and not:
dtStops.Rows.Add(dr2);
dtTmp.Rows.Add(dr1);

-obislavu-


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top