Adding a datarow from an existing table to a new table
-
06-07-2019 - |
Question
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
Solution
Do the two data tables have the same schema? Those errors might be thrown if they do not match columns, datatypes, or keys.
OTHER TIPS
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;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow