“IndexOutOfRangeException was unhandled”

本文关键字:unhandled was IndexOutOfRangeException | 更新日期: 2023-09-27 18:22:05

string Query = "SELECT [AA], [TEXT] FROM [TABLE] WHERE FK_ML_PROGRAM=200 AND FK_ML_LANGUAGE=" +  Convert.ToInt32(cboLanguage.SelectedIndex) + " AND AA like 'Msg_%' ORDER BY ID ";
LanguageData = DAL.ExecuteDataTable(Query);
private DataRow GetRows(string b)
{
    DataRow[] result_start = LanguageData.Select("AA='" + b.ToString() + "'");
    DataRow Row = result_start[0];
    return Row;
}

当我运行应用程序时,它会抛出一个IndexOutOfRangeException.关于导致这种情况的原因的任何想法?

“IndexOutOfRangeException was unhandled”

result_start可能没有元素。您需要检查:

private DataRow GetRows(string b)
{
    DataRow[] result_start = LanguageData.Select("AA='" + b.ToString() + "'");
    if (results_start.Length > 0)
        return result_start[0];
    return null;
}

筛选器似乎与已返回的数据集中的任何行都不匹配。

在尝试从中获取特定行之前,您需要检查result_start是否有行。