I have a datatable from a stored procedure. I want to add a column named "Assessment" which will be populated from a sql query. Finally I want to populate a store with this datatable. In visual studio I see that the datatable column that contains the "SiteId" is named 7. But I receive this error:
Load failed:Column '7' does not belong to table .
and the code:
DAL.DBDataContext dc = new DAL.DBDataContext();
try
{
SqlConnection sqlConnection1 = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DBConnectionString"].ConnectionString);
SqlCommand cmd = new SqlCommand();
SqlDataReader sqlreader;
cmd.CommandText = "StorProc";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@CTid", ctid));
cmd.Connection = sqlConnection1;
sqlConnection1.Open();
sqlreader = cmd.ExecuteReader();
var dt = new DataTable();
dt.Load(sqlreader);
//Add assessment column
dt.Columns.Add("Assessment", typeof(System.String));
var data = new List<object>();
foreach (DataRow r in dt.Rows)
{
if (r[7] != null)
{
var res = (from d in dc.Doctors
join ct in dc.CTUsers on d.id equals ct.DoctorId
where ct.CTid == ctid && ct.SiteId == Int32.Parse(r["7"].ToString())
select d).FirstOrDefault();
r["Assessment"] = res.Assessment;
}
data.Add(r);
}
this.storeSites.DataSource = dt;
this.storeSites.DataBind();
sqlConnection1.Close();
}
catch (Exception a)
{
X.Msg.Alert("Warning", "Load failed:" + a.Message).Show();
}

7.Int32.Parse(r["7"].ToString()is your problem, I believe.