2011-03-24 26 views
4

我在asp.net中製作了一個WebForm,並且想要顯示帶有asp的數據的gridview:SqlDataSource
我的問題變爲當我嘗試跳過(傳遞null)一些參數。在WebForm中跳過SqlDataSource中sql參數的設置值

這裏是我的代碼

在aspx文件SqlDataSource的一些片段看起來像

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
     ConnectionString="<%$ ConnectionStrings:ApplicationServices %>" 
     SelectCommand="art_bat_art" SelectCommandType="StoredProcedure"> 
     <SelectParameters> 
      <asp:Parameter Name="art_naz" DefaultValue="HLA" ConvertEmptyStringToNull="true" Type="String" /> 
      <asp:Parameter Name="art_sifra"  DbType="String" Direction="Input"/> 
      <asp:Parameter Name="vrsta_id" DefaultValue="3" ConvertEmptyStringToNull="true" Type="Int32" />  
      <asp:Parameter Name="podvrsta_id" DefaultValue="13" ConvertEmptyStringToNull="true" 
       Type="Int32" /> 
      <asp:Parameter Name="podgrupa2" DefaultValue="1365" ConvertEmptyStringToNull="true" Type="Int32"  /> 
      <asp:Parameter Name="podosobine" DefaultValue="1:9241" ConvertEmptyStringToNull="true" 
       Type="String" /> 
      <asp:Parameter Name="podosobineOR" DefaultValue="true" ConvertEmptyStringToNull="true" 
       Type="Boolean" /> 
     </SelectParameters> 

我的網格看起來是這樣的:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
     DataKeyNames="id" DataSourceID="SqlDataSource1"> 

和麻煩進來後面的代碼 的一個我的存儲過程的參數是可選的,如果我在SQL中將它作爲null傳遞,則存儲過程是 ,無論如何。

我試圖跳過並傳遞參數如下

public partial class _Default : System.Web.UI.Page 
    { 
     protected void Page_Load(object sender, EventArgs e) 
     { 


     } 
     protected void Button1_Click1(object sender, EventArgs e) 
     { 
      SqlDataSource1.SelectParameters["art_sifra"].DefaultValue = DBNull.Value; //Cannot implicitly convert type 
      SqlDataSource1.SelectParameters["art_sifra"].DefaultValue = System.Data.SqlTypes.SqlString.Null; //Cannot implicitly convert type 
      SqlDataSource1.SelectParameters["art_sifra"].DefaultValue = System.Data.SqlTypes.SqlString.Null.ToString(); //IT compiles but i get NULL as text in SQL 
      //IF I Just skip seting of this parametar, My SqlDataSource won't call stored proce 
      SqlDataSource1.SelectParameters["art_sifra"].DefaultValue = string.Empty; //Again no execution of stored proce 
      //Even if I call SqlDataSource1.DataBind(); //nothing cames to sql 
      GridView1.DataBind(); 
     } 
    } 

回答

6

您必須編輯SQL(或存儲過程)才能處理可選參數。

即:如果您有:
SELECT blah FROM yourTable WHERE art_sifra = @art_sifra

將其更改爲:
SELECT blah FROM yourTable WHERE (art_sifra = @art_sifra OR @art_sifra = '')

,然後就改變ConvertEmptyStringToNull="false"和你選擇的參數設置爲""SqlDataSource1.SelectParameters["art_sifra"].DefaultValue = "";

還需要在您的數據源上設置CancelSelectOnNullParameter="false"

1
DBNull.Value; //Cannot implicitly convert type 

您是否嘗試過鑄造的呢?例如。

DBNull.Value.Tostring() 
+0

SqlDataSource1.SelectParameters [「art_sifra」]。DefaultValue = DBNull.Value.ToString();根本不要調用存儲過程。我沒有得到任何錯誤,但我也沒有得到任何數據 – adopilot 2011-03-24 11:07:24

+0

@adopilot:你想發佈你的存儲過程,所以我們可以檢查正在處理NULL參數嗎? – openshac 2011-03-24 12:33:14