views:

313

answers:

2

Hi i want to add a datarow that im getting back from a data table to a new datatable

this is the code im using:

foreach (DataRow dr1 in dt.Rows)
{
  string AptType = dr1["AppointmentType"].ToString();
  if (AptType == "FreeTime")
  {
    dt2.ImportRow(dr1);
  }
}
RadGrid2.DataSource = dt2;
reader.Close();
conn.Close();

the problem is that when i then go to run the page with the table on it im getting a datakey error and that one of the columns isnt being recognised

thanks in advance

+1  A: 

Do the two data tables have the same schema? Those errors might be thrown if they do not match columns, datatypes, or keys.

Matthew Jones
they should have the same schema i havent done this before do i need to set the columns first because i thought that the clone function did that.
kevinw
Not if datatable 2 is a clone of datatable 1, or vice versa. Clone() copies all columns and constraints. Could you post a simplified schema of your tables?
Matthew Jones
#TempEngineerGroupMain ( AppointmentType VARCHAR(50), BookedDate DateTime, BookedFromTime DateTime, BookedToTime DateTime, TotalDays INT, JobMins INT )thats the temp table that my stored proc is bring back
kevinw
Good, but not helpful. Can you post the exact error messages?
Matthew Jones
thanks for your help on this i have fixed it it was a case of schema's being wrong between the tables and me suffering sleep deprevation not noticing it
kevinw
A: 

You should use Typed TableDataAdapters, I would make your life so much easier...

This is very easy to do and to understand.

Follow this tutorial Strongly Typed TableDataAdapters and DataTables

Once you grasp the concept, you should do something like this:

MyTypedTableAdapter tableAdapter = new MyTypedTableAdapter();
    MyTypedDataTable dt = tableAdapter.GetData();
    foreach (MyTypedDataRow row in dt.Rows)
    {
        string AptType = row.AppointmentType;
        if (AptType == "FreeTime")
        {
            dt2.ImportRow(row);
        }
    }
    RadGrid2.DataSource = dt2;
Maxime