Sunday, February 19, 2012

ASP: SqlDataSource - Select Command

I am using <asp:SqlDataSourceIDand for the Select Command, the following, where the WHERE clause ... for an exact match (=) works correctly:

SelectCommand="SELECT [PatientID], [MedRecord] , [Accession], [FirstName], [LastName], [Address1] FROM [ClinicalPatient] WHERE (LastName = @.LastName) ORDER BY [LastName]DESC">

I would like to do a "LIKE" search where the LastName Parameter is matched using "LIKE". In this situation how would the syntax be written...

I tried:

LastName LIKE '%" & LastName & "%'"

But I get an error????Any suggestions, please...

Thanks !!

Hey,

LastName like @.LastName

And you need to embed a % in the passed in parameter value.

|||

Hi and Thanks for getting back to me... however Im not sure that I understand your response... would really appreciate an 'example' or instruction on how to do what you indicate...

Thanks Again !!

|||

How are you assigning the value to the drop down? Because whatever value it uses is sent directly to the data source control. You need to modify the value sent. There are a couple of ways to do this. You could modify the textbox value and manually call the select() for the data source, but that would leave the % value in the textbox. Better would be to not use the ControlParameter parameter object, but to use Parameter. Tap into the Selecting event, and do this:

this.SqlDS.SelectParameters["LastName"].DefaultValue = "%" + this.txtLastName.Text + "%";

|||I took your lead...and I coded the 'LastName' value from the form search page, like this...

LastNamePresent ="%" & LastName.Text &"%"
and on the result page extracted the value from the QueryString and used it like this...

SelectCommand="SELECT [PatientID], [MedRecord] , [Accession], [FirstName], [LastName], [Address1] FROM [ClinicalPatient] WHERE [LastName] LIKE @.LastName ORDER BY [FirstName]ASC"
And OILA!!!!!
IT WOOOOOOOOOOOOOOOOOORKED !!!!

THANKS !!!!Big Smile


No comments:

Post a Comment