C#使用所选项目从数据库填充下拉列表

时间:2015-08-17 16:45:31

标签: c# sql asp.net

可能有一个简单的答案而我是愚蠢的,但我需要帮助和局外人的观点。所以我在数据库中有3个表:类别(CategoryID,描述),制造商(ManufacturerID,名称)和产品(ProductID,CategoryID,ManufactureID,Model和其他废话)。 我需要根据Category DropDownList的选择填充制造商DropDownList,但是我的代码抛出“Column'ManufactIDID'不属于表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();

但它也没有用,就像我在拼写与制造商表中的列匹配之前提到的那样。

感谢您的帮助

1 个答案:

答案 0 :(得分:2)

您使用下拉列表绑定的数据表不包含ManufacturerID列。将此字段包含在您的选择查询中,然后尝试

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