2016-07-06 4 views
0

SQLデータを表示するためにasp.netのGridviewを使用していますエラー:以下エラー:選択したデータソース(asp.net c#、sql)で「ProductID」という名前のフィールドまたはプロパティが見つかりません

A field or property with the name 'ProductID' was not found on the selected data source

は私の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(); 
} 

くださいadvis私のコーディングのおかげで何かエラーがある場合e。ここ は、製品全体のクラスです:

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; 
    } 
+0

このエラーを表示する行は何ですか?また、どのような例外が正確に表示されていますか? – Kinetic

+0

'' System.Web.dllで 'System.Web.HttpException'型の例外が発生しましたが、ユーザコードで処理されませんでした。 "Thanks @KiNeTiC –

+0

「GvProduct.Databind()クラス? – Kinetic

答えて

2

あなたのProductクラスは、PRODUCT_IDプロパティを持っていますが、あなたの結合は、「商品コード」(ノーアンダースコア)です。これらのうちの1つを修正する必要があります。

+0

ありがとうございました!これは今すぐ働きます –

+1

@denissapopあなたがあなたの命名に一貫していれば、これはもっと簡単になります。 prod_ID、prodID、ProductIDおよびProduct_IDはすべて同じものを参照します。 –

+0

これを答えとして気軽に記入してください! ;) – Kinetic

関連する問題