Possible Help with ASP.net?

Associate
Joined
21 Jun 2011
Posts
16
I am creating a website that needs to be able to search a database that contains information on fish. I wish to filter the grid view on both the "scientific name" of the fish and also its "common name". For this i have created two text boxes one named "txtScientificName" and one called "txtCommonName" each of which has a corresponding button. The code below is what i have so far and searches just the "Scientific Name" based on the criteria entered into the text box "txtScientificName" when the user presses enter.

Code:
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" width="800px" 
AutoGenerateColumns="False" DataSourceID="FishOverview" 
HorizontalAlign="Center" BorderStyle="None" GridLines="Horizontal">
<Columns>
<asp:BoundField DataField="ScientificName" HeaderText="Scientific Name" 
SortExpression="ScientificName" />
<asp:BoundField DataField="CommonName" HeaderText="Common Name" 
SortExpression="CommonName" />
<asp:BoundField DataField="FishSpecies" HeaderText="Fish Species" 
SortExpression="FishSpecies" />
</Columns>
<RowStyle HorizontalAlign="Center" />
</asp:GridView>
<asp:SqlDataSource ID="FishOverview" runat="server" 
ConnectionString="<%$ ConnectionStrings:db_0905193_CO6009ConnectionString %>" 

SelectCommand="SELECT FishPhoto, ScientificName, CommonName, FishSpecies FROM tblFish WHERE (ScientificName LIKE '%' + @ScientificName + '%') ">
<SelectParameters>
<asp:ControlParameter ControlID="txtScientificName" Name="ScientificName" 
PropertyName="Text" Type="String" />
</SelectParameters>
</asp:SqlDataSource>


I wish to be able to do the same for the common name but have the process attached to a button and for the user to be able to search using either the Scientific Name or Common Name. Any help with this would be very much appreciated.

Thanks in advance

Ryan
 
You can either use

* Two button with two event handlers
* Some radio buttons
* Or some logic in the event handler which can tell if one of the text boxes is empty.

Edit: I read it wrong.

What you need to do is place the code which performs the search in a separate method. Then call this method from each buttons event handler.
 
Last edited:
Alright this is how I do it, but it's a bit of a change of pace from what you're currently doing:

1) Create a Stored Proc in SQL that accepts the parameters you want to parse in. In the Stored Proc build a dynamic SQL statement and then execute it with exec sp_executesql to help protect against SQL injection (this uses paramaterized SQL).

2) Add an .xsd Dataset object to your project in Visual Studio.

3) Add a table adapter and point it to your Stored Proc.

4) On your .aspx page add an ObjectDataSource and point it to your table adapter. For the parameters you want a couple of text box controls and you just point each parameter to each textbox.

5) On your GridView bind it to your datasource.

6) Add a submit button to your page.

When you hit submit the values will be passed from your textboxes down to your ObjectDataSource then into your table adapter and will be parsed as such.

Works for me anyway.
 
Back
Top Bottom