Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi
"where" clause does not working.combobox not retrieve data.


Working

string Query ="SELECT txIT FROM It ";

Not working

string Query ="SELECT txIT FROM It WHERE cmboItem ='"+cmboItem.SelectedValue+"'";
Posted
Comments
Thanks7872 9-Sep-13 5:06am    
It would be better if you elaborate your question on what do you mean by "where" clause does not working.combobox not retrieve data.It also important to note here,care must be taken while binding dropdown on postback.

It is never a good idea to construct a SQL query by concatenating string values from user inputs.
Better use parameterized queries.

As to your problem: you should check first that
cmboItem.SelectedValue
returns the actual data you need ; maybe
cmboItem.SelectedText
would fit best.

But finally, when you have determined which one of these properties is the right one, you should use a parameterized query:
C#
using (SqlConnection con = new SqlConnection(yourConnectionString)) {
   string sql = "SELECT txIT FROM It WHERE cmboItem = @value;";
   SqlCommand cmd = new SqlCommand(sql, con);
   cmd.Parameters.AddWithValue("value", cmboItem.SelectedText);
   con.Open();
   // Then execute the query, get your results, etc.
}
 
Share this answer
 
Comments
V.Lorz 9-Sep-13 4:49am    
Note that cmboItem.SelectedText, if it refers to ComboBox's property SelectedText, doesn't always return the text of the selected item, only when not in dropdown list mode and if SelectionLength equals the item text length and SelectionStart equals zero.

The Text property does return the text for the selected item (when there is an item selected, of course).
phil.o 9-Sep-13 4:52am    
Debugging is the solution here to see what property needs to be used.
Thanks for your clarifications!
Check your database for the existance of the items you're willing to select and try the query sentence manually so you can ensure yourself that the query generation code works properly.

Combined with the parameterized query generation provided by phil.o[^] here where clause not working when combobox select data[^] in his previous solution, alter his query and replace:
cmd.Parameters.AddWithValue("value", cmboItem.SelectedText);

by (1):
cmd.Parameters.AddWithValue("value", cmboItem.Text);

or by (2):
cmd.Parameters.AddWithValue("value", cmboItem.SelectedItem.ToString());

See the comment I placed in his solution.
Depending on the databindings you made to populate the combobox items list you could even need to do it in some other ways.
 
Share this answer
 
try this:

C#
string Query ="SELECT txIT FROM It WHERE cmboItem ='" & cmboItem.SelectedText & "'";
 
Share this answer
 
Comments
indiancodinglove 9-Sep-13 4:38am    
Show an Error

Operator '&' cannot be applied to operands of type 'string' and 'string'
Uknownymous 9-Sep-13 4:48am    
oh .. im sorry i forgot you are using C#. just replace `&` to `+` .
V.Lorz 9-Sep-13 4:43am    
Visual Basic string concatenation operator (&)?????
Uknownymous 9-Sep-13 4:48am    
oh .. im sorry i forgot you are using C#. just replace `&` to `+` .

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900