Hi everybody,

Could anyone help me in the select statement. I created small form to search in the database, it gaves me an error Missing Expression. The combox has the attributes of the table because I want restrict the search to only on specfic columns.

commandString = "Select * from tableadd where " + this.comboBox1.SelectedText + "like '%" + textBox1.Text + "%'" ;

Recommended Answers

All 6 Replies

If you have a WHERE clause you can't use the selected value from your comboBox you need to specify what field in the database table you want to check e.g.

commandString = "Select * from tableadd where customerName like '%" + textBox1.Text + "%'" ;

This will return all records in tableadd WHERE curstomerName field is contains the value of textBox1.

What is it you're trying to use the selected value of the comboBox for ?

Thanks a lot for your reply,

Actually the statement that you gave it to me I know about it. But what I want to do is I want to select one attribut, for example

If I have this table,
create table CUSTOMER(
cust_name varchar2(20),
cust_country varchar2(20),
cust_city varhcar2(20),
cust_type varchar2(20));

If put these 4 attributes in combobox I can put for example in the textbox= paris

So I know that PARIS is a city, I'll select from combobox CITY. but I use your statement and imagine that I want to search by CITY not by Customer Name it will be easy for me everytime I select different attribute depend on the search.

I hope you can understand what I want

commandString = "Select * from tableadd where " + this.comboBox1.SelectedText + "like '%" + textBox1.Text + "%'" ;

Your code should be correct. Try to add space before like statement.

commandString = "Select * from tableadd where " + this.comboBox1.SelectedText + " like '%" + textBox1.Text + "%'" ;
commented: Looks like the OP forgot to +1 you +9

Thankx JX Man you are great,

I wish one day I can be perfect in C# like you

Thankx JX Man you are great,

I wish one day I can be perfect in C# like you

lol! Looks like you have a fan

Try using this.comboBox1.SelectedItem.ToString() instead of the SelectedText value which gets cleared when focus leaves the ComboBox (ie. when user clicks a button Read here)

If your ComboBox style is DropDownList, then just use comboBox1.Text

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.