IndexOutofRangeException:位置0并没有行

本文关键字:并没有 位置 IndexOutofRangeException | 更新日期: 2023-09-27 17:58:27

我正在尝试使用资源包设计一个问答应用程序的小型项目。当我插入以下if条件时,我得到了There is no row at position 0的错误。其中,我正在从变量X中的组合框中捕获字符串。

SampleResourceBundle.Loginpage l = new SampleResourceBundle.Loginpage();
if(l.x.Equals("mr_IN"))
   sql = "select * from quesans where qid>48";
else if (l.x.Equals("en_US"))
   sql = "select * from quesans where qid<48";

错误在下一个表格的方法中的以下语句中给出:

textBox1.Text = ds.Tables["QA"].Rows[recno].ItemArray[2].ToString();

但是,如果我删除if语句,代码执行得非常好。

IndexOutofRangeException:位置0并没有行

ds.Tables["QA"]中没有行。

在尝试之前,您需要检查是否有要读取的行:

if (recno < ds.Tables["QA"].Rows.Count)
    textBox1.Text = ds.Tables["QA"].Rows[recno].ItemArray[2].ToString();