我在C#中有一个事件,它通过上面提到的类文件中包含的oracle存储过程,使用来自不同数据库的相关值来填充类文件(database.cs)中的下拉列表。这是我目前使用的代码:
///<summary>
///Populates the model dropdownlist with available chrome data associated with
///the year selected
/// </summary>
private void PopulateChromeModel()
{
//define a counter
int itemCounter = 1;
//create a database object
Database cmake = new Database();
//call GetChromeMakeByYear to retrieve the available models according to
//the year
DataTable table = cmake.GetChromeMakeByYear(ddlVehicleYear.SelectedItem);
Trace.Write("populating models");
//create a flag showing whether an item should be selected. Preset it to false
bool selected = false;
//we may need to select, then later deselect, an item based on the model.
int selectedItemByModel = 0;
//preset the current selected code to ""
string currentSelectedCode = "";
//define a flag to indicate whether we've already selected an item. Preset it to false.
bool hasSelected = false;
//create a list item for the 0 position
ListItem firstItem = new ListItem("-- SELECT --", "");
//first see if there is a currently selected item. If so, set the current selected model.
//this is done because we have to clear all of the selected items before adding the new list.
//but we want to be able to select the model that is already selected.
if (ddlVehicleModel.SelectedIndex > 0)
{
currentSelectedCode = ddlVehicleModel.SelectedValue;
}
//clear any items from the list
ddlVehicleModel.Items.Clear();
//add the first item
ddlVehicleModel.Items.Add(firstItem);
//loop through the table and add items for each row.
foreach (DataRow row in table.Rows)
{
//get this record's chrome id
string id = row["CHROME_ID"].ToString();
//get this record's make
string make = row["CHROME_MAKE"].ToString();
//set a flag specifying whether the item should be selected based on year
bool selectBasedOnYear = false;
if (currentSelectedCode == id)
{
selected = true;
}
else
{
selected = false;
selectBasedOnYear = false;
}
Trace.Write(string.Format("{0}: {1}: {2}: {3}", id, make, selected, selectBasedOnYear));
//create a new list item for this model
ListItem newItem = new ListItem(id, make);
//if we have thrown either selected flag and we have not already selected an item,
//mark this option as selected
if ((selected || selectBasedOnYear) && !hasSelected)
{
Trace.Write("-- Either selected or selectedBasedOnYear was true, and hasSelected was false");
//first make sure the first item is deselected
firstItem.Selected = false;
Trace.Write("-- deselected the first item.");
//next deselect and items that were selected due to year. This allows the user selected
//region to override the default model for year.
if (selectBasedOnYear != null)
{
Trace.Write(string.Format(" -- deselecting item {0}, which was selected due to year", selectBasedOnYear));
ddlVehicleMake.Items[selectedItemByModel].Selected = false;
}
//select this item
newItem.Selected = true;
Trace.Write(" -- selected the current item");
//only throw the hasSelected flag if this was a user-selected region
if (selected)
{
hasSelected = true;
Trace.Write(" -- set hasSelected to true");
}
}
//add the model to the list
ddlVehicleMake.Items.Add(newItem);
itemCounter++;
}
//if there's no items selected and we have more than just the default item,
//default to the first item
if (ddlVehicleMake.Items.Count > 0 && !hasSelected && selectedItemByModel == 0)
{
ddlVehicleMake.SelectedIndex = 1;
}
}
错误发生在以下行:
//call GetChromeMakeByYear to retrieve the available models according to
//the year
DataTable table = cmake.GetChromeMakeByYear(ddlVehicleYear.SelectedItem);
我已将我的内联评论和摘要包含在内,以帮助您了解此活动的范围。我不太清楚错误的原因是什么。
答案 0 :(得分:2)
您需要将值取消装箱为整数。
DataTable table = cmake.GetChromeMakeByYear((int)ddlVehicleYear.SelectedItem);
当从下拉列表中检索项目时,它们将以对象的形式检索,因为您可以在其中存储任何类型的数据。编译器不知道你在列表中指定的项目实际上是一个整数(而不是一个字符串),所以不知道你试图调用什么函数。
有关拆箱和装箱的更多信息,请访问:http://msdn.microsoft.com/en-us/library/yz2be5wk.aspx
答案 1 :(得分:0)
试试这个:
DataTable table = cmake.GetChromeMakeByYear((int) ddlVehicleYear.SelectedValue);