Simplify your code, and try again with that:
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("@Id", typeof(int)));
DataRow dr = dt.NewRow();
dr["Id"] = 666;
You will get the same error.
And the error is right: "Column 'Id' does not belong to table"
Column '@Id' does though:
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("@Id", typeof(int)));
DataRow dr = dt.NewRow();
dr["@Id"] = 666;
Will work fine.
So match your columns up, either by changing the initial names:
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("Id", typeof(int)));
dt.Columns.Add(new DataColumn("Code", typeof(string)));
dt.Columns.Add(new DataColumn("Description", typeof(string)));
DataRow dr = dt.NewRow();
foreach (var i in lkup)
{
dr = dt.NewRow();
dr["Id"] = i.Id.Replace("\"", "");
dr["Code"] = i.Code.Replace("\"", "");
dr["Description"] = i.Description.Replace("\"", "");
dt.Rows.Add(dr);
}
Or changing the usage:
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("@Id", typeof(int)));
dt.Columns.Add(new DataColumn("@Code", typeof(string)));
dt.Columns.Add(new DataColumn("@Description", typeof(string)));
DataRow dr = dt.NewRow();
foreach (var i in lkup)
{
dr = dt.NewRow();
dr["@Id"] = i.Id.Replace("\"", "");
dr["@Code"] = i.Code.Replace("\"", "");
dr["@Description"] = i.Description.Replace("\"", "");
dt.Rows.Add(dr);
}