2012-11-29 43 views
0

我有一個asp.net頁面,其中列出了帶有編輯/刪除按鈕的產品(從數據庫中提取)。用戶可以通過點擊編輯按鈕來編輯產品。我已經能夠根據所選產品將數據從數據庫中提取到文本框中。但是,我在下拉框中獲取重複的項目。它只應該有32個項目,並有160個項目(每個項目出現5次)。我已經使用Items.Clear(),但仍然得到重複。此外,收存箱只顯示列表中的第一項,而不是當前在數據庫中的該產品的適當項目。任何人都可以看到我可能做錯了什麼?填充數據庫中的下拉列表

謝謝。

protected void Page_Load(object sender, EventArgs e) 
    { 
     this.Master.HighlightNavItem("Products"); 
     string Mode = (Request.QueryString["Mode"]); 

     //Upon opening page, if this is an edit to existing product (populate product data) 
     if (Mode == "E") 
     { 
      if (!IsPostBack) 
      { 
       int ProductID = Int32.Parse(Request.QueryString["ID"]); 

       //Declare the connection object 
       SqlConnection Conn = new SqlConnection(); 
       Conn.ConnectionString = ConfigurationManager.ConnectionStrings["MyDatabase"].ConnectionString; 

       //Connect to the db 
       Conn.Open(); 

       //Define the query      
       //string sql = "SELECT dbo.Vendor.VendorName, dbo.Vendor.VendorID, dbo.Product.ProductName, dbo.Product.ProductNumber, dbo.lu_Category.CategoryID, dbo.lu_Category.Description FROM dbo.Product INNER JOIN dbo.Vendor ON dbo.Product.VendorID = dbo.Vendor.VendorID INNER JOIN dbo.lu_Category ON dbo.Product.CategoryID = dbo.lu_Category.CategoryID WHERE [email protected]"; 
       string sql = "SELECT ProductName, ProductNumber, ProductDescription, Cost, Markup, Unit, QtyOnHand, ShippingWeight, dbo.Vendor.VendorID, VendorName, dbo.lu_Category.CategoryID, Description FROM Vendor, Product, lu_Category WHERE [email protected]"; 

       //Declare the Command 
       SqlCommand cmd = new SqlCommand(sql, Conn); 

       //Add the parameters needed for the SQL query 
       cmd.Parameters.AddWithValue("@ProductID", ProductID); 

       //Declare the DataReader 
       SqlDataReader dr = null; 

       //Fill the DataReader 
       dr = cmd.ExecuteReader(); 

       //Loop through the DataReader 
       ddlVendor.Items.Clear(); 
       while (dr.Read()) 
       { 

        txtProductName.Text = dr["ProductName"].ToString(); 
        txtProductNo.Text = dr["ProductNumber"].ToString(); 
        txtDescription.Text = dr["ProductDescription"].ToString(); 
        txtCost.Text = dr["Cost"].ToString(); 
        txtMarkup.Text = dr["Markup"].ToString(); 
        txtUnit.Text = dr["Unit"].ToString(); 
        txtQty.Text = dr["QtyOnHand"].ToString(); 
        txtWeight.Text = dr["ShippingWeight"].ToString(); 
        ListItem li = new ListItem(); 
        li.Text = dr["VendorName"].ToString(); 
        li.Value = dr["VendorID"].ToString(); 
        ddlVendor.Items.Add(li); 
+3

加入您的查詢時必須有問題。這就是爲什麼它會在你的組合框 – phadaphunk

回答

1

你應該改變你的SQL query並刪除,類型的連接。
然後直接在你的數據庫中測試你的查詢,以確保你沒有得到雙打。

你的代碼的其餘部分看起來很好,所以我敢肯定,測試你的查詢將解決您的問題。
不要使用Comma Joins它已棄用。

0

我想你應該使用內部或外部連接,讓您的數據。用','分開的連接你從兩個表中接收數據。就像一張桌子的32行一樣被其他表的5行所佔用。

+0

中重演。我在Sql中運行了查詢並獲得了6000多條記錄!好吧,我在VB中更改了我的查詢,以使用帶有ProductID參數的視圖。現在我只能在下拉菜單中找回1個項目,這是該產品的數據庫中的當前項目。但我需要找回所有32個下拉項目,並突出顯示所選項目。這裏是我的新查詢:string sql =「SELECT * FROM ProductsView WHERE ProductID = @ ProductID」; – user1576304

+0

您是否刪除了items.clear? – CodeSpread

+0

沒有沒有刪除。這是觀點,但不確定它是否正確。它返回37條記錄。供應商tbl有32個項目&lu_Category tbl有5個項目,所以它看起來就是結合它們。我使用內部連接。那是對的嗎? – user1576304

相關問題