Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4 > BOOK: Beginning ASP.NET 4 : in C# and VB
| Search | Today's Posts | Mark Forums Read
BOOK: Beginning ASP.NET 4 : in C# and VB
This is the forum to discuss the Wrox book Beginning ASP.NET 4: in C# and VB by Imar Spaanjaars; ISBN: 9780470502211
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 4 : in C# and VB section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old August 25th, 2010, 07:31 AM
Authorized User
Points: 150, Level: 2
Points: 150, Level: 2 Points: 150, Level: 2 Points: 150, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2009
Location: Perth, Western Australia
Posts: 33
Thanks: 7
Thanked 1 Time in 1 Post
Send a message via MSN to cuddling101 Send a message via Yahoo to cuddling101
Question WHERE {field name passed as a parameter} 'contains' {value passed as a parameter}

My standard Grid View display, or LINQ display, of my primary database is just how I want it, but I want to be able to make it display different sets of records based on parameters passed to it from a 'calling' page.

Code:
SELECT [ID], [Title], [Series_Available], [Available], [Authors_UK], [Illustrators_UK], [Authors_USA], [Illustrators_USA], [Book_Numbers], [ISBNs], [Publication_Dates], [Front_Cover_JPEGs] FROM [Titles]
what I want to be able to then do is have the system generate a varying WHERE clause based on the two parameters of field to test, and value to test, so that for example on one call it might generate

WHERE Available 'contains' "A"

BUT on another occasion it might be called on to generate

WHERE Authors_UK 'contains' "Notkin"


Data commentary

The contents of Available is an up to 16 character string with one, some or all of the letters A to P, each letter occuring, if at all, only once.

The contents of Authors_UK is a string such as "Gulkin, S; Notkin, J. J."


Bottom line questions -

1. Is there a 'contains' operator, or a function that can be called to do that? If it is a function, how would I incorporate that call to the building of the WHERE clause?

2. Can I pass a field name as a parameter and, if so, how do I do it?


Caveat

If these questions are beyond the scope of the Beginning book and more the scope of the Professional book, I am very happy to buy that book, and look to find the answers there, or on the forum for that book. Please advise accordingly.

Best regards

Philip
  #2 (permalink)  
Old August 25th, 2010, 08:49 AM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

It depends on where your data comes from. When using LINQ you can take a look at the Dynamic LINQ library: http://weblogs.asp.net/scottgu/archi...y-library.aspx

For a SqlDataSource you could habdle the Selecting event and change the CommandText:

Code:
protected void SqlDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{
  e.Command.CommandText+= " WHERE Description LIKE '%whatever%'";
}
Two things worth noting:

1. I am using LIKE with % symbols which acts like the contains you talked

2. Make sure you don't open your code for SQL injection. This dynamic nature of SQL statements make it easy to inject something like:

; DROP DATABASE MASTER; --

instead of the literal whatever, which would select all records from your table and then delete your master database. No fun.... ;-)

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
  #3 (permalink)  
Old August 25th, 2010, 08:52 AM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

BTW: these kind of questions are not necessarily beyond the scope of the book, but they are a bit off-topic. You're better off posting in a more general ASP.NET 4 forum like this one: http://p2p.wrox.com/asp-net-4-general-discussion-561/

Posting there will likely draw more attention to your questions, and makes this forum a bit more focused on the book.

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to pass a control parameter and process a return parameter in sqldatasource jayshankar ASP.NET 2.0 Basics 1 January 1st, 2009 10:18 AM
How to passed an xsl parameter to a javascript fun jadeite100 XSLT 1 June 22nd, 2007 02:51 PM
Value in Querystring not being passed u_heet Classic ASP Professional 2 September 16th, 2004 07:15 AM
wrong passed parameter apek PHP How-To 1 January 11th, 2004 01:08 PM
All My Checkboxes are passed as "on", whether or n WebDevel Javascript How-To 1 December 9th, 2003 10:40 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.