Solved

ASP.net TableAdapter - SELECT query with parameters

Posted on 2011-02-13
2
1,030 Views
Last Modified: 2012-05-11
Hi

I am new to ASP.net. I have a simple SQL database in my project and have a DataSet where I
am using the TableAdapter wizard to select data from my database.
I have two text boxes on my ASP.net site. How do I do a SELECT statement that
selects data that is similar to what is in my two text boxes?
I want to build a statement similar to the following:
SELECT * FROM PRODUCTS WHERE Detail LIKE '%Apple%' OR Detail LIKE '%Orange%'
0
Comment
Question by:murbro
2 Comments
 
LVL 10

Accepted Solution

by:
P1ST0LPETE earned 500 total points
ID: 34884056
Lets say you had the following two textboxes:

<asp:Textbox ID="tbDetail1" runat="server" />
<asp:Textbox ID="tbDetail2" runat="server" />

On postback you would build your sql select statement like this:

string sql = "SELECT * FROM [PRODUCTS] WHERE [Detail] LIKE '%" + tbDetail1.Text + "%' OR [Detail] LIKE '%" + tbDetail2.Text + "%'";

Actually the above string is what you would pass to the database to load a DataSet/DataTable.
If however, you already had all your data loaded into a DataSet because you happened to already pass a "SELECT * FROM [Products]",
Then you could perform a 'select' on the DataSet/DataTable itself like this:

DataSet dataset = new DataSet();  //you would have this loaded with data already
DataTable table = dataset.Tables["PRODUCTS"];
DataRow[] filteredrows = table.Select("[Detail] LIKE '%" + tbDetail1.Text + "%' OR [Detail] LIKE '%" + tbDetail2.Text + "%'");

That is how you'd do it in C#.  VB would be pretty much the same thing with just minor tweaks to the syntax.

Have a look here: http://www.developerfusion.com/article/4703/filtering-and-sorting-in-adonet/2/
0
 

Author Closing Comment

by:murbro
ID: 34884109
Thanks very much
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question