错误:在所选数据源(asp.net c#、sql)上找不到名为“ProductID”的字段或属性

本文关键字:找不到 ProductID 属性 字段 sql 数据源 net asp 错误 | 更新日期: 2023-09-27 17:55:17

我在 asp.net 上使用网格视图来显示我的sql数据,它显示了错误:

在所选数据源上找不到名为"ProductID"的字段或属性

以下是我的sql编码:

public List<Product> getWomenProductAll()
    {
        List<Product> prodList = new List<Product>();
        string prod_Name, prod_Desc, Prod_Image, prod_ID;
        decimal unit_Price;
        string queryStr = "SELECT ProductID, Name, Description, Model, UnitPrice FROM WomenProduct Order By Name";
        SqlConnection conn = new SqlConnection(_connStr);
        SqlCommand cmd = new SqlCommand(queryStr, conn);            
        conn.Open();
        SqlDataReader dr = cmd.ExecuteReader();
        while (dr.Read())
        {
            prod_ID = dr["ProductID"].ToString();
            prod_Name = dr["Name"].ToString();
            prod_Desc = dr["Description"].ToString();
            Prod_Image = dr["Model"].ToString();
            unit_Price = decimal.Parse(dr["UnitPrice"].ToString());
            Product a = new Product(prod_ID, prod_Name, prod_Desc, unit_Price, Prod_Image);
            prodList.Add(a);
        }
        conn.Close();
        dr.Close();
        dr.Dispose();
        return prodList;
    }

这是我要绑定到 gridview 的 c# 编码:

 protected void bind ()
{
    List<Product> prodList = new List<Product>();
    prodList = aProd.getWomenProductAll();
    gvProduct.DataSource = prodList;
    gvProduct.DataBind();
}

请告知我的编码是否有任何错误,谢谢。以下是整个产品类别:

public class Product
{string _connStr = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
//private string _connStr = Properties.Settings.Default.DBConnStr;
private string _prodID = null;
private string _prodName = string.Empty;
private string _prodDesc = ""; // this is another way to specify empty string
private decimal _unitPrice = 0;
private string _prodImage = "";

// Default constructor
public Product()
{
}
// Constructor that take in all data required to build a Product object
public Product(string prodID, string prodName, string prodDesc,
               decimal unitPrice, string prodImage)
{
    _prodID = prodID;
    _prodName = prodName;
    _prodDesc = prodDesc;
    _unitPrice = unitPrice;
    _prodImage = prodImage;
}
// Constructor that take in all except product ID
public Product(string prodName, string prodDesc,
       decimal unitPrice, string prodImage, int stockLevel)
    : this(null, prodName, prodDesc, unitPrice, prodImage)
{
}
// Constructor that take in only Product ID. The other attributes will be set to 0 or empty.
public Product(string prodID)
    : this(prodID, "", "", 0, "")
{
}
// Get/Set the attributes of the Product object.
// Note the attribute name (e.g. Product_ID) is same as the actual database field name.
// This is for ease of referencing.
public string Product_ID
{
    get { return _prodID; }
    set { _prodID = value; }
}
public string Product_Name
{
    get { return _prodName; }
    set { _prodName = value; }
}
public string Product_Desc
{
    get { return _prodDesc; }
    set { _prodDesc = value; }
}
public decimal Unit_Price
{
    get { return _unitPrice; }
    set { _unitPrice = value; }
}
public string Product_Image
{
    get { return _prodImage; }
    set { _prodImage = value; }
}
        //Below as the Class methods for some DB operations. 
    public Product getWomenProduct(string prodID)
    {
        Product prodDetail = null;
        string prod_Name, prod_Desc, Prod_Image;
        decimal unit_Price;
        string queryStr = "SELECT ProductID, Name, Description, Model, UnitPrice FROM WomenProduct WHERE ProductID = @ProdID";
        SqlConnection conn = new SqlConnection(_connStr);
        SqlCommand cmd = new SqlCommand(queryStr, conn);
        cmd.Parameters.AddWithValue("@ProdID", prodID);
        conn.Open();
        SqlDataReader dr = cmd.ExecuteReader();
        if (dr.Read())
        {
            prodID = dr["ProductID"].ToString();
            prod_Name = dr["Name"].ToString();
            prod_Desc = dr["Description"].ToString();
            Prod_Image = dr["Model"].ToString();
            unit_Price = decimal.Parse(dr["UnitPrice"].ToString());

            prodDetail = new Product(prodID, prod_Name, prod_Desc, unit_Price, Prod_Image);
        }
        else
        {
            prodDetail = null;
        }
        conn.Close();
        dr.Close();
        dr.Dispose();
        return prodDetail;
    }
    public Product getMenProduct(string prodID)
    {
        Product prodDetail = null;
        string prod_Name, prod_Desc, Prod_Image;
        decimal unit_Price;
        string queryStr = "SELECT ProductID, Name, Description, Model, 
    public List<Product> getWomenProductAll()
    {
        List<Product> prodList = new List<Product>();
        string prod_Name, prod_Desc, Prod_Image, prod_ID;
        decimal unit_Price;
        string queryStr = "SELECT ProductID, Name, Description, Model, UnitPrice FROM WomenProduct Order By Name";
        SqlConnection conn = new SqlConnection(_connStr);
        SqlCommand cmd = new SqlCommand(queryStr, conn);            
        conn.Open();
        SqlDataReader dr = cmd.ExecuteReader();
        while (dr.Read())
        {
            prod_ID = dr["ProductID"].ToString();
            prod_Name = dr["Name"].ToString();
            prod_Desc = dr["Description"].ToString();
            Prod_Image = dr["Model"].ToString();
            unit_Price = decimal.Parse(dr["UnitPrice"].ToString());
            Product a = new Product(prod_ID, prod_Name, prod_Desc, unit_Price, Prod_Image);
            prodList.Add(a);
        }
        conn.Close();
        dr.Close();
        dr.Dispose();
        return prodList;
    }

错误:在所选数据源(asp.net c#、sql)上找不到名为“ProductID”的字段或属性

产品类具有 Product_ID 属性,但绑定位于"ProductID"上(无下划线)。您需要修复其中之一。