从数据库填充下拉列表

时间:2012-11-29 18:29:05

标签: c# asp.net sql

我有一个asp.net页面,列出了带有编辑/删除按钮的产品(从数据库中提取)。用户可以通过单击编辑按钮来编辑产品。我已经能够根据所选产品将数据从数据库提取到文本框。但是,我在下拉框中收到重复的项目。它应该只有32个项目,有160个项目(每个项目出现5次)。我使用过Items.Clear(),但仍然有重复项。此外,Dropbox只显示列表中的第一个项目,而不是当前位于db中的该产品的相应项目。任何人都可以看到我可能做错了什么?

感谢。

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

2 个答案:

答案 0 :(得分:1)

您应该更改SQL query并删除,类型的联接。
然后直接在数据库中测试您的查询,以确保您没有获得双打。

其余代码看起来很好,所以我确定测试您的查询会解决您的问题 不要使用它已被弃用的Comma Joins

答案 1 :(得分:0)

我认为您应该使用内部或外部联接来获取数据。使用','分隔连接,您将从两个表中接收数据。像一个表的32行正在与5行其他表进行匹配。