0
[...]
public DataSet ReturnPromoMagazinesDs()
{

    MySql.Data.MySqlClient.MySqlConnection mysqlConnection = new MySql.Data.MySqlClient.MySqlConnection(this.connectionString);

    MySql.Data.MySqlClient.MySqlCommand mysqlCommand = new MySql.Data.MySqlClient.MySqlCommand("SELECT id, magazine_name FROM `magazines`", mysqlConnection);

    MySql.Data.MySqlClient.MySqlDataAdapter mysqlAdaptor = new MySql.Data.MySqlClient.MySqlDataAdapter(mysqlCommand);

    DataSet ds = new DataSet();

    mysqlAdaptor.Fill(ds, "magazines");

    return ds;
}

[...]
protected void Page_Load(object sender, EventArgs e)
{
        DataSet ds = new DataSet();
        ds = coreObject.ReturnPromoMagazinesDs();

        DropDownList1.DataSource = ds;
        DropDownList1.DataTextField = ds.Tables["magazines"].Columns["magazine_name"].ColumnName;
        DropDownList1.DataValueField = ds.Tables["magazines"].Columns["id"].ColumnName;
        DropDownList1.DataBind();

}

[...]
<asp:dropdownlist id="DropDownList1" runat="server"></asp:dropdownlist>

The above code works ok, untill I'm fetching the DropDownList1.SelectedValue which is always 1 (the fist value from the table). Values in the table aren't the blame for this, and if I manually add items to the DropDownList, everything works fine. What can cause this?

2 Answers 2

2

Does it work properly when you wrap all of the code in the Page_Load method in the following:

if (!IsPostBack) { /* Code as in the original post */ }
Sign up to request clarification or add additional context in comments.

Comments

0

Not sure if something is getting lost in transalation but you could simplify the following lines, even if its doesnt fix the issue.

DropDownList1.DataTextField = "magazine_name";

DropDownList1.DataValueField = "id";

1 Comment

Both ways result in the same thing. Even if I use dropdown.datetextfiel = "myfield" and dropdown.datatextvaluefield = "id" before or after droptdownlist.datasource = ds;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.