views:

44

answers:

3

Using C# & Mysql

I have combobox & button in my webpage, if i select a value from the combobox, then i click the button the page is refreshing, combobox value is changing.

For Example

Combobox value: 1, 2, 3, 4 .....

If i selected 2 then i press the button, webpage is refreshing, combobox values is display from: 1, 2, 3, 4....

Combobox should display 2

Why combobox is displaying a selected value when i press the button.

Table Name: user

Name username

Raja 1
Ravi 2
Ram 3
kumar 4
...,

Button click event code

cmd1 = new OdbcCommand("Select name from users where username = '" + combobox1.Items[combobox1.SelectedIndex].Text + "' ", dbcon);
            dr1 = cmd1.ExecuteReader();
            while (dr1.Read())
            {
                textbox1.Text = dr1.GetString(0);

            }
            dr1.Close();

The Above code is working, but if i select the value 2 from the combobox, then i press the button textbox should display ravi, but textbox is displaying Raja, and also combobox also refreshing, combobox is not displaying 1 instead of 2.

How to solve this issue.

Need C# code Help

A: 

Understanding ASP.NET View State might be a useful read. Generally, you need to somehow store the entered values and restore then on page load.

Given the fact that the button is reloading the entire page, wouldn't it be more efficient to reload just a part of it instead?

Dennis Delimarsky
A: 

Just use combobox1.SelectedIndex or SelectedText

instead of the one you used. May be it resolves your issue

Dorababu
A: 

maybe you are populating the dropdownlist in the page load so be sure to populate the dropdownlist in the !Page.IsPostBack instead , i think that this is your problem

Baharanji