【问题标题】:The best overloaded method match for 'ColConfigSubsystem.Database.GetChromeMakeByYear(int)' has some invalid arguments'ColConfigSubsystem.Database.GetChromeMakeByYear(int)' 的最佳重载方法匹配有一些无效参数
【发布时间】:2014-02-28 16:56:55
【问题描述】:

我在 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);

我已包含我的内联 cmets 和摘要以帮助确定此事件的范围。我不太确定错误的原因是什么。

【问题讨论】:

    标签: c#


    【解决方案1】:

    您需要将值拆箱为整数。

    DataTable table = cmake.GetChromeMakeByYear((int)ddlVehicleYear.SelectedItem);
    

    从下拉列表中检索项目时,它们以对象的形式检索,因为您可以在其中存储任何类型的数据。编译器不知道您在列表中指定的项目实际上是一个整数(而不是字符串),因此不知道您要调用什么函数。

    更多关于拆箱和装箱的信息:http://msdn.microsoft.com/en-us/library/yz2be5wk.aspx

    【讨论】:

    • 好的,我照你说的做了,但又抛出了另一个错误“无法将类型 'System.Web.UI.WebControls.ListItem' 转换为 'int'。我必须将其转换为 int 吗?
    • @MaximusPrime:试试SelectedValue 而不是SelectedItem
    • 嗯...我尝试使用 SelectedValue 并引发另一个错误“无法将类型 'string' 转换为 'int'
    • 在这种情况下,您需要转换它而不是拆箱或铸造它。 int.Parse(SelectedValue) 应该可以工作。这当然是如果字符串是一个有效的整数。作为旁注,通常最好使用int date; int.TryParse(SelectedValue, out date) 以防万一。
    • 它确实可以工作,但是现在它会引发无法将类型“System.Data.DataSet”隐式转换为“System.Data.DataTable”的错误?现在我彻底糊涂了
    【解决方案2】:

    试试这个:

    DataTable table = cmake.GetChromeMakeByYear((int) ddlVehicleYear.SelectedValue);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-14
      • 2018-07-14
      • 2013-01-07
      • 2013-06-09
      • 2013-12-30
      • 2014-05-17
      • 1970-01-01
      相关资源
      最近更新 更多