EsiX
EsiX

Reputation: 65

ASP.NET DropDownList Issue

[...]
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?

Upvotes: 0

Views: 455

Answers (2)

Mark Redman
Mark Redman

Reputation: 24535

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";

Upvotes: 0

IanT8
IanT8

Reputation: 2217

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 */ }

Upvotes: 2

Related Questions