Solved

ASP.net TableAdapter - SELECT query with parameters

Posted on 2011-02-13
2
1,038 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
[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:murbro
ID: 34884109
Thanks very much
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
c#, datatable, aspx 4 52
How to set focus on a dynamic control 18 52
What are MicroServices? 4 68
ASP.NET, Globalization, language indexes, translation, conversion. 2 71
This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…
Suggested Courses

751 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