Select 并更新数据表中的数据

Select and updating data from datatable

我 select 从我的数据表中获取数据,使用下面的代码,但是它抛出了一个我不熟悉的异常。

"Index was outside the bounds of the array."

tblRooms.Select(IDRoom = 4)这是thetblroom中IDRoom的值,不知道为什么会报错

foreach (DataRow dr in tblRoomCart.Rows)
{

     DataRow drRoom = tblRooms.Select("IDRoom =" + dr["IDRoom"])[0];//here the error in this line 
}

如异常所述,索引越界,您正在读取第一个元素(索引为0)但集合中没有元素。
与此相同:

int[] array = new int[0];
int x = array[0];//You will get an exception here

添加 if 语句以确保至少有 1 个元素,例如:

int[] array = new int[0];
if (array.Length > 0)
{
        int x = array[0];
}