Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 表不包括';没有主键(缺少PrimaryKeyException)_C# - Fatal编程技术网

C# 表不包括';没有主键(缺少PrimaryKeyException)

C# 表不包括';没有主键(缺少PrimaryKeyException),c#,C#,我在一个购物车上工作,在使用Find()方法时,我遇到了这个缺少PrimaryKeyException(表没有主键)的问题,当我已经为数据表设置了主键时,我感到困惑 创建购物车和添加到购物车的我的代码: public static void CreateShopCart() { // create a Data Table object to store shopping cart data DataTable shoppingCartDataTable = ne

我在一个购物车上工作,在使用Find()方法时,我遇到了这个缺少PrimaryKeyException(表没有主键)的问题,当我已经为数据表设置了主键时,我感到困惑

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

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;
}
public静态void CreateShopCart()
{
//创建数据表对象以存储购物车数据
DataTable shoppingCartDataTable=新数据表(“购物车”);
shoppingCartDataTable.Columns.Add(“ProductID”,typeof(int));
//将ProductID设为主键
DataColumn[]primaryKeys=新DataColumn[1];
primaryKeys[0]=shoppingCartDataTable.Columns[0];
shoppingCartDataTable.PrimaryKey=PrimaryKey;
shoppingCartDataTable.Columns.Add(“数量”,typeof(int));
shoppingCartDataTable.Columns.Add(“单价”,类型(十进制));
shoppingCartDataTable.Columns.Add(“ProductName”,typeof(string));
shoppingCartDataTable.Columns.Add(“ProductDescription”,typeof(string));
shoppingCartDataTable.Columns.Add(“SellerUsername”,typeof(string));
shoppingCartDataTable.Columns.Add(“Picture”,typeof(string));
//在会话中存储数据表
HttpContext.Current.Session[“购物车”]=shoppingCartDataTable;
}
公共静态void AddShopCartItem(int-ProductID、十进制价格、字符串strPName、字符串strPDesc、字符串strSellerUsername、字符串strImage)
{
int intQty=1;
var retStatus=HttpContext.Current.Session[“Cart”];
如果(retStatus==null)
CreateShopCart();
//从会话获取购物数据
DataTable shoppingCartDataTable=(DataTable)HttpContext.Current.Session[“Cart”];
//查找购物车中是否已存在ProductID

DataRow dr1=shoppingCartDataTable.Rows.Find(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);

打印列名

您已使用
将要添加的列命名为主键,而列名只是
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);

打印ColumnName

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


干杯并感谢您的帮助。

我已经计算了一整天,发现我犯了一个错误,我没有调用CreateShopCart方法,这就是AddShopCartItem方法在表中没有主键的原因


干杯并感谢您的帮助。

抱歉,我复制了错误的编码。它应该是primaryKeys[0]=shoppingCartDataTable.Columns[0];我尝试了您的方法,但不幸的是,它仍然不起作用。您能否试着调试代码并检查(在将表添加到会话之前)是否,primarykey的定义是否正确?如果从会话检索表后primarykey仍然存在?我已尝试使用简化版本的代码(不涉及会话),但没有发生任何奇怪的情况。很抱歉,我复制了错误的编码。它应该是primarykey[0]=shoppingCartDataTable.Columns[0];我尝试了你的方法,不幸的是它仍然不起作用。你能试着调试你的代码并检查(在将表添加到会话之前)primarykey是否定义正确吗?从会话检索表后primarykey是否仍然存在吗?我试过使用你的代码的简化版本(不涉及会话)没有什么奇怪的事情发生。