超出范围的数据集异常

时间:2022-03-21 16:44:35

Hoping this is a simple one

希望这是一个简单的

I am extracting the data from a column of my dataset. As the SQL query is supplied an ID only one result will ever be returned. So I would think this result would always be at row '0' of the dataset.

我从我的数据集列中提取数据。由于SQL查询提供了ID,因此只返回一个结果。所以我认为这个结果总是在数据集的第0行。

If i click the first result in my datagrid........send the id '0' across to my second page which then extracts the image name from the DB it crashes. If I select the second result in the grid i.e '1' it is fine.

如果我点击我的数据网格中的第一个结果........将id“0”发送到我的第二页,然后从数据库中提取图像名称它崩溃了。如果我在网格中选择第二个结果,即'1',那就没问题了。

Here is my code:

这是我的代码:

     SqlConnection sqlcon = new SqlConnection(connstring);
        SqlCommand sqlcmd = new SqlCommand("select pic from cds WHERE _id = '" + passedID + "'", sqlcon);
        SqlDataAdapter adp = new SqlDataAdapter(sqlcmd);
        DataSet ds = new DataSet();
        adp.Fill(ds);
        // Rows set to '0' for the first result in the dataset but crashes if the first item is selected.
        object a = ds.Tables[0].Rows[0]["pic"];
        string test = a.ToString();

2 个解决方案

#1


2  

Add a check to be sure that your query has retrieved at least one row

添加一项检查以确保您的查询至少检索了一行

if(ds.Tables[0].Rows.Count > 0)
{
    object a = ds.Tables[0].Rows[0]["pic"];
    string test = a.ToString();
}
else
{
    Response.Write("No rows for the ID = " + passedID);
}

Datasets don't have Rows. It's the DataTable contained in the Dataset that has the Rows property, but if your query fails (doesn't find a row with the ID requested) then the Rows collection has zero elements and you cannot access the index zero. (out of range)

数据集没有行。它是数据集中包含的具有Rows属性的DataTable,但如果您的查询失败(找不到具有请求ID的行),则Rows集合具有零元素,您无法访问索引零。 (超出范围)

#2


0  

I would hazzard a guess that the problem is that you are either passing the wrong id, or that there is no data in the cds table for the passed id.

我会猜测问题是你传递了错误的id,或者cds表中没有传递id的数据。

When you try to access Rows[0], you get an error because there is no data, so there are no rows in the collection.

当您尝试访问Rows [0]时,由于没有数据,您会收到错误,因此集合中没有行。

#1


2  

Add a check to be sure that your query has retrieved at least one row

添加一项检查以确保您的查询至少检索了一行

if(ds.Tables[0].Rows.Count > 0)
{
    object a = ds.Tables[0].Rows[0]["pic"];
    string test = a.ToString();
}
else
{
    Response.Write("No rows for the ID = " + passedID);
}

Datasets don't have Rows. It's the DataTable contained in the Dataset that has the Rows property, but if your query fails (doesn't find a row with the ID requested) then the Rows collection has zero elements and you cannot access the index zero. (out of range)

数据集没有行。它是数据集中包含的具有Rows属性的DataTable,但如果您的查询失败(找不到具有请求ID的行),则Rows集合具有零元素,您无法访问索引零。 (超出范围)

#2


0  

I would hazzard a guess that the problem is that you are either passing the wrong id, or that there is no data in the cds table for the passed id.

我会猜测问题是你传递了错误的id,或者cds表中没有传递id的数据。

When you try to access Rows[0], you get an error because there is no data, so there are no rows in the collection.

当您尝试访问Rows [0]时,由于没有数据,您会收到错误,因此集合中没有行。