blah blah blah is here! blah blah » Close

up1down
link

Hi

I'm doing a winform application and uses a gridview, a textbox and a button in VS2005. I'm trying to make a query that gives me all lastnames starting with the letter I enter in my textbox. It works for me to enter a complete lastname in the textbox but not just the beginning letters... I have done this in the query builder, please look at my code below.

This is the code in my query builder:

SELECT        ID, FirstName, LastName, City
FROM Customer
WHERE (LastName = @LastName)


This is my code in the application:
private void searchButton_Click(object sender, EventArgs e)
{
customerTableAdapter.FillByLastName(contactDataSet.Customer, searchTextBox.Text);
}


Does anyone know what I need to do to get it work as I described above?
Thank you!

last answered one year ago

2 answers

up1down
link

SELECT        ID, FirstName, LastName, City
FROM Customer
WHERE (LastName LIKE @LastName)


then

private void searchButton_Click(object sender, EventArgs e)
{
// make sure to add the wild card character to the end of your query.
customerTableAdapter.FillByLastName(contactDataSet.Customer, searchTextBox.Text + "%" );
}


and for future reference, you can use google to show you all about this type of thing: http://www.google.com/search?q=sql+server+like+syntax

Thank you very much, I tried to google but without success.

MadHatter
2309

np, but the syntax reference is the very first link that shows up on google.

up0down
link

It all depends on what features you want to give to the user.

If you tell the user they can enter AB% to collect Abbott, Abernathy, Abcott
and %AB% to collect those as well as Pablo
or %AB to get Crab, then you can do this without 'fixing' by adding the wildcard '%'.

Otherwise, you need to give the user capability to select 'Starts With', 'Contains', 'Ends With' and apply the required '%'s.

Personally, I let the user enter the asterisk '*' and translate it before presenting that to the filter, because users are dumb.

BTW. Beware cases where the storage is not variable-length. Finding items ending with AB can be interesting if you have trailing spaces in a fixed-length string field.

And then, you can get into all sorts of issues with filters requiring case sensitivity.

Feedback