2

我是编程新手,目前在 C# asp.net 网站上工作,该网站从数据库 on_load 填充 Telerik RadComboBoxes。

我有一个包含 15 个以上 ComboBox 的表单,当我从这些 ComboBox 中选择值时,必须使用这些 ComboBox 选择来搜索数据库中的一个非常大的表。gridView 将显示返回的数据。

我在整个项目的其余部分都使用了相同格式的代码,它运行良好,但是当我从“位置”下拉框中选择一个项目来搜索我的数据库时,我得到了错误'Input string was not in a correct format',我不知道为什么

这是我的 Location.cs 类

区域属性

    [Key]
    public int LocationID { get; se; }
    [Column("Location")]
    public string LocationName { get; set; }
    private int? _ParentLocationID;
    [Column]
    public int? ParentLocationID
    {
        get
        {
            return _ParentLocationID;
        }
        set
        {
            if (value == 0)
            {
                _ParentLocationID = null;
            }
            else
            {
                _ParentLocationID = value;
            }
        }
    }

    [Column]
    public int SiteID { get; set; }
    [Column]
    public bool Active { get; set; }

区域法

    public static IEnumerable<Location> LoadActiveLocations(int siteID)
    {
        iThNkContext db = new iThNkContext();

        var LocationList = (from l in db.Locations
                                where(l.SiteID == siteID && l.Active == true)
                                orderby l.LocationID
                                select l).ToList();

        return LocationList;
    }

这是我在 .aspx 文件中使用的代码

RadTreeView trvLocation = (RadTreeView)cboLocation.Controls[2].FindControl("trvLocation");
         if (trvLocation.SelectedValue != "")
         {
            var locationID = Convert.ToInt32(trvLocation.SelectedValue);  //Error
            predicates.Add(p => p.LocationID == locationID);
        }

在 //Error 行是我收到“输入字符串格式不正确”错误的地方,请提供任何建议。我不明白为什么我会遇到这个问题

先感谢您

4

2 回答 2

1

看起来您的 LocationID 是一个可为空的值 - 您无法将空值转换为 Int32。您需要确保绑定到下拉列表的每个值成员不是空值而是有效整数。

于 2013-04-16T10:54:59.290 回答
1

如果您希望 locationID 是数字,请确保组合框中每个项目的值都可以转换为数字。否则,即使您使用 tryparse,您的页面也不会正确运行。

P/S:TryParse 只会避免错误,不会将 selectedvalue 分配给 locationID,因此会破坏您的逻辑。

于 2013-04-16T10:39:18.200 回答