Search code examples
c#asp.netcrystal-reportsdataset

system.argumentexception column does not belong to table


I have a Defined Dataset with table Customer in it. I'm trying to add a row to this Dataset but I'm getting an error below is my code

DataSet ds = new DataSet1();
DataTable dt = new DataTable("Customer");

using (StreamReader reader = new StreamReader(fileStream))
{
   while(reader.Peek() >= 0)
   {
      string row = reader.ReadLine();
      string[] content = row.Split('|');
      DataRow dr = dt.NewRow();
      dr["mother_name"] = content[0];  // I'm getting an error here.
      dr["beneficial_name"] = content[1];
      dr["industry_code"] = content[2];
      dt.Rows.Add(dr);
   }

}

filestream.Close();
ds.Tables.Add(dt);
cryRpt.SetDataSource(ds);

So I'm getting an exception error in line dr["emp_lastname"] = content[0]; the key exist in the table in my dataset. So I assumed that when I do dt.NewRow() it will create an instance of my dataset table row. How do I solve this? I just want to populate a row in my DataSet Table. Below is the screenshot of my Dataset

enter image description here


Solution

  • You have a Dataset instance in ds and a DataTable instance in dt. That DataTable instance is not linked to your Dataset in anyway. The Datatable has therefore no columns.

    Do a dt=ds.Tables["Customer"]; instead to get an instance of the Datatable that conforms to the schema in your typed dataset.