How to insert data into datatable and then into a dataset from datarow array?

Hello

I have a problem in my program with put data into a new datatbale and then the datatable into a new dataset from a DataRow array, I have triede the following:

DataSet ds_row = new DataSet();
DataTable dt_row = new DataTable("test");

DataRow[] arr = ds.Tables[0].Select("opgave_id = "+item+"");

ds_row.Tables.Add(dt_row);

foreach(DataRow row in arr)
{
dt_row.NewRow();

dt_row.Rows.Add(row);
}

I get only one row from the "ds.Tables[0].Select command" so that works, but when I try to add new row in the foreach a get the following error:
Something like: "Row always exist in another table".

Someone to help how a get the select row into a new datatable and then the datatable into new dataset

Hope someone will answer.

spot



Answer this question

How to insert data into datatable and then into a dataset from datarow array?

  • b-man

    that maybe the case however you are performing a query on the datatable, which returns all relevent rows including any relational data. Are you saying you have executed this EXACT same code elsewhere and works fine

     

    To add a column:

     

    theDataSet.Tables[Index].Columns.Add();



  • Helen999888

    you don't need the "Dt_row.Rows.NewRow();" - just the dt_ro.Rows.Add(row)

    I'm also guessing that you perhaps have a relational database there which there are more than 1 table in the dataset, which when querying it as you have done, will also get the records relating to the query from the other table



  • RajeevP5

    You had a solution Yes I have a relations database but the syntax ds.Tables[0] works properly fine all places in my program.

    I have try this:

    DataRow[] arr = ds.Tables[0].Select("opgave_id = "+item+"");

    ds_row.Tables.Add(dt_row);

    foreach(DataRow row in arr)

    {

    dt_row.NewRow();

    dt_row.ImportRow(row);

    }

    But then I get an error message with no columns in the datatable, someone out there to help me how a get this problem fixed

    I have tried with messagebox to see what I get return, I get 1 row, but 0 columns.

    Hope someone can help me with syntax.

    spot


  • LauraCapatina

    This is what I get and want:

    I have a mysql SELECT sentence which take some columns fra some database tabels with relations and put into a dataset, and then I will get only one row out of the dataset and put it into another dataset, because i need the dataset in my program with only the one row, and then I want the same columns in the new dataset with the row and data in the columns, my syntax is here:

    DataSet ds = new DataSet();
    DataSet ds_row = new DataSet();
    DataTable dt_row = new DataTable("test");

    ds_row.Tables.Add(dt_row)

    db.SelectQuery(ds, "SELECT opgave_id, kategori_navn, priotet_navn FROM opgave, kategori, priotet, WHERE opgave_kategori LIKE kategori_id AND opgave_priotet LIKE priotet_id");

    DataRow[] arr = ds.Tables[0].Select("opgave_id = "+item+""); //The item come as input in this function as an int

    foreach(DataRow row in arr)
    {
    dt_row.ImportRow(row);
    }

    Hope someone can help me.

    spot


  • How to insert data into datatable and then into a dataset from datarow array?