Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error using LIKE Operator 2

Status
Not open for further replies.

FurqanAhmed

Programmer
Oct 1, 2001
87
0
0
PK
Hi,

I'm working on a text box control on windows form. The text box helps the user in inputing hotel names. As the user inputs each letter of the hotel name,the text box provides the remaining name that matches the user provided input. For example, if the user input "HIL", the text box displays "HILTON".

Recently I've received couple of complain from users when they input this hotel name "4 * SELECT-RAD HOTEL". The specific error that is given is "Error in Like operator: the string pattern '4 * %' is invalid.". What I've figured out so far is whenever the '%' operation is used after '*', an error is generated. The C# code is

----------------------------------

DataView dv;
dv = new DataView(this.hotelTable, "Name LIKE '"+txtHotelName.Text+"%'", "Name", DataViewRowState.CurrentRows);

if(dv.Count > 0)
{
int txtCount = txtHotelName.Text.Length;
string hotelName = dv[0]["Name"].ToString();
this.lsbErrors.Items.Add(""+dv[0]["ID"].ToString());
this.acceptChanges = false;
txtHotelName.Text = hotelName;
txtHotelName.SelectionStart = txtCount;
txtHotelName.SelectionLength = hotelName.Length - txtCount;
this.acceptChanges = true;
}

------------------------------------------------------

this is the method body for the TextChanges Event.

Can anyone tell me how to solve this problem?

 
This is explained in the VS help - you can use either the * or % wildcards in a data expression, but not both in the same string.

To allow a LIKE pattern including the *, use the following:
Code:
dv = new DataView(this.hotelTable, "Name LIKE '"+txtHotelName.Text.Replace("*", "[*]")+"%'", "Name", DataViewRowState.CurrentRows);
 


Thanks a lot Shelton... your reply really helped.

Furqan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top