Reputation: 23
I am getting an index out of bounds of array error when I try to access a value in a row of a dataset table. Here is the code
DataRow[] userRow = newsdataset.Tables["users"].Select("Id = " + userid);
if (userRow != null && userRow.Length != 0)
{
string connectionName = userRow[1].ToString() + " " + userRow[2].ToString();
}
When I debug I can see that userRow isn't null and it has values in the array.
EDIT - I should have added - I am not mixing up the indices (I know that the index starts at 0) I have the error when I try to retrieve index 1 and 2, but in debugging I can see that the row array has values at indexes 0, 1, 2 and 3
Any clues? Thanks!
Upvotes: 1
Views: 3968
Reputation: 216353
The Select method of a DataTable returns an array of DataRow, and a DataRow contains an array of DataColumn.
You have correctly checked if the returned array is not null and if it contains at least one row, but if you want to access the content of that row at index zero you need to use two indexes, one to select the row in the array and one to choose the column of interest in the selected DataRow.
DataRow[] userRow = newsdataset.Tables["users"].Select("Id = " + userid);
if (userRow != null && userRow.Length != 0)
{
string connectionName = userRow[0][1].ToString() + " " + userRow[0][2].ToString();
}
Your code instead tries to read the second and third DataRow of the returned array not the second and third column of the first row. And of course this triggers the exception because there is no a second or third row in the userRow array. (Better pluralize that name, I bet that this is what side-tracked many answers here)
Consider also that you can access the columns using their ColumnName property. This makes your code less prone to error if something changes in the order in which the columns are retrieved. Suppose the column 1 is called "Host" and column 2 is called "Database" then
string connectionName = userRow[0]["Host"].ToString() + " " +
userRow[0]["Database"].ToString();
Also, if there is a possibility of a null value in these columns, you should take a look on how to use the DataRow.IsNull method to check for null values
Upvotes: 1
Reputation: 1633
You are retrieving at, index 1 and index 2. Your results may contain only 2 elements or less than 2 and in which case you will get the exception. You shoud start retrieve at index 0 and index 1 instead as below
DataRow[] userRow = newsdataset.Tables["users"].Select("Id = " + userid);
if (userRow != null && userRow.Length != 0)
{
string connectionName = userRow[0].ToString() + " " + userRow[1].ToString();
}
A better solution is to loop as below to avoid array index out of bound exceptions. Because your index will always be within the bounds of your array
DataRow[] userRow = newsdataset.Tables["users"].Select("Id = " + userid);
if (userRow != null && userRow.Length != 0)
{
string connectionName = string.Empty;
for( int i = 0; i < userRow.Length; i++)
{
connectionName += userRow[i].ToString() + " ";
}
}
var result = connectionName;
Upvotes: 0
Reputation: 3204
This is probably because maybe it has values, but it doesn't have the value for the index that you are requesting.
In this line :
string connectionName = userRow[1].ToString() + " " + userRow[2].ToString();
You are accessing two elements, at the second and third index respectively. Now lets say if your datatable has two rows, then you will get an exception for userRow[2]
since it doesn't exists in the datatable.
Check the number of rows that are returned and the rows that you are accessing.
Hope this helps.
Upvotes: 0
Reputation: 305
You havn't checked that the array is at least 3 items long, therefore this exception might occur. Moreover, I am not sure if you are familiar with it but array indexes are starting at zero.
I guees you should to do this:
string connectionName = userRow[0].ToString() + " " + userRow[1].ToString();
Upvotes: 0