2016-03-16 12 views
2

我試圖用ASP .NET無法使用ASP.NET的

按照同一個存儲過程來填充同一頁面上的多個DropDownList的同一個存儲過程來填充同一頁面上的多個DropDownList的是我的代碼:

1)網頁加載事件

if(!Page.IsPostBack){ 
    fillDropDown(SearchByBrand); 
    fillDropDown(SearchByBrand1); 
} 

2)fillDropDown方法

protected void fillDropDown(DropDownList list) { 
    try 
    { 
     cmd = new SqlCommand("fillDropdownWithBrandName", conn); 
     cmd.CommandType = CommandType.StoredProcedure; 
     reader = cmd.ExecuteReader(); 

     list.DataSource = reader; 
     list.DataTextField = "BrandName"; 
     list.DataValueField = "BrandID"; 
     list.DataBind(); 
     list.Items.Insert(0, new ListItem("--Select Brand--", "0")); 
    } 
    catch(Exception e){ 
     errorMessage = e.StackTrace; 
    } 
    finally{    
     cmd.Dispose(); 
     conn.Close(); 
    } 
} 

3)下拉列表,我已經使用 1下拉

<asp:DropDownList ID="SearchByBrand" CssClass="form-control" runat="server"> 
</asp:DropDownList> 

第二個下拉列表中的UpdatePanel進一步爲bootstrap模式,其獲得的按鈕點擊打開裏面。

<asp:DropDownList ID="SearchByBrand1" CssClass="form-control" runat="server"> 
</asp:DropDownList> 

存儲過程

ALTER PROCEDURE fillDropdownWithBrandName 
AS 
BEGIN 
SELECT BrandID,BrandName FROM Brand 
END 

只有一個下拉列表被填充。可能是什麼問題呢?

回答

1

有一些問題與您的代碼:

  • 在fillDropDown你關閉連接的方法,但你不要在任何其他可見的地方再次打開它
  • 你應該處置讀者對象或內部使用使用語句
+0

是的我只在Page_Load事件中打開連接。我重新打開fillDropDown方法中的連接,並獲得所需的結果。謝謝 !! – user4185336