Solved

ASP.net TableAdapter - SELECT query with parameters

Posted on 2011-02-13
2
1,039 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:Murray Brown
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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:Murray Brown
ID: 34884109
Thanks very much
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
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…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

615 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