tags:

views:

52

answers:

3

In my webpage I am using combobox & searchbutton.

When i select a value from combobox then i click the search button, the combobox first matching value is displaying

For Example

  • combobox values are - 001, 002, 003
  • When i select the value in the combobox -003
  • Then i click the search button
  • The page is refreshing and 001 values are displaying instead of 003

Code for Search Click button:

cmd2 = new OdbcCommand("Select * from table where id = '" + combobox1.Text + "' ", con);
ada2 = new OdbcDataAdapter(cmd2);
ds1 = new DataSet();
ada2.Fill(ds1);
gridview1.DataSource = ds1;
gridview1.DataBind();

How can I solve this issue?

+1  A: 

It could be just a typo but it looks like you are selecting the combobox text and not the selectedvalue of the combo box. Is that correct?

spinon
Also another thought might be that you don't have viewstate enabled so as to maintain state for your items. So if you aren't resetting the selected value of the combobox to the selected value that could be why it is resetting.
spinon
+4  A: 

Are you binding your gridview or combobox in your Page_Load method? I have a hunch this may be the issue. Make sure it looks like this:

void Page_Load(Object obj, EventArgs e)
{
  if (!IsPostBack){
     //do your stuff
  }
}
rlb.usa
Sounds very viable, happens all too often that people forget the IsPostBack-check and the behavior as mentioned is precisely what happens then.
Abel
A: 

Have you tried with combobox1.SelectedItem.ToString() ?

Santiago