Reputation: 51
I am writing to seek help on implementing a multiple criteria search function in asp.net webform.
I am novice user of asp.net and implemented the following vb code using an sample C# code but I can not get it to display any output.
Any help would be very much appreciated. Thank you for your time.
aspx
<div class = "hideSkiplink">
<asp:Label ID="Label1" runat="server" Text="Name" ForeColor="White"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Label ID="Label2" runat="server" Text="Cusip/Isin" ForeColor="White"> </asp:Label>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Search" OnClick = "btnSearch_Click" />
</div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:bwic testConnectionString %>"
SelectCommand="SELECT [Deal_name], [Cusip], [Isin], [Original_size], [Price],_
[Price_recieved], [Deal_type], [Price_type], [Date] FROM [Price]"> </asp:SqlDataSource>
<div>
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
</div>
aspx.vb
Imports System.Data
Imports System.Web.Services
Imports System.Configuration
Imports System.Data.SqlClient
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
SqlDataSource1.SelectCommand = "select * from Price where Deal_name like '%" + TextBox1.Text + "%' or Cusip and Isin like '%" + TextBox2.Text + "%'"
SqlDataSource1.DataBind()
End Sub
End Class
Upvotes: 0
Views: 1697
Reputation: 22733
It looks like you want to be doing an or on both Cusip
and Isin
separately:
SqlDataSource1.SelectCommand = _
"SELECT * FROM Price " + _
"WHERE Deal_name like '%" + TextBox1.Text + _
"%' AND (Cusip like '%" + TextBox2.Text +
"%' OR Isin like '%" + TextBox2.Text + "%')"
This will select records where the Deal_name
matches TextBox1
and Cusip
or Isin
matches TextBox2
. If this is not what you are after, please clarify.
EDIT:
Your GridView
requires a DataSourceID
to be set on it, otherwise it doesn't know where it's data is coming from. Take a look at the example below taken from here, which doesn't require a button click to do the binding.
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$Connectionstrings:ERPConnectionString%>"
SelectCommand="SELECT * FROM AccountsTable">
</asp:SqlDataSource>
<asp:GridView ID="GridView1" DataSourceID="SqlDataSource1"
AutoGenerateColumns="true" runat="server">
</asp:GridView>
Upvotes: 1