簡體   English   中英

從數據庫填充下拉列表

[英]Populating a dropdown list from database

我有一個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 ProductID=@ProductID";
                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 ProductID=@ProductID";

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

你應該改變你的SQL query和刪除,的連接類型。
然后直接在數據庫中測試查詢,以確保不會出現雙打。

您其余的代碼看起來不錯,所以我確定測試您的查詢可以解決您的問題。
不要使用不推薦使用的Comma Joins

我認為您應該使用內部或外部聯接來獲取數據。 使用“,”分隔的聯接,您將從兩個表中接收數據。 像是一張桌子的32行和另一張桌子的5行一樣。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM