1

我正在处理购物车,并且在使用 Find() 方法时得到了这个 MissingPrimaryKeyException(表没有主键),当我已经为数据表设置了主键时,我很困惑出了什么问题。

我的创建购物车并添加到购物车的代码:

public static void CreateShopCart()
{
        // create a Data Table object to store shopping cart data
        DataTable shoppingCartDataTable = new DataTable("Cart");

        shoppingCartDataTable.Columns.Add("ProductID", typeof(int));
        // make ProductID primary key
        DataColumn[] primaryKeys = new DataColumn[1];
        primaryKeys[0] = shoppingCartDataTable.Columns[0];
        shoppingCartDataTable.PrimaryKey = primaryKeys;

        shoppingCartDataTable.Columns.Add("Quantity", typeof(int));
        shoppingCartDataTable.Columns.Add("UnitPrice", typeof(decimal));
        shoppingCartDataTable.Columns.Add("ProductName", typeof(string));
        shoppingCartDataTable.Columns.Add("ProductDescription", typeof(string));
        shoppingCartDataTable.Columns.Add("SellerUsername", typeof(string));
        shoppingCartDataTable.Columns.Add("Picture", typeof(string));
        // store Data Table in Session
        HttpContext.Current.Session["Cart"] = shoppingCartDataTable;
}

public static void AddShopCartItem(int ProductID, decimal Price, string strPName, string strPDesc, string strSellerUsername, string strImage)
{
    int intQty = 1;
    var retStatus  = HttpContext.Current.Session["Cart"];
    if (retStatus == null)
        CreateShopCart();

    // get shopping data from Session
    DataTable shoppingCartDataTable = (DataTable)HttpContext.Current.Session["Cart"];

    //  Find if ProductID already exists in Shopping Cart

    DataRow dr1 = shoppingCartDataTable.Rows.Find(ProductID); **<- This is the line giving the error** 
    if (dr1 != null)
    {
        // ProductID exists. Add quantity to cart 
        intQty = (int)dr1["Quantity"];
        intQty += 1; // increment 1 unit to be ordered
        dr1["Quantity"] = intQty; // store back into session
    }
    else
    {
        // ProductID does not exist; create a new record
        DataRow dr = shoppingCartDataTable.NewRow();
        dr["ProductID"] = ProductID;
        dr["ProductName"] = strPName;
        dr["ProductDescription"] = strPDesc;
        dr["Quantity"] = intQty;
        dr["UnitPrice"] = Price;
        dr["SellerUsername"] = strSellerUsername;
        dr["Picture"] = strImage;
        shoppingCartDataTable.Rows.Add(dr);
    }

    // store back shopping cart in session
    HttpContext.Current.Session["Cart"] = shoppingCartDataTable;
}
4

2 回答 2

1

您已将要添加为主键的列命名为使用 <ProductID>,而列名只是ProductID. 奇怪的是,这种语法没有错误(至少使用 LinqPAD 测试您的代码),但是如果您尝试在添加后打印 PrimaryKey,您将看到没有定义 PrimaryKey。

所以,这段代码

DataColumn[] primaryKeys = new DataColumn[1];
primaryKeys[0] = shoppingCartDataTable.Columns["<ProductID>"];
shoppingCartDataTable.PrimaryKey = primaryKeys;
foreach(DataColumn dc in shoppingCartDataTable.PrimaryKey)
    Console.WriteLine(dc.ColumnName);

不产生任何输出

修复只需添加 PrimaryKey

 DataColumn[] primaryKeys = new DataColumn[1];
 primaryKeys[0] = shoppingCartDataTable.Columns["ProductID"];
 shoppingCartDataTable.PrimaryKey = primaryKeys;
 foreach(DataColumn dc in shoppingCartDataTable.PrimaryKey)
     Console.WriteLine(dc.ColumnName);

打印列名

于 2013-07-27T17:16:45.473 回答
0

琢磨了一整天,发现我犯了一个错误,我没有调用 CreateShopCart 方法,这就是为什么 AddShopCartItem 方法在表中没有主键的原因。

干杯并感谢您的帮助。

于 2013-07-28T16:31:11.993 回答