My scenario is a Gridview which is bound to a stored procedure using asp:SQLDataSource. This was quickly done using the wizard where i selected the SP and all data was dispalyed. The SP was created in such a way that it contains 4 parameters. When such parameters are null, they will return all values in the query.
I have created 3 textboxes and a dropdown list to populate these parameters, binding them to a button in an attempt to create a search functionality. I have tried many ways and methods, especially by research on this site but none seem to work. I cant quite find my issue so I appreciate any help on the matter. Below is my code.
<asp:SqlDataSource ID="sqlSourceID" runat="server" ConnectionString="<%$ ConnectionStrings:DBCONNSTRING%>"
SelectCommand="storedProcedureName" SelectCommandType="StoredProcedure">
</asp:SqlDataSource>
The above is the SQL stored procedure bind.
I have attemtped to bind the controls as parameters for the SQL server. Prior to this code, the page used to load all records fine without search parameters as the SP was assuming a null value. Now nothing loads.
<SelectParameters>
<asp:ControlParameter ConvertEmptyStringToNull="true" ControlID="textBox1" PropertyName="Text" Name="1" Type="DateTime" />
<asp:ControlParameter ConvertEmptyStringToNull="true" ControlID="textBox2" PropertyName="Text" Name="2" Type="DateTime" />
<asp:ControlParameter ConvertEmptyStringToNull="true" ControlID="textBox3" PropertyName="Text" Name="3" Type="String" />
<asp:ControlParameter ConvertEmptyStringToNull="true" ControlID="ddl1" PropertyName="SelectedText" Name="4" Type="String" />
</SelectParameters>
Where the Control ID represents the textbox ID
Below is my code behind.
sqlSourceID.SelectParameters["textbox1"].DefaultValue = this.textbox.Text;
sqlSourceID.SelectParameters["textbox2"].DefaultValue = this.textbox1.Text;
sqlSourceID.SelectParameters["textbox3"].DefaultValue = this.textbox2.Text;
sqlSourceID.SelectParameters["ddl1"].DefaultValue = Convert.ToString(this.ddl.SelectedText);
Well that is my problem. I need to find a way to pass parameters from the textboxes when the Search Button is clicked and assume null on page load or if nothing is written.
Thanks to all those who contribute in advance.
SelectParameterssection, the various control parameters are named "1", "2", "3", etc., but in your code-behind you're using the control names "textbox1", "textbox2", etc., as indexers to the parameters ofsqlSourceID. Is that correct?