我是编程新手,目前正在开发一个 C# asp.net 网站,该网站从 on_load 数据库填充 Telerik RadComboBoxes。
我有一个包含 15 个以上组合框的表单,当我从这些组合框中选择值时,必须使用这些组合框选择来搜索数据库中的一个非常大的表。 gridView 将显示返回的数据。
我在项目的其余部分中使用了相同格式的代码,并且它工作得很好,但是当我从“位置”DropDownBox 中选择一个项目来搜索我的数据库时,我收到错误'输入字符串是格式不正确'
,我不明白为什么
这是我的 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 行上,我收到“输入字符串格式不正确”错误,请提供任何建议。我不明白为什么我会遇到这个问题
提前谢谢
最佳答案
如果您希望 locationID 为数字,请确保组合框中每个项目的值都可以转换为数字。否则,即使您使用 tryparse,您的页面也无法正确运行。
P/S:TryParse 只会避免错误,不会将所选值分配给 locationID,因此会破坏您的逻辑。
关于c# - 输入字符串的格式不正确 C# ASP.NET,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16034718/