Cs
if (DDlLoanType.Text == "Home")
{
interestRte fmLoanRte = new interestRte();
List LoanRteList = new List();
LoanRteList = fmLoanRte.getCurrentLoanRte();

// Step 3: propogate LoanTerm as dropdown list text , propogate intRte as dropdown list value
DdlTerm.Items.Clear();
DdlTerm.Items.Insert(0, new ListItem("--Select--", "0"));
//AppendDataBoundItems property allows you to add items to the ListControl object before data binding occurs.
DdlTerm.AppendDataBoundItems = true;

DdlTerm.DataTextField = "LoanTerm";
DdlTerm.DataValueField = "intRte";
DdlTerm.DataSource = LoanRteList
.Select(o => new { LoanTerm = o.LoanTerm, intRte = o.intRte })
.Distinct();
DdlTerm.DataBind();
}
Class file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;

///

/// Summary description for interestRte
///

public class interestRte
{
//
// Step 1: construct interestRte attribute
//
public int LoanTerm { get; set; }
public float intRte { get; set; }

// Step 2 : construct getCurrtentLoanRte()
public List getCurrentLoanRte()
{
// Step 3 : declare list named as rteList to hold list of interestRte object

List rteList = new List();

// Step 4 :Retrieve connection string from web.config
string DBConnect = ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;

// Step 5 :Create SQLcommand to select LoanTerm and LoanRate from LoanRate table where the rate is current

StringBuilder sqlStr = new StringBuilder();
sqlStr.AppendLine("SELECT LoanTerm, LoanRate From TDLoanRate ");
sqlStr.AppendLine("where GETDATE() between LoanEffectFrom and LoanMaturity ");

// Step 6 :Instantiate SqlConnection instance and SqlCommand instance

SqlConnection myConn = new SqlConnection(DBConnect);
SqlCommand cmd = new SqlCommand(sqlStr.ToString(), myConn);

// Step 7 :Open connection then execute reader
myConn.Open();
SqlDataReader reader = cmd.ExecuteReader();

// Step 8 : if reader has no rows return, set the rteList to null
if (!reader.HasRows)
{
rteList = null;
}
else
// Step 9 : Read the reader to extract required table column LoanTerm and LoanRate
// Construct interestRte instance 

while (reader.Read())
{
interestRte myLoanrte = new interestRte();
myLoanrte.LoanTerm = Convert.ToInt32(reader["LoanTerm"]);
myLoanrte.intRte = Convert.ToSingle(reader["LoanRate"]);

// Step 10 Add interest rate instance to array list rteList 
rteList.Add(myLoanrte);
}
// Step 11: Close the reader and connection 
myConn.Close();
reader.Close();
return rteList;
}
}

``

Im trying to populate a dropdown list which is ddlterm on another dropdown list which is ddlloantype

As long as the items in an array or list have a ToString() method that returns the value of the item, you can populate a dropdownbox by assigning the collection to the datasource property.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.