Solved

ASP.net TableAdapter - SELECT query with parameters

Posted on 2011-02-13
2
1,023 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

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…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

758 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

Need Help in Real-Time?

Connect with top rated Experts

27 Experts available now in Live!

Get 1:1 Help Now