1

我正在为我的 ShoppingCart 需要的每个项目从 Northwind 数据库打开一个数据库查询。它是从桌子上拿走ProductID的。在我从数据库中取出两列以将数据保存到. 然后我比较以确保用户输入的数量小于数据库中库存的列单位。UnitsInStockProductsDataTabel ds

theCart.Values是 ICollections 的一部分。

我遇到错误:来自我的异常消息:“连接到数据库时出现问题:对象引用未设置为对象的实例。”

这是代码。

DataSet ds = new DataSet();
        OleDbConnection conn = new OleDbConnection((string)Application["DBConnectionString"]);
        foreach (OrderItem item in theCart.Values)
        {
            string selectionString =
            "SELECT Products.ProductID, Products.UnitsInStock " +
                "FROM Products" +
                "WHERE Products.ProductID = " + item.ProductID + ";";
            try
            {
                OleDbCommand cm = new OleDbCommand(selectionString, conn);
                OleDbDataAdapter da = new OleDbDataAdapter();
                da.SelectCommand = cm;
                da.Fill(ds);
                da.Dispose();
                 if (ds.Tables["Products"].Columns.Count != 0 &&
            ds.Tables["Products"].Rows.Count != 0)
            {
                for (int index = 0; index < ds.Tables["Products"].Rows.Count; index++)
                {
                    if (item.ProductID == int.Parse(ds.Tables["Products"].Rows[index][indexOfProductID].ToString()))
                    {
                        if (item.QuantityOrdered > int.Parse(ds.Tables["Products"].Rows[index][indexOfUnitsInStock].ToString()))
                        {
                            hasStock = false;
                            int inStock = int.Parse(ds.Tables["Products"].Rows[index][indexOfUnitsInStock].ToString());
                            txtUnderstockedItems.Text += "Sorry we do not have enough stock of item: " + item.ProductName +
                                "<br> Currently, " + item.ProductName + " (ID:" + item.ProductID + ") has " + inStock + " in stock.";
                        }
                        else
                        {//can output how many items in stock here. 
                            hasStock = true;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                txtUnderstockedItems.Text = "There was a problem connecting to the database: " + ex.Message;
            }
            finally
            {

                conn.Close();
            }


            }
        }
4

3 回答 3

3

行或列很可能为空。在 if 语句之前检查 ds 。发生这种情况的一个常见原因是 Products 表没有返回任何内容。您无法获取不存在的对象的属性,因此异常。您应该进行!= null比较而不是检查计数。如果长度为零,则循环内的代码将永远不会执行,但您不会崩溃或任何事情。

if (ds.Tables["Products"].Columns != null && ds.Tables["Products"].Rows != null)

请注意,如果您的行数为零,这不会导致任何问题,但是您可能需要循环中的一些逻辑来检查您计划访问的列是否存在。

于 2012-10-26T21:29:57.463 回答
0

DataTableCollection.Item如果没有具有指定表名的表,则返回 null。

如果命令未返回任何行,则不会将表添加到 DataSet,并且不会引发异常。所以我假设 DataSet 中没有表,因为没有返回行。

我会改为手动初始化一个DatatTable,并使用它的重载Fill需要一个DataTable.

Dim table = new DataTable("Products")
da.Fill(table)
于 2012-10-26T21:32:53.297 回答
0

您正在尝试从数据集中获取具有硬编码名称的表,以了解您在数据库中看到的内容,我刚刚对类似模式进行了测试,看起来就像您创建Dataset并从数据库中填充它时所做的那样,表名不是从数据库中复制的。正如蒂姆建议的那样,您应该检查ds.Table[0]

于 2012-10-26T21:34:17.813 回答