<asp:ControlParameter - pass null value to DB v

2019-06-15 07:35发布

问题:

I'm having problems passing a null value to a stored procedure, e.g. if an option isn't selected on a dropdown how can this be passed as null to through the sqdatasource to the database.

I currently have a number of dropdown controls on my page which are holding paramater values to get passed to a stored procedure.

When I select some values from these controls the gridview will display the results that it should, but what I'm having a problem with is when no values are selected is passing a null value to the SP. I've checked the SP and when I execute it and pass in null values it gets me the correct results so I'm happy with the SP. I've tried

ConvertEmptyStringToNull="true" DefaultValue=""

settings in the control paramater with no luck, and the dropdown's "ALL" option has a value of ""

The code for the sqldatasource is:

                    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:scConnString %>"
                    SelectCommand="spGetOrgTickets" SelectCommandType="StoredProcedure" CancelSelectOnNullParameter="false">
                    <SelectParameters>
                        <asp:SessionParameter Name="org_id" Type="Int32" SessionField="org_id" DefaultValue="" ConvertEmptyStringToNull="false" />
                        <asp:ControlParameter ControlID="drpPriority" Name="priority_id" PropertyName="SelectedValue"
                            Type="Int32" ConvertEmptyStringToNull="true" DefaultValue="" />
                        <asp:ControlParameter ControlID="drpStatus" Name="status_id" PropertyName="SelectedValue"
                            Type="Int32" ConvertEmptyStringToNull="true" DefaultValue=""/>
                    </SelectParameters>
                </asp:SqlDataSource>

One of the dropdown's is :

                            <asp:DropDownList Style="width: 100%" ID="drpStatus" runat="server" class="field select"
                                AppendDataBoundItems="True" AutoPostBack="True" DataSourceID="SqlDataSource2_Status"
                                DataTextField="status" DataValueField="status_id">
                                <asp:ListItem Value="">All</asp:ListItem>
                            </asp:DropDownList>

Any ideas?

Thanks

回答1:

Sorted...just need CancelSelectOnNullParameter="false" within sqldatasource.



标签: asp.net tsql