【问题标题】:pass null value to stored procedure in sqldatasource when page is loading加载页面时将空值传递给sqldatasource中的存储过程
【发布时间】:2013-10-21 12:29:25
【问题描述】:

我有一些用于输入搜索值的文本框。我有一个存储过程,它有 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>

【问题讨论】:

    标签: c# asp.net


    【解决方案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>
    

    【讨论】:

    • +1 为正确答案,并且OP必须在该列中设置allow null,否则会引发错误
    • @RameshRajendran 。是的,从这个问题我假设他在存储过程结束时有一个空句柄。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-13
    相关资源
    最近更新 更多