2016-01-19 28 views
1
  1. 我有這個複選框,從一個名爲贈款的位列獲取數據。這是需要的鑄造:

enter image description here用複選框布爾鑄造給出錯誤「指定的演員表無效」

  • 這裏我添加布爾的鑄造來解決這個問題。
  • enter image description here

  • 但是它產生一個錯誤。爲什麼?我一直在試圖弄明白,不能明白爲什麼,因爲我有使用相同的代碼和正常工作等複選框。
  • enter image description here

    它說:

    An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 
    
    Exception Details: System.InvalidCastException: Specified cast is not valid. 
    
    +0

    嘗試鑄造''布爾值,因爲它可能是你正在處理一個空值。 – NikolaiDante

    +0

    也許是它的字符串?那麼你需要使用convert類。 Convert.ToBoolean –

    +1

    什麼是'讀者[ 「免費」]的值。的GetType()。Name'? –

    回答

    1

    試試這個:

    object value = reader["Complimentary"]; 
    
    if(value != null && value != DBNull.Value) 
    { 
        CbIsComplimentary.Checked = (bool)value; 
    } 
    else 
    { 
        //Optionally handle null value, e.g.: 
        //CbIsComplimentary.Checked = false; 
    } 
    
    +0

    這樣做。謝謝。 – RockOn

    +0

    很高興我能幫到你。 –

    1

    你不能使用拆箱拆箱字符串類型到bool類型,我懷疑一個是另一個或反之亦然子類。你必須使用預定義的功能。

    Convert.ToBoolean(reader["Complimentary"].ToString()); 
    

    Convert.ToBoolean Method (String)