I have a very simple ASP.NET page that has two textboxes, a button, a SqlDataSource and a GridView. The SqlDataSource gets it's two parameters from the two textboxes, which it then passes on to a stored procedure. I want to have the ability for a user to fill out either field or both fields and have the results display in the GridView. Here is the relevant page code:
<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">
</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>
To simplify things, the cmdSearch button just calls the databind for the GridView:
Protected Sub cmdSearch_Click(sender As Object, e As EventArgs) Handles cmdSearch.Click
gvShipBoxes.DataBind()
End Sub
GridView code:
<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>
My problem is that if both fields are filled out, everything works, but if only one is filled out, nothing happens. I have used SQL Server Profiler to verify this. If both fields are filled out, I get an event in the profiler that shows my stored procedure being called with those two parameters. However, if only one is filled out, nothing is shown in profiler. Therefore, it is not even calling the stored procedure when only one textbox is filled out. I have experimented with setting the "DefaultValue" and "ConvertEmptyStringToNull" properties on both of the control parameters, but that made no difference.
I am probably missing something simple, and I would appreciate any suggestions. Thank you!
DataBind
is actually called at all if only one textbox is filled out? – Andrei Jun 1 '16 at 13:58cmdSearch_Click
to events ofcmdSearch
– Andrei Jun 1 '16 at 14:10