I could use some help with a SQL SELECT LIKE command. I have two TextBox'es that I am using as input for two different searches through a SQL database with the result placed in a GridView.

It is important to note that the search can be performed from either TextBox, so I wanted to use the LIKE command separated by an OR expression. But it does not seem to work. I always need to place something in BOTH TextBox'es to get a result.

I am a novice at VB programming and SQL databases. So any assistance is warmly accepted.

Thanks!

William Aldrich-Thorpe

Hi Willem,

Can Textbox1 and Textbox2 contain a value at the same time , or only one of them at the same time?

If textbox1.text<>"" and textbox2<>"" then
strQuery = "SELECT * FROM Table WHERE
Field LIKE %" & Textbox1.text & "% OR Field
LIKE %" & Textbox2.text & "%"
elseif Textbox1.text<>"" end Textbox2.text="" then
strQuery = "SELECT * FROM Table WHERE
Field LIKE %" & Textbox1.text & "%
elseif Textbox1.text="" end Textbox2.text<>"" then
strQuery = "SELECT * FROM Table WHERE
Field LIKE %" & Textbox2.text & "%
endif

This will do the job.

If you don't need all fields from the table, replace the * for the fieldnames separated bij a ,
This will speedup yor query when using larger databases.

Grtz,
Nokkieja

Hi Willem,

Can Textbox1 and Textbox2 contain a value at the same time , or only one of them at the same time?

If textbox1.text<>"" and textbox2<>"" then
strQuery = "SELECT * FROM Table WHERE
Field LIKE %" & Textbox1.text & "% OR Field
LIKE %" & Textbox2.text & "%"
elseif Textbox1.text<>"" end Textbox2.text="" then
strQuery = "SELECT * FROM Table WHERE
Field LIKE %" & Textbox1.text & "%
elseif Textbox1.text="" end Textbox2.text<>"" then
strQuery = "SELECT * FROM Table WHERE
Field LIKE %" & Textbox2.text & "%
endif

This will do the job.

If you don't need all fields from the table, replace the * for the fieldnames separated bij a ,
This will speedup yor query when using larger databases.

Grtz,
Nokkieja

Thank you for the quick reply. I can see how this will work. However, I am building a web application using ASP and VB. I am unsure how to make this fit within those constraints. As I I stated previously I am a mere novice in all of this. Here is a sample of what I see:

My Default.ASPX code sample (just a couple of lines):

<asp:GridView ID="GridView24" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource24" Font-Names="Arial" Font-Size="10pt" Font-Strikeout="False" DataKeyNames="OneViewID">
<Columns>
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField DataField="OneViewName" HeaderText="OneViewName" SortExpression="OneViewName" />
<asp:BoundField DataField="OneViewID" HeaderText="OneViewID" SortExpression="OneViewID" />
<asp:BoundField DataField="AccountMD" HeaderText="AccountMD" SortExpression="AccountMD" />
<asp:BoundField DataField="ProposalDueDate" HeaderText="ProposalDueDate" SortExpression="ProposalDueDate" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource24" runat="server" ConnectionString="<%$ ConnectionStrings:DealReview1ConnectionString %>"
SelectCommand="SELECT OneViewName, OneViewID, AccountMD, ProposalDueDate FROM GeneralData WHERE (OneViewName LIKE '%' + @OneViewName + '%')">
<SelectParameters>
<asp:ControlParameter ControlID="ovNameInput" Name="OneViewName" PropertyName="Text" Type="String" />
</SelectParameters>
</asp:SqlDataSource>

Code Sample From Default.aspx.vb:

Protected Sub GridView24_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GridView24.SelectedIndexChanged
If reportPanel.Visible = False Then
reportPanel.Visible = True
End If
End Sub

Your assistance is greatly appreciated.

William Aldrich-Thorpe

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.