2015-08-17 53 views
0

可能有一個簡單的答案,我很笨,但我需要幫助和局外人的觀點。所以我在數據庫中有3個表:Category(CategoryID,Description),Manufacturer(ManufacturerID,Name)和Products(ProductID,CategoryID,ManufactureID,Model和其他廢話)。 我需要基於類別DropDownList的選擇填充製造商DropDownList,但我的代碼拋出「列'製造商ID'不屬於表格表格。」,拼寫是正確的,所以我一定是做錯了什麼,但我不能圖瞭解它是什麼。任何建議都會受到歡迎,因爲這會殺死我。這是我的代碼。C#使用所選項目從數據庫填充下拉列表

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 
     } 
    } 

引發異常存儲過程IM在 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(); 

但它也沒有工作,就像我的拼寫之前提到的製造商表中的列相匹配。

感謝您的幫助

回答

2

你的數據表,您正在使用下拉列表約束力,不包含ManufacturerID列。包括此字段到您的選擇查詢,然後嘗試

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

薩欽,你是一個天才! – Max

+0

非常感謝! – Max

相關問題