0

So in an application I'm making, I have a selectCommand that is supposed to read the text within a textbox on a button click, and filters search results based off of the value.

Select command is.

SelectCommand="SELECT (bunch of columns) WHERE members.LastName LIKE '%@Ln%';

The value of @Ln is retrieved from code behind here:

public void searchButtonClick(object sender, EventArgs e)
    {
        SqlDataSource1.SelectParameters.Clear();
        SqlDataSource1.SelectParameters.Add("Ln", inputLastName.Text);        
    }

My goal is to select all records with last names containing whatever was entered into the last name text box. For example, I want "Mar", "Martinez", or a blank text box to all select somebody with the last name Martinez. A blank text box should select all last names.

But I believe my problem lies in the syntax of the SelectCommand. I've tested it without the single quotation marks, and many other variances of it to no avail. If I leave just "WHERE members.LastName LIKE @Ln", it selects records where the last name is the exact value of inputLastName.Text, but as I mentioned, that's not what I'm looking for. :(

I'm new to coding so please try to explain as clearly as possible, sorry if my problem was not articulated in the best way. Thank you in advance!

3
  • 1
    Can you show the rest of the code as well? Commented Sep 8, 2014 at 12:41
  • I agree, we need your full code copy and pasted because this should work as written. Commented Sep 8, 2014 at 12:42
  • @MeanGreen Do you mean of the SelectCommand? Commented Sep 8, 2014 at 12:42

1 Answer 1

1

Dont forget the "@" in SqlDataSource1.SelectParameters.Add("@Ln",... and change your SelectCommand to:

SelectCommand="SELECT (bunch of column) WHERE members.LastName LIKE @Ln";

SqlDataSource1.SelectParameters.Add("@Ln", '%' +  inputLastName.Text + '%');

so it should work.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, works like a charm! :) One more question though. You mentioned I should add the '@' when adding parameters. The new code is working fine for me without the "@", but with it, I'm getting an error that says I "must declare scalar variables". Do you know why that could be?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.