当页面加载时,将null值传递给sqldatasource中的存储过程

时间:2013-10-21 12:29:26

标签: c# asp.net

我有一些用于输入搜索值的文本框。我有一个存储过程有2个日期时间,1个int和1个char参数。我将SqlDataSource附加到TextBox并将默认值设置为null但是当我运行我的项目时,将空传递给存储过程,但我的存储过程需要空值。如何通过绑定到文本框的SqlDataSource传递null?

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:DB_MosquesAffairsConnectionString3 %>"
    SelectCommand="mqa.S_Conference_Select_Date"
    SelectCommandType="StoredProcedure" onload="SqlDataSource1_Load"
    onselecting="SqlDataSource1_Selecting">
    <SelectParameters>
        <asp:ControlParameter ControlID="TextBox1" DbType="Date" DefaultValue="null"
            Name="StartDate" PropertyName="Text" />
        <asp:ControlParameter ControlID="TextBox2" DbType="Date" DefaultValue="null" 
            Name="EndDate" PropertyName="Text" />
        <asp:ControlParameter ControlID="TextBox3" DefaultValue="null" 
            Name="ConferenceName" PropertyName="Text" Type="String" />
        <asp:ControlParameter ControlID="TextBox4" DefaultValue="null" 
            Name="ConferenceID" PropertyName="Text" Type="Int32" />
    </SelectParameters>
</asp:SqlDataSource>

1 个答案:

答案 0 :(得分:1)

您可以在ControlParameter

中使用以下属性/属性
ConvertEmptyStringToNull="true" DefaultValue=""


  <SelectParameters>
                     <asp:ControlParameter ControlID="TextBox1" DbType="Date"     ConvertEmptyStringToNull="true" DefaultValue=""
                         Name="StartDate" PropertyName="Text" />
                     <asp:ControlParameter ControlID="TextBox2" DbType="Date"     ConvertEmptyStringToNull="true" DefaultValue=""
                         Name="EndDate" PropertyName="Text" />
                     <asp:ControlParameter ControlID="TextBox3"     ConvertEmptyStringToNull="true" DefaultValue="" 
                         Name="ConferenceName" PropertyName="Text" Type="String" />
                     <asp:ControlParameter ControlID="TextBox4"     ConvertEmptyStringToNull="true" DefaultValue=""
                         Name="ConferenceID" PropertyName="Text" Type="Int32" />
                 </SelectParameters>