Click here to Skip to main content
15,878,970 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I've bound three drop down lists with same oledb data adapter. this oledbdataadapter fills three different datatables which I use eventually to fill up three dropdowns. But when I access three dropdown values in submit button, all three dropdowns give same selected item even when I've selected three different options in dropdowns. Can Anybody help?
here's my code:-

C#
//select command
cmdExcel.CommandText = "Select * from [" + SheetName + "]";
//oda - oledbdataadapter
oda.SelectCommand = cmdExcel;
oda.Fill(dt_Temp);
DataTable dt_costcode = new DataTable();
oda.Fill(dt_costcode);
DataTable dt_desc = new DataTable();
oda.Fill(dt_desc);
DataTable dt_unit = new DataTable();
oda.Fill(dt_unit);
connExcel.Close();
ViewState["TempImport"] = dt_Temp;

List<CostColumns> list_costCode = Columns(dt_costcode);
Bind_ddlCostCode(list_costCode);

List<CostColumns> list_desc = Columns(dt_desc);
Bind_ddlDescription(list_desc);

List<CostColumns> list_unit = Columns(dt_unit);
Bind_ddlUnitofMeasure(list_unit);

here code for binding Dropdowns:-
C#
<public void Bind_ddlCostCode(List<CostColumns> list_costCode)
        {
            foreach (CostColumns costcol in list_costCode.ToList())
            {
                if (costcol.ColumnType == "System.String")
                {
                    ddlCostCodeNumber.Items.Add(new ListItem(costcol.Columns, costcol.ColumnType));
                }
            }
            ddlCostCodeNumber.DataBind();
            ddlCostCodeNumber.Items.Insert(0, new ListItem("--Select CostCode--", "0"));
        }
        public void Bind_ddlDescription(List<CostColumns> list_desc)
        {

            foreach (CostColumns costcol in list_desc.ToList())
            {
                if (costcol.ColumnType == "System.String")
                {
                    ddlDescription.Items.Add(new ListItem(costcol.Columns, costcol.ColumnType));
                }
            }
            ddlDescription.DataBind();
            ddlDescription.Items.Insert(0, new ListItem("--Select Desc.--", "0"));
        }
    //(Same code for Bind_ddlUnitofMeasure(list_unit))
    public List Columns(DataTable dt)
        {
            List list=new List();
            CostColumns CC ;
            foreach (DataColumn col in dt.Columns)
            {
                CC = new CostColumns();
                CC.Columns=Convert.ToString(col.ColumnName);
                CC.ColumnType=Convert.ToString(col.DataType);
                list.Add(CC);
            }
            return list;
        }

And here's where It all goes wrong:-
C#
protected void Btn_ImportRecords_Click(object sender, EventArgs e)
        {
            DataTable dt_Temp= (DataTable)ViewState["TempImport"];
            DataTable dt_Import = new DataTable();
            Int32 i = costCodeNo();
            string ccn = ddlCostCodeNumber.SelectedItem.Text;//gives same selected option(CostCodeNumber)
            string desc = ddlDescription.SelectedItem.Text;//gives same selected option
            string unit = ddlUnitofMeasure.SelectedItem.Text;//gives same selected option
            dt_Import.Columns.Add(new DataColumn("CostCodeNo", typeof(Int32)));
            dt_Import.Columns.Add(new DataColumn(ddlCostCodeNumber.SelectedItem.Text, typeof(string)));
            dt_Import.Columns.Add(new DataColumn(ddlDescription.SelectedItem.Text, typeof(string)));//throws exception that dt_Import already has same column name
    }


What I have tried:

I've tried creating diffrent datatables for different dropdowns but so far no luck.
Posted
Updated 16-Aug-17 0:09am
v2
Comments
Karthik_Mahalingam 16-Aug-17 2:55am    
show the code for Columns function and select query
sam_matte 16-Aug-17 5:24am    
I've updated the code please take a look
Karthik_Mahalingam 16-Aug-17 6:18am    
how many sheets are there
sam_matte 16-Aug-17 7:02am    
there's only one sheet for now.
Karthik_Mahalingam 16-Aug-17 8:06am    
then the data will be same, since you are using same dataset for all the ddl

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900