I created a datatable and filled it with contents of a table in a MS Access database table.

My problem started arising when I wanted to add a new row to the datatable: I get an error that my input array is longer than the columns in the table, so I went ahead and checked using a column count on the datatable only to get a zero.

string str = "Select * from Mazda";

OleDbCommand cmd = new OleDbCommand(str, con);

OleDbDataAdapter adapt = new OleDbDataAdapter(cmd);
adapt.Fill(Mazda);

I tried adding a row using the same format as the datatable in my database

Mazda.Rows.Add(a,b, c, d);

Instead of adding the row, this code throws an error

Input array is longer than the number of columns

How is it possible, if I had already populated the table and how can I fix it?

1 Answers

0
Leon On

Try it like this:

var newRow = Mazda.NewRow();
newRow["ColumnA"] = "a";
newRow["ColumnB"] = "b";
newRow["ColumnC"] = "c";
newRow["ColumnD"] = "d";

Mazda.Rows.Add(newRow);

You can use in newRow either the column-name as string or use integer values as index. Here i have used "ColumnA-D" as column-name only as example.