データベースに4つのオブジェクトがあります。たとえば、zen、maruthi、scorpioです。値をドロップダウンリストにバインドした後、scorpioが3回繰り返されているのを確認できます。
として取得する代わりに
zen
maruthi
scorpio
..私は蠍座を取得します
scorpio
scorpio..
コード
List<Cab> CabTypeList = new List<Cab>();
using (DataTable table = SqlDBConnector.ExecuteSelectCommand("GetCabType", CommandType.StoredProcedure))
{
//check if any record exist or not
if (table.Rows.Count > 0)
{
//Lets go ahead and create the list of cab
foreach (DataRow row in table.Rows)
{
cab.CabType = row["CabType"].ToString();
cab.CabId = Convert.ToInt32(row["Cab_Id"]);
CabTypeList.Add(cab);
}
}
}
ASPXページ
if (!IsPostBack)
{
CabDbAccess cabdbaccess = new CabDbAccess();
DropDownList1.DataSource = cabdbaccess.GetCabType();
DropDownList1.DataTextField = "CabType"; // the items to be displayed in the list items
DropDownList1.DataValueField = "CabId"; // the id of the items displayed
DropDownList1.DataBind();
}