ceroceps
ceroceps

Reputation: 1

ASP.NET DropdownList populated from MYSQL always returns first value

I have strange issue going on: Whatever I'm choosing from my DropDownList populated from MySQL database it always returns the value of first item in it.

Here's HTML:

<asp:DropDownList CssClass="form-control" runat="server" ID="selectStudentList"/>

and C#:

String getStudentsQuery = "SELECT CONCAT(imie,' ',nazwisko) AS name, id FROM users WHERE klasa = " + Session["rok"] + " AND access = 0";
        cmd = new MySql.Data.MySqlClient.MySqlCommand(getStudentsQuery, conn);

        var students = new DataTable();
        students.Load(cmd.ExecuteReader());
        selectStudentList.DataSource = students;
        selectStudentList.DataTextField = "name";
        selectStudentList.DataValueField = "id";
        selectStudentList.DataBind();

I've checked if fields are populated corectly. When I'm choosing lets say second item from dropdown and sending it via form the value: selectStudentList.SelectedValue always returns id of first item, even if another is chosen

Any tips? :)

Upvotes: 0

Views: 54

Answers (1)

Michael Andrews
Michael Andrews

Reputation: 304

If you're loading that dropdownlist in the page load and aren't checking for a post back it would do that. I.e. since you're loading it on page load every time, it gets reloaded.

Try throwing the c# code in an if(!ispostback){//loadHere}

Upvotes: 1

Related Questions