locked
Load Dropdown List from Two Columns in Database RRS feed

  • Question

  • User564995064 posted

    I have loaded dropdown lists from a database table before, but not like this.  My table has  servicer, lender 1 and lender 2 fields.  I have a dropdown list of servicers.  When the user selects the servicer, I want to load the lender dropdown list with the lender 1 and lender 2 fields based on the servicer selected.  Here is what I have so far, however, I am getting an error stating lender is not a field in the datatable. 

                    string id = ddlServicer.SelectedValue;
                    cl_Class getLenders = new cl_Class();
    
                    DataTable lenders = getLenders.getLenders(id);
    
                    ddlLender.DataSource = lenders;
                    ddlLender.DataValueField = "id";
                    ddlLender.DataTextField = "lender";
                    ddlLender.DataBind();
    
                    ddlLender.Items.Insert(0, new ListItem("Select Lender"));
    

    I am not sure what to use as the datatextfield since there are two columns.  Any help is appreciated.

    Thanks!

    Friday, July 20, 2018 2:24 PM

All replies

  • User-369506445 posted

    Hi

    You have to create a new type and join your fields

    var datasource = from x in lenders.AsEnumerable()
                 select new {
                    
                     Id= x.Field<int>("id") ,
                     DisplayField = String.Format("{0} ({1})", p.Field<string>("lender 1"),p.Field<string>("lender 2"))
                 };
    
    ddlLender.DataSource = datasource;
    ddlLender.DataValueField = "Id";
    ddlLender.DataTextField = "DisplayField";
    ddlLender.DataBind();

    Now put your fields name instead of lender 1 and lender 2

    Friday, July 20, 2018 2:48 PM
  • Friday, July 20, 2018 2:52 PM
  • User-369506445 posted

    here I create a sample that shows how to implement it

    protected void Page_Load(object sender, EventArgs e)
            {
             // DataTable lenders = getLenders.getLenders(id);
                DataTable lenders = GetTable();
    
                var datasource = from x in lenders.AsEnumerable()
                    select new
                    {
                        Id=x.Field<int>("id"),
                        DisplayField=x.Field<string>("lender1") + " " + x.Field<string>("lender2")
                    };
                                  
    
                ddlLender.DataSource = datasource;
                ddlLender.DataValueField = "Id";
                ddlLender.DataTextField = "DisplayField";
                ddlLender.DataBind();
    
            }
            static DataTable GetTable()
            {
                // Here we create a DataTable with four columns.
                DataTable table = new DataTable();
                table.Columns.Add("id", typeof(int));
                table.Columns.Add("lender1", typeof(string));
                table.Columns.Add("lender2", typeof(string));
                
    
                // Here we add five DataRows.
                table.Rows.Add(1, "lender 1", "lender 2");
                table.Rows.Add(2, "lender 1", "lender 2");
                table.Rows.Add(3, "lender 1", "lender 1");
              
                return table;
            }

    in this <g class="gr_ gr_138 gr-alert gr_gramm gr_inline_cards gr_run_anim Punctuation only-ins replaceWithoutSep" id="138" data-gr-id="138">sample</g> I create a DataTable with 3 columns id, lender1, and lender2 (you can bind your data-table)

    result

    Saturday, July 21, 2018 4:14 AM