Prashanth KM
Prashanth KM

Reputation: 75

dropdownlist not behaving properly when datavaluefield and datatextfield are same

I have a dropdownlist for which I am loading data from excel. Excel has 2 columns Product and Email. Data in Product column is binding to DataTextField and Email column to DataValueField. The dropdown works fine when email is different for different product but when email has same value for different product then whatever I select, on postback the selected value changes to the first item of the same email value.

Below are the sample data in Excel to show the behavior of dropdown

Example 1. (Drop down works fine for this example)

 
Product               Email 
iPad                  [email protected]
iPhone 3G             [email protected]
iPhone4               [email protected]

Example 2. (In the example below, whatever I select (iPad or iPhone 3G or iPhone4) on post back the dropdown selected value will be iPad)

 
Product               Email 
iPad                  [email protected]
iPhone 3G             [email protected]
iPhone4               [email protected]

Example 3. (In the example below, when I select iPad the dropdown works fine but when I select iPhone 3G or iPhone4 on post back the dropdown selected value will be iPhone 3G. Basically, on selecting iPhone4 here, on post back it shows iPhone 3G)

 
Product               Email 
iPad                  [email protected]
iPhone 3G             [email protected]
iPhone4               [email protected]

Below is the function where I am loaidng the data from excel to dropdown

private void ExtractFromExcelInitial()
{

    // Put user code to initialize the page here
    // Create connection string variable. Modify the "Data Source"
    // parameter as appropriate for your environment.
    string ExcelFilePath = Server.MapPath("~/ProductExcel") + "\\ProductEmail.xls";
    String sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
    "Data Source=" + ExcelFilePath + ";" +
    "Extended Properties=Excel 8.0;";

    // Create connection object by using the preceding connection string.
    OleDbConnection objConn = new OleDbConnection(sConnectionString);

    // Open connection with the database.
    objConn.Open();

    // The code to follow uses a SQL SELECT command to display the data from the worksheet.

    // Create new OleDbCommand to return data from worksheet.
    OleDbCommand objCmdSelect = new OleDbCommand("SELECT * FROM [Sheet1$]", objConn);


    // Create new OleDbDataAdapter that is used to build a DataSet
    // based on the preceding SQL SELECT statement.
    OleDbDataAdapter objAdapter1 = new OleDbDataAdapter();

    // Pass the Select command to the adapter.
    objAdapter1.SelectCommand = objCmdSelect;

    // Create new DataSet to hold information from the worksheet.
    DataSet objDataset1 = new DataSet();

    // Fill the DataSet with the information from the worksheet.
    objAdapter1.Fill(objDataset1, "XLData");


    ddlProduct.DataTextField = "Product";
    ddlProduct.DataValueField = "Emailid";
    ddlProduct.DataSource = objDataset1.Tables[0];

    ddlProduct.DataBind();
    ddlProduct.Items.Insert(0, new ListItem("Select Product", "0"));

    // Bind data to DataGrid control.
    ////DataGrid1.DataSource = objDataset1.Tables[0].DefaultView;
    ////DataGrid1.DataBind();

    // Clean up objects.
    objConn.Close();

}

Please please help me on this as Iam stuck on this for past 3 days.

Upvotes: 3

Views: 3855

Answers (1)

Graham Clark
Graham Clark

Reputation: 12956

I think ASP.NET makes an assumption that the values in a drop-down list are going to be unique. Usually Values are used to store something like an ID, so you don't have to parse the more descriptive Text property.

When you do a postback, all that ASP.NET is getting from your page is the normal HTML form post data, plus some ControlState and ViewState (if it's enabled). The form post data will contain the name/id of your drop-down list, and the currently selected value. The ControlState/ViewState will potentially contain the full list of text/value pairs in your drop-down list, so that the control can be automatically re-populated on postback without you needing to worry.

I guess during a postback ASP.NET is just setting the SelectedValue property of the drop-down list; as you have non-unique values it's just defaulting to selecting the first one.

Basically, you need to make your drop-down list values unique. When doing the initial data-bind, you could opt for a composite value. Looking at your current implementation, binding to a DataSet, that might be a bit of a pain. If, instead of binding to a DataSet you were to bind to a list of objects, it might be easier. So maybe something like this:

internal class Product
{
   public int Id { get; set; }
   public string Email { get; set; }
   public string ProductName { get; set; }
   public string CompositeId
   {
      get
      {
         return String.Format("{0}|{1}", this.Id, this.Email);
      }
   }
}

// in the data-binding
List<Product> products = GetProductsFromDataSet(objDataset1);
ddlProduct.DataTextField = "ProductName";
ddlProduct.DataValueField = "CompositeId";
ddlProduct.DataSource = products;
ddlProduct.DataBind();

Or don't bother with a composite ID, just use a numerical ID for the Value, and look-up the associated email address when you need it at a later point.

Upvotes: 2

Related Questions