i have 4 objects in my database eg zen ,maruthi and scorpio ..after binding the values to dropdown list i can only see scorpio repeated 3 times ..
instead of getting it as
zen
maruthi
scorpio
..i get scorpio
scorpio
scorpio..
Code
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 page
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();
}
Where is
cabcoming from ? Make sure you are adding a new item in theforeachloop.