简体   繁体   中英

Problem in populating gridview from stored procedure

I have a Gridview which I'm populating from a Stored Procedure which accepts 3 parameters. First parameter gets its value from a form variable Request.Form["uid"], second and third get its value from DropDownLists SelectedValue property. Second and third parameter should accept NULL if nothing is selected from DropDownLists. I have also set the ConvertEmptyStringToNull property of the parameters to true. The Gridview is still not populating. 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.

My SQLDataSource for the Gridview looks like this:

<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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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