I´m trying to set a cell of type “System.Int32” in a DataSet by code, and my try looks like this:
int aCarID = 5; // as an example...
// points out the row that I want to manipulate - I guess this is what doesn´t work???
int insertIndex = myDataSet.tableCars.Rows.Count
myDataSet.tableCars.Rows[insertIndex]["CarID"] = aCarID;
What happens is: I get an exception of “System.IndexOutOfRangeException”.
You´re allowed to say that I´m stupid as long as you provide an answer…
UPDATE!
Yes, I´m trying to create a new row, that´s true – that´s why I´m not using “-1”.
So what´s the syntax to create a new row?
If I use tableCars.Rows.Add(…) I need to supply a “DataRow Row” to the Add-function, and I don´t have one to provide – yet! (Catch 22)
NEW UPDATE!
Ooops, found it – “NewRow()” 🙂
You do realize that indices start with zero in C#? That means if your table has 3 rows, you’re trying to access the 4th row because insertIndex = 3.
Try insertIndex – 1.
Edit: Since you’re trying to add a new row and already found out how to do so, also don’t forget to save those changes to the database (I assume that’s what you want to do). The most simple way is to set the UpdateCommand-property of the DataAdapter you used to fill the DataSet (or actually the DataTable in the DataSet).
You can also have the update commands generated, using a subclass of the DbCommandBuilder.