"This row already belongs to another table" error

  • Thread starter Thread starter Saso Zagoranski
  • Start date Start date
S

Saso Zagoranski

Hi!

I have a little search textbox, which goes through a dataset as the user
types in the textbox...
If the user types "1", then all the fields beginning with a 1 get shown in
the datagrid...

here's the problem... I load the dataSet with all possible entries at
Form_Load event...
but during the search I don't want to change the initial dataSet, so I have
created:
tempDataSet = new DataSet();
/* fill code for tempDataSet goes here */

I also created a SqlCommand and other stuff in order to fill the values in
tempDataSet... the where clause in this
select statement is always false so I just get the right schema in the
tempDataSet...

now I try to loop through the initial dataset and find the correct rows in
this dataSet... if a match is found
I try to add that row to tempDataSet but I get the above error...

and ideas?

thanks,
saso
 
The problem is in your fill code. You need to declare a new datarow object
and assign the values to that new row then add the row.

DataRow myRow = dsNew.Tables[0].NewRow();

myRow[3]= "00000.00";

myRow[2]= "00000.00";

myRow[1]= dsDonor.Tables[0].Rows[0][col].ToString();

myRow[0]= dsDonor.Tables[0].Columns[col].ColumnName;


dsNew.Tables[0].Rows.Add(myRow);

roughly

You are getting this error because the row you are adding belongs to the
donor datarow. To avoid problems with circular reference, the run-time does
not allow you to do that.

regards
 
Saso said:
I also created a SqlCommand and other stuff in order to fill the
values in tempDataSet... the where clause in this
select statement is always false so I just get the right schema in the
tempDataSet...

If all you want is an existing table's schema, use the Clone method of
the DataTable class, It returns a DataTable containing only the schema
of the original.
now I try to loop through the initial dataset and find the correct
rows in this dataSet... if a match is found
I try to add that row to tempDataSet but I get the above error...

You can't add a row from another DataTable but you can import it using
ImportRow.

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)
 
BTW, instead of jumping through these hoops you might consider using the
RowFilter property of the DataTable's DefaultView member. This member is
a DataView object and they're quite handy.

Set your grid's DataSource property to your table's DataView and you can
quickly sort and filter records using only a single DataTable.

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)
 
Back
Top