2013-04-16 35 views
2

我是新來的編程和目前工作的一個C#asp.net網站從數據庫ON_LOAD填充Telerik的RadComboBoxes。輸入字符串的不正確的格式C#ASP.NET

我有了15+組合框,當我從這些組合框選擇值的那些組合框選擇,必須使用在我的數據庫,通過一個真正的大表進行搜索的形式。一個gridView將顯示返回的數據。

我已經使用相同的代碼格式在我的項目的其餘部分,它完美的作品,但是當我從我的「位置」 DropDownBox選擇項目通過我的數據庫來搜索我的錯誤'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); 
     } 

在//錯誤行的代碼是在哪裏我得到了「輸入字符串的不正確的格式」的錯誤,有什麼建議請。我無法理解爲什麼我有這個問題

預先感謝您

+0

該錯誤表示您嘗試從中解析整數的字符串實際上並不包含有效整數。使用Int.TryParse – Satpal

+3

放置一個斷點,看看你在'trvLocation.SelectedValue'中得到了什麼' – Habib

+1

你看過'trvLocation.SelectedValue'返回什麼?什麼類型和/或價值? –

回答

1

如果預計locationID是一個數,確保在組合框中每個項目的值可以轉換爲數字。否則,即使您使用tryparse,您的頁面也無法正常運行。

P/S:的TryParse只會避免錯誤,並且不會對的SelectedValue分配給locationID,因此,打破你的邏輯。

+0

+1提到'TryParse'不會真正解決問題。 – DGibbs

1

它看起來像你的LocationID是一個空值 - 你不能爲空值轉換爲Int32。您需要確保綁定到下拉列表的每個值成員不是空值,而是有效的整數。