IndexOutOfRangeException

IndexOutOfRangeException

This question already has answers here:
What does IndexOutofRangeException mean?
                                
                                    (3个答案)
                                
                        
                                6年前关闭。
            
                    
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。关于什么原因的任何想法?

最佳答案

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;
}

关于c# - “未处理IndexOutOfRangeException” ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17622594/

10-08 20:44