Solved

ASP.net TableAdapter - SELECT query with parameters

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
@Html.EditorFor displays mm/dd/yyyy rather than date 2 35
Align Buttons next to each other 2 25
ASP.Net Session State alternatives 3 60
Gridview alignment 1 10
Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

896 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

19 Experts available now in Live!

Get 1:1 Help Now