简体   繁体   English

从数据库填充下拉列表

[英]Populating a dropdown list from database

I have a asp.net page listing products (pulled from a database) with edit/delete buttons. 我有一个asp.net页面,其中列出了带有编辑/删除按钮的产品(从数据库中拉出)。 The user can edit the product by clicking the edit button. 用户可以通过单击编辑按钮来编辑产品。 I've been able to pull in data from the db to the textboxes based on the product selected. 我已经能够根据所选的产品将数据从数据库中提取到文本框中。 However, I am getting duplicate items in the dropdown box. 但是,我在下拉框中得到重复的项目。 It's only supposed to have 32 items and has 160 items (each item is appearing 5 times). 它应该只有32个项目,但有160个项目(每个项目显示5次)。 I've used Items.Clear() but am still getting duplicates. 我使用了Items.Clear(),但仍在重复。 Also the dropbox just shows the first item in the list rather than the appropriate item for that product that is currently in the db. 下拉框也仅显示列表中的第一项,而不是数据库中当前该产品的适当项。 Can anyone see what I may be doing wrong? 谁能看到我可能在做错什么?

Thanks. 谢谢。

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

You should change your SQL query and remove the , type of joins. 你应该改变你的SQL query和删除,的连接类型。
Then test your query directly in your database to make sure you don't get doubles. 然后直接在数据库中测试查询,以确保不会出现双打。

The rest of your code looks fine so i'm sure testing your query will solve your problem. 您其余的代码看起来不错,所以我确定测试您的查询可以解决您的问题。
Do not use Comma Joins it's deprecated. 不要使用不推荐使用的Comma Joins

I think you should have used inner or outer join to get your data. 我认为您应该使用内部或外部联接来获取数据。 With ',' separated joins you are receiving data from both tables. 使用“,”分隔的联接,您将从两个表中接收数据。 like 32 rows of one table are getting appneded with 5 rows of other table. 像是一张桌子的32行和另一张桌子的5行一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM