简体   繁体   中英

Populating a dropdown list from database

I have a asp.net page listing products (pulled from a database) with edit/delete buttons. 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). I've used Items.Clear() but am still getting duplicates. 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.
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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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