2013-07-27 123 views
1

我正在购物车上工作,当使用Find()方法时,我得到了这个MissingPrimaryKeyException(表没有主键),我感到困惑当我已经有什么错误设置数据表的主键。表没有主键(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; 
} 

回答

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); 

不产生任何输出

修复只需用

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

打印添加的PrimaryKey中的ColumnName

+0

对不起,我复制了错误的编码。它应该是primaryKeys [0] = shoppingCartDataTable.Columns [0];我尝试了你的方法,不幸的是它仍然不起作用。 – Spencer

+0

您可以尝试调试您的代码并检查(在将表添加到会话之前)主键是否正确定义?并且如果在从Session中检索表之后,PrimaryKey仍然存在?我试着用你的代码缩减版本(没有涉及Session),也没有发生任何奇怪的事情。 – Steve

0

我已经搞清楚整天,发现我犯了一个错误,我没有叫出CreateShopCart方法,这就是为什么AddShopCartItem方法在表中没有主键的原因。

干杯和感谢您的帮助。