簡體   English   中英

C#使用所選項目從數據庫填充下拉列表

[英]C# populating dropdownlist from database using selected item

可能有一個簡單的答案,我很傻,但是我需要幫助和局外人的觀點。 因此,我在數據庫中有3個表:類別(類別ID,描述),制造商(制造商ID,名稱)和產品(產品ID,類別ID,制造ID,模型和其他廢話)。 我需要根據類別DropDownList的選擇填充制造商DropDownList,但是我的代碼拋出“ Column'ManufacturerID'不屬於Table Table。”,拼寫是正確的,所以我必須做錯了事,但是我無法弄清楚這是什么。 任何建議都將受到歡迎,因為這殺死了我。 這是我的代碼。

public DataSet Manufacturers_Load_by_Category(int category)
    {
        dwas = new SqlDataAdapter("Manufacturers_Load_By_Category", conn); // stored procedure created in database
        ds = new DataSet();
        dwas.SelectCommand.CommandType = CommandType.StoredProcedure;
        dwas.SelectCommand.Parameters.AddWithValue("@category", category); 
        try
        {
            dwas.SelectCommand.Prepare();
            dwas.Fill(ds);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return ds;
            }
            else
            {
                return null;
            }
        }
        catch (Exception ex)
        {
            ex.ToString();
            return null;
        }
        finally
        {
            conn.Close();
        }
    }   

這是存儲過程即時通訊使用(它的工作原理,我檢查了100次)

ALTER procedure [dbo].[Manufacturers_Load_By_Category]
@category int
as
SELECT        Manufacturers.Name
FROM            Manufacturers INNER JOIN
              Products ON Manufacturers.ManufacturerID =      Products.ManufacturerID
WHERE        (Products.CategoryID = @category)

最后是我的失敗點:(

protected void dlCategory_SelectedIndexChanged(object sender, EventArgs e)
    {
        string cat = dlCategory.SelectedValue.ToString(); // get the value the user selected from the manufactuers drop down list
        Datalayer DL = new Datalayer(); // get access to the data layer
        DataSet ds = new DataSet();
        int x = 0;
        //lblError.Visible = false;
        dlManufacturer.Items.Clear(); // clear all the values in the models drop down list
        dlManufacturer.Items.Add(new ListItem("Select", "-1")); // reload the select option

        if (cat != "-1")
        {
            ds = DL.Manufacturers_Load_by_Category(Convert.ToInt32(cat));
            if (ds != null)
            {

                while (x < ds.Tables[0].Rows.Count)
                {
                    //exception is thrown here
                    // "Column 'ManufacturerID' does not belong to table Table."
                    dlManufacturer.Items.Add(new ListItem(ds.Tables[0].Rows[x]["Name"].ToString(), ds.Tables[0].Rows[x]["ManufacturerID"].ToString()));

                    x++;
                }
            }
            else
            {
                //failed to load models
            }
        }
        else
        {
            //invalid manufacturer
        }
    }

在dlManufacturer.Items.Add(new ListItem(ds.Tables [0] .Rows [x] [“ Name”]。ToString(),ds.Tables [0] .Rows [x] [“ ManufacturerID” ] .ToString()));

我也試過

  //dlManufacturer.DataSource = ds;
                    //dlManufacturer.DataTextField = "Name";
                    //dlManufacturer.DataValueField = "ManufacturerID";
                    //dlManufacturer.DataBind();

但是它也不起作用,就像我在拼寫匹配“制造商表”中的列之前提到的那樣。

感謝您的任何幫助

與dropdownlist綁定的數據表不包含ManufacturerID列。 在您選擇的查詢中包含此字段,然后嘗試

SELECT Manufacturers.Name, Manufacturers.ManufacturerID 
FROM   Manufacturers INNER JOIN Products ON Manufacturers.ManufacturerID = Products.ManufacturerID
WHERE  Products.CategoryID = @category

暫無
暫無

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

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