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

system.argumentexception column does not belong to table

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x