locked
Object cannot be cast from DBNull to other types RRS feed

  • Question

  • User2033107836 posted

    Hello

    This is my code

        <asp:TemplateField HeaderText="Closing Value">                   
                            <ItemTemplate>
                                <asp:Label ID="L4" runat="server" Text='<%# Convert.ToDouble(Eval("m_Closing")) * Convert.ToDouble(Eval("ChemicalRatePerUnit"))%>'></asp:Label>
                            </ItemTemplate>   
                            <FooterTemplate>
                                <asp:Label ID="GTV4" runat="server"></asp:Label>
                            </FooterTemplate> 
                        </asp:TemplateField>

    How to avoid this below from the above code

    Object cannot be cast from DBNull to other types.

    Description: 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: Object cannot be cast from DBNull to other types.

    Saturday, March 25, 2017 4:06 AM

Answers

  • User-1509636757 posted

    Object cannot be cast from DBNull to other types.

    ((Eval("m_Closing")!= DBNull.Value ? Convert.ToDouble(Eval("m_Closing")) : 0) * 
    (Eval("ChemicalRatePerUnit")!= DBNull.Value ? Convert.ToDouble(Eval("ChemicalRatePerUnit")) : 0)).ToString()
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, March 27, 2017 10:40 AM

All replies

  • User-1716253493 posted

    This because the data is null, the trick is ensure the result always has value

    select isnull(m_Closing,0) as m_Closing

    Saturday, March 25, 2017 4:38 AM
  • User2033107836 posted

    Can it be possible to do in aspx page in this below code

     <asp:Label ID="L4" runat="server" Text='<%# Convert.ToDouble(Eval("m_Closing")) * Convert.ToDouble(Eval("ChemicalRatePerUnit"))%>'></asp:Label>
                           

    Saturday, March 25, 2017 4:49 AM
  • User-1716253493 posted

    vb

    IIF(IsDbNull(Eval("closing")),0,Convert.ToDouble(Eval("Closing")))

    c#

    Eval("closing")!=null ? Convert.ToDouble(Eval("closing")), 0

    vb

    Convert.ToDouble(IIF(IsDbNull(Eval("closing")),0,Eval("Closing")))

    Saturday, March 25, 2017 7:01 AM
  • User2033107836 posted

    vb

    IIF(IsDbNull(Eval("closing")),0,Convert.ToDouble(Eval("Closing")))

    c#

    Eval("closing")!=null ? Convert.ToDouble(Eval("closing")), 0

    vb

    Convert.ToDouble(IIF(IsDbNull(Eval("closing")),0,Eval("Closing")))

    I'm using asp.net 2.0 C#

    getting error

    Syntax error, ':' expected

    Saturday, March 25, 2017 10:37 AM
  • User-1716253493 posted

    i mean

    Eval("closing")!=null ? Convert.ToDouble(Eval("closing")) : 0

    Saturday, March 25, 2017 10:40 AM
  • User2033107836 posted

    i mean

    Eval("closing")!=null ? Convert.ToDouble(Eval("closing")) : 0

    Still the same error

    Object cannot be cast from DBNull to other types.

    Saturday, March 25, 2017 10:48 AM
  • User2033107836 posted

    This is my code for Multiply

    <asp:Label ID="L4" runat="server" Text='<%# Convert.ToDouble(Eval("m_Closing")) * Convert.ToDouble(Eval("ChemicalRatePerUnit"))%>'></asp:Label>

    Your Code Please

    Saturday, March 25, 2017 10:51 AM
  • User-1780421697 posted

    As you already now that there is some issue with your data that raise casting issue, why not you have a public method in your code behind and you pass parameters from UI to method , its easy to debug that function in Cb and you can have try catch block over there, you can do more..

    Saturday, March 25, 2017 11:57 AM
  • User2033107836 posted

    any other way to do it in rowdatabound.. to avoid this error

    Saturday, March 25, 2017 12:07 PM
  • User-832373396 posted

    Hi asp.ambur,

    Please refer to code below:

    <asp:Label ID="L4" runat="server" Text='<%= Eval("m_Closing") * Eval("ChemicalRatePerUnit")%>'></asp:Label>

    please have a try.

    Best regards,

    Jolie
     
                          

    Saturday, March 25, 2017 1:53 PM
  • User-1716253493 posted
    ((Eval("m_Closing")!=null ? Convert.ToDouble(Eval("m_Closing")) : 0) * 
    (Eval("ChemicalRatePerUnit")!=null ? Convert.ToDouble(Eval("ChemicalRatePerUnit")) : 0)).ToString()

    Saturday, March 25, 2017 3:36 PM
  • User2033107836 posted

    ((Eval("m_Closing")!=null ? Convert.ToDouble(Eval("m_Closing")) : 0) * 
    (Eval("ChemicalRatePerUnit")!=null ? Convert.ToDouble(Eval("ChemicalRatePerUnit")) : 0)).ToString()

    Again same error

    Object cannot be cast from DBNull to other types.

    Sunday, March 26, 2017 7:53 AM
  • User-1509636757 posted

    Object cannot be cast from DBNull to other types.

    ((Eval("m_Closing")!= DBNull.Value ? Convert.ToDouble(Eval("m_Closing")) : 0) * 
    (Eval("ChemicalRatePerUnit")!= DBNull.Value ? Convert.ToDouble(Eval("ChemicalRatePerUnit")) : 0)).ToString()
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, March 27, 2017 10:40 AM