简体   繁体   English

从存储过程填充 gridview 时出现问题

[英]Problem in populating gridview from stored procedure

I have a Gridview which I'm populating from a Stored Procedure which accepts 3 parameters.我有一个 Gridview,我从一个接受 3 个参数的存储过程中填充它。 First parameter gets its value from a form variable Request.Form["uid"], second and third get its value from DropDownLists SelectedValue property.第一个参数从表单变量 Request.Form["uid"] 中获取其值,第二个和第三个参数从 DropDownLists SelectedValue 属性中获取其值。 Second and third parameter should accept NULL if nothing is selected from DropDownLists.如果从 DropDownLists 中未选择任何内容,则第二个和第三个参数应接受 NULL。 I have also set the ConvertEmptyStringToNull property of the parameters to true.我还将参数的 ConvertEmptyStringToNull 属性设置为 true。 The Gridview is still not populating. Gridview 仍未填充。 But when I pass parameters manually and execute the stored procedure from SQL Server Management Studio as well as Visual Studio's test query window, it works fine.但是当我手动传递参数并从 SQL Server Management Studio 以及 Visual Studio 的测试查询 window 执行存储过程时,它工作正常。

My SQLDataSource for the Gridview looks like this: Gridview 的我的 SQLDataSource 如下所示:

<asp:SqlDataSource ID="SqlGVDownloadHistory" CancelSelectOnNullParameter="false" runat="server" ConnectionString="<%$ ConnectionStrings:xxx %>"
            SelectCommand="spDHR" SelectCommandType="StoredProcedure">
            <SelectParameters>
                <asp:FormParameter FormField="uid" Name="UIDUser" Type="String" />
                <asp:ControlParameter ControlID="ddlSelectDocument" Name="FileName"
                    PropertyName="SelectedValue" Type="String" ConvertEmptyStringToNull="true" />
                <asp:ControlParameter ControlID="ddlSelectCCO" Name="UIDSelect" PropertyName="SelectedValue"
                    Type="String" ConvertEmptyStringToNull="true" />
            </SelectParameters>
        </asp:SqlDataSource>

Please help!!请帮忙!!

Try setting the default value for your two input parameters on your stored procedure to null.尝试将存储过程中的两个输入参数的默认值设置为 null。

Try this.尝试这个。

<asp:SqlDataSource ID="SqlGVDownloadHistory" runat="server" 
            ConnectionString="<%$ ConnectionStrings:xxx %>" 
            SelectCommand="spDHR"
            SelectCommandType="StoredProcedure" 
            OnSelecting="SqlGVDownloadHistory_Selecting">
    <SelectParameters>
        <asp:FormParameter FormField="uid" Name="UIDUser" Type="String" />
    </SelectParameters>
</asp:SqlDataSource>

And on Code Behind以及背后的代码

protected void SqlGVDownloadHistory_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{
    SqlGVDownloadHistory.SelectParameters.Remove("FileName");
    SqlGVDownloadHistory.SelectParameters.Remove("UIDSelect");
    if (ddlSelectDocument.SelectedIndex > 0)
    {
        SqlGVDownloadHistory.SelectParameters.Add("FileName", ddlSelectDocument.SelectedValue);
    }
    if (ddlSelectCCO.SelectedIndex > 0)
    {
        SqlGVDownloadHistory.SelectParameters.Add("UIDSelect", ddlSelectCCO.SelectedValue);
    }
}

The idea is to set the parameters before the data retrieval operation occurs.这个想法是在数据检索操作发生之前设置参数。

As per MSDN documentation on COntrolParameters , I don't see asp:DropDownList Selectedvalue being set as a ControlValuePropertyAttribute根据关于 COntrolParameters 的 MSDN 文档,我没有看到asp:DropDownList Selectedvalue被设置为ControlValuePropertyAttribute

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM