Copying records from datatable to datatable in dataset

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

    Copying records from datatable to datatable in dataset

    Running on VS.net 2005,

    I am trying to copy rows from my datatable to another datatable in the same
    dataset.

    The schema would be identical.

    I need to make the table name "forms" as I am passing this dataset to
    function that expects a dataset with a datatable in it.

    I am loading up the dataset with my database information and then copying
    the data row by row to the new datatable until the key changes then I would
    pass the dataset to the function for processing. When done, I would clear
    the datatable (not sure the best way to do this - but was looking as
    Clear()).

    I am getting errors trying to clone the table so that the schema will be the
    same but I can't seem to get it to work. I get an error trying to clone to
    may already set up table:

    Property or indexer 'System.Data.Da taTableCollecti on.this[string]'
    cannot be assigned to -- it is read only

    *************** *************** *********
    ds.Tables.Add(" Forms");
    ds.Tables["Forms"] = ds.Tables["UnUsed"].Clone; <---- Error here

    foreach (DataRow dr in ds.Tables["UnUsed"].Rows)
    {
    if (oldApsID == 0)
    oldApsID = (int)dr["AppsID"];

    if (oldApsID != (int)dr["AppsID"])
    {
    oldApsID = (int)dr["AppsID"];
    ds.Tables["Tables"].Clear();
    ds.Tables.Add(" Forms");
    ds.Tables["Forms"] = ds.Tables["UnUsed"].Clone; <--- same error
    here
    }

    if(oldApsID == (int)dr["ApsID"])
    {
    ds.Tables["Forms"].ImportRow (dr);
    }
    }
    *************** *************** ***********

    I assume my syntax is incorrect, or you have to clone and create the table
    at the same time.

    But I can't seem to get it to work.

    Thanks,

    Tom


  • tshad

    #2
    Re: Copying records from datatable to datatable in dataset

    Actually, Clone may not be what I am looking for.

    What I want to do is create a table in my dataset this has the same columns
    as my other table and then copy selected rows into the table (until a value
    changes), then clear the table and do it again until I finish with the
    original table.

    Thanks,

    Tom
    "tshad" <tshad@dslextre me.comwrote in message
    news:epH3%239Y1 IHA.4704@TK2MSF TNGP05.phx.gbl. ..
    Running on VS.net 2005,
    >
    I am trying to copy rows from my datatable to another datatable in the
    same dataset.
    >
    The schema would be identical.
    >
    I need to make the table name "forms" as I am passing this dataset to
    function that expects a dataset with a datatable in it.
    >
    I am loading up the dataset with my database information and then copying
    the data row by row to the new datatable until the key changes then I
    would pass the dataset to the function for processing. When done, I would
    clear the datatable (not sure the best way to do this - but was looking as
    Clear()).
    >
    I am getting errors trying to clone the table so that the schema will be
    the same but I can't seem to get it to work. I get an error trying to
    clone to may already set up table:
    >
    Property or indexer 'System.Data.Da taTableCollecti on.this[string]'
    cannot be assigned to -- it is read only
    >
    *************** *************** *********
    ds.Tables.Add(" Forms");
    ds.Tables["Forms"] = ds.Tables["UnUsed"].Clone; <---- Error here
    >
    foreach (DataRow dr in ds.Tables["UnUsed"].Rows)
    {
    if (oldApsID == 0)
    oldApsID = (int)dr["AppsID"];
    >
    if (oldApsID != (int)dr["AppsID"])
    {
    oldApsID = (int)dr["AppsID"];
    ds.Tables["Tables"].Clear();
    ds.Tables.Add(" Forms");
    ds.Tables["Forms"] = ds.Tables["UnUsed"].Clone; <--- same error
    here
    }
    >
    if(oldApsID == (int)dr["ApsID"])
    {
    ds.Tables["Forms"].ImportRow (dr);
    }
    }
    *************** *************** ***********
    >
    I assume my syntax is incorrect, or you have to clone and create the table
    at the same time.
    >
    But I can't seem to get it to work.
    >
    Thanks,
    >
    Tom
    >
    >

    Comment

    Working...