"This row already belongs to another table" error

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Saso Zagoranski

    "This row already belongs to another table" error

    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


  • Alvin Bruney

    #2
    Re: "This row already belongs to another table" error

    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

    --


    -----------
    Got TidBits?
    Get it here: www.networkip.net/tidbits
    "Saso Zagoranski" <saso.zagoransk i@guest.arnes.s i> wrote in message
    news:bos0kd$r8n $1@planja.arnes .si...[color=blue]
    > 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[/color]
    have[color=blue]
    > 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
    >
    >[/color]


    Comment

    • Frank Oquendo

      #3
      Re: &quot;This row already belongs to another table&quot; error

      Saso Zagoranski wrote:
      [color=blue]
      > 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...[/color]

      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.
      [color=blue]
      > 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...[/color]

      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.


      (Pull the pin to reply)


      Comment

      • Frank Oquendo

        #4
        Re: &quot;This row already belongs to another table&quot; error

        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.


        (Pull the pin to reply)


        Comment

        • Saso Zagoranski

          #5
          Re: &quot;This row already belongs to another table&quot; error

          Thanks!

          That was real useful advice...

          saso

          "Frank Oquendo" <frankopin@acad x.com> wrote in message
          news:%23NiXgYMq DHA.1728@TK2MSF TNGP09.phx.gbl. ..[color=blue]
          > 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)
          >
          >[/color]


          Comment

          Working...