Using 'Like' in DataTable select query
-
I have a datatable which I wish to search for 'Like' matches. So searching for "p" would bring up the entries "p1", "Pea green", "p3" etc. I have code DataRow[] foundRows; foundRows = debtorDataSet.Tables["AutoText"].Select("Code LIKE " + lastWord + "%" ); if (foundRows.Length == 1) { .... } else { .... } but this is not working. Any pointers much appreciated.
-
I have a datatable which I wish to search for 'Like' matches. So searching for "p" would bring up the entries "p1", "Pea green", "p3" etc. I have code DataRow[] foundRows; foundRows = debtorDataSet.Tables["AutoText"].Select("Code LIKE " + lastWord + "%" ); if (foundRows.Length == 1) { .... } else { .... } but this is not working. Any pointers much appreciated.
as it is a string literal you are using, your statement seems to be missing some quotes, try this:
...Select("Code LIKE '" + lastWord + "%'" );
:)
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
-
as it is a string literal you are using, your statement seems to be missing some quotes, try this:
...Select("Code LIKE '" + lastWord + "%'" );
:)
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
Many thanks.
-
I have a datatable which I wish to search for 'Like' matches. So searching for "p" would bring up the entries "p1", "Pea green", "p3" etc. I have code DataRow[] foundRows; foundRows = debtorDataSet.Tables["AutoText"].Select("Code LIKE " + lastWord + "%" ); if (foundRows.Length == 1) { .... } else { .... } but this is not working. Any pointers much appreciated.
Luc's already given you the information you need, but I'd like to suggest that you should also look at using
string.Format
rather than explicit string concatenation - sometimes it just makes it easier to read. So, you'd end up with something likestring.Format("code like '{0}%'", lastWord);
"WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
-
as it is a string literal you are using, your statement seems to be missing some quotes, try this:
...Select("Code LIKE '" + lastWord + "%'" );
:)
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
-
as it is a string literal you are using, your statement seems to be missing some quotes, try this:
...Select("Code LIKE '" + lastWord + "%'" );
:)
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
If column name is (FIRST NAME) If I use as Select("FIRST NAME LIKE '" + lastWord + "%'" ); It shows error
-
If column name is (FIRST NAME) If I use as Select("FIRST NAME LIKE '" + lastWord + "%'" ); It shows error
It is a bad idea to post a question to a more than four years old thread. Most users here will not even see it. If you got an error message, add the full message text to your question. That helps others to help you. However, your column name contains a space. Then you have to enclose it in brackets:
Select("[FIRST NAME] LIKE '" + lastWord + "%'" );