SqlDataSource没有绑定多个参数

时间:2016-06-01 13:43:21

标签: asp.net data-binding sqldatasource

我有一个非常简单的ASP.NET页面,它有两个文本框,一个按钮,一个SqlDataSource和一个GridView。 SqlDataSource从两个文本框中获取它的两个参数,然后将它传递给存储过程。我希望用户能够填写字段这两个字段,并将结果显示在GridView中。以下是相关的页面代码:

<table>
    <tr>
        <td>
            <asp:Label ID="Label4" runat="server" Text="Sales Order * Line Number:"></asp:Label>
        </td>
        <td>
            <asp:TextBox ID="txtSalesOrderLineNumber" runat="server"></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td class="auto-style1">
            <asp:Label ID="Label2" runat="server" Text="Shipping box number:"></asp:Label>
        </td>
        <td class="auto-style1">
            <asp:TextBox ID="txtShipBoxNumber" runat="server"></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td class="auto-style1">
            &nbsp;</td>
        <td class="auto-style1">
            <asp:Button ID="cmdSearch" runat="server" Text="Search" />
        </td>
    </tr>
</table>

<asp:SqlDataSource ID="SqlDataSource_GetShipBoxesBySearch" runat="server" ConnectionString="<%$ ConnectionStrings:MyApp.My.MySettings.ConnStr %>" SelectCommand="usp_PerformSearch" SelectCommandType="StoredProcedure">
    <SelectParameters>
        <asp:ControlParameter ControlID="txtSalesOrderLineNumber" Name="pSalesOrderLineNumber" PropertyName="Text" Type="String" />
        <asp:ControlParameter ControlID="txtShipBoxNumber" Name="pShipBoxNumber" PropertyName="Text" Type="String" />
    </SelectParameters>
</asp:SqlDataSource>

为简化起见,cmdSearch按钮只调用GridView的数据绑定:

   Protected Sub cmdSearch_Click(sender As Object, e As EventArgs) Handles cmdSearch.Click
            gvShipBoxes.DataBind()
    End Sub

GridView代码:

<asp:GridView ID="gvShipBoxes" runat="server" AllowPaging="True" 
AllowSorting="True" AutoGenerateColumns="False" CellPadding="4" DataKeyNames="ShipBoxID" 
DataSourceID="SqlDataSource_GetShipBoxesBySearch" ForeColor="#333333" GridLines="None">
        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
        <Columns>
            ...columns in here...
        </Columns>
        <EditRowStyle BackColor="#999999" />
        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
        <SortedAscendingCellStyle BackColor="#E9E7E2" />
        <SortedAscendingHeaderStyle BackColor="#506C8C" />
        <SortedDescendingCellStyle BackColor="#FFFDF8" />
        <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>

我的问题是,如果两个字段都填写完毕,一切正常,但如果只填写一个,则不会发生任何事情。我使用SQL Server Profiler来验证这一点。如果两个字段都填写完毕,我会在分析器中看到一个事件,该事件显示使用这两个参数调用我的存储过程。但是,如果只填写一个,则分析器中不会显示任何内容。因此,当只填写一个文本框时,它甚至不调用存储过程。我已经尝试在两个控制参数上设置“DefaultValue”和“ConvertEmptyStringToNull”属性,但这没有任何区别。

我可能遗漏了一些简单的东西,我会很感激任何建议。谢谢!

2 个答案:

答案 0 :(得分:0)

你试过这个吗?

SqlDataSource_GetShipBoxesBySearch.SelectParmeter['pSalesOrderLineNumber'].DefaultValue = 'X';
SqlDataSource_GetShipBoxesBySearch.SelectParmeter['pShipBoxNumber'].DefaultValue = 'Y';
gvShipBoxes.DataBind();

答案 1 :(得分:0)

根据Umer的建议,我从网页代码中删除了参数声明。它现在看起来像这样:

<asp:SqlDataSource ID="SqlDataSource_GetShipBoxesBySearch" runat="server" ConnectionString="<%$ ConnectionStrings:MyApp.My.MySettings.ConnStr %>" SelectCommand="usp_PerformSearch" SelectCommandType="StoredProcedure">
        <SelectParameters>
        </SelectParameters>
    </asp:SqlDataSource>

然后我按钮代码更改为:

Protected Sub cmdSearch_Click(sender As Object, e As EventArgs) Handles cmdSearch.Click

Try
    'Create the parameters for the SqlDataSource
    Dim p1 As Parameter = SqlDataSource_GetShipBoxesBySearch.SelectParameters("pSalesOrderLineNumber")
    Dim p2 As Parameter = SqlDataSource_GetShipBoxesBySearch.SelectParameters("pShipBoxNumber")

    'In any case, remove the existing parameters (on the 2nd search, don't want to keep previous values)
    SqlDataSource_GetShipBoxesBySearch.SelectParameters.Remove(p1)
    SqlDataSource_GetShipBoxesBySearch.SelectParameters.Remove(p2)

    'If there is a value in the textbox, add it as a parameter value...
    If txtSalesOrderLineNumber.Text <> "" Then
        SqlDataSource_GetShipBoxesBySearch.SelectParameters.Add("pSalesOrderLineNumber", txtSalesOrderLineNumber.Text.ToString)
    End If

    If txtShipBoxNumber.Text <> "" Then
        SqlDataSource_GetShipBoxesBySearch.SelectParameters.Add("pShipBoxNumber", txtShipBoxNumber.Text.ToString)
    End If

    gvShipBoxes.DataBind()

Catch ex As Exception
    lblError.Text = "Error in [cmdSearch_Click]: " & ex.Message.ToString
End Try

End Sub

一旦我这样做,我验证了Sql Profiler中传递的内容。正如我所希望的那样,当只填写一个文本框时,只传递了一个参数。两者都填写完毕后,两个参数都通过了。在这两种情况下,GridView都反映了存储过程的正确结果。

感谢您的帮助!