Divide By Zero Error

15 posts, 0 answers
  1. Mike
    Mike avatar
    338 posts
    Member since:
    Jun 2008

    Posted 11 Feb 2009 Link to this post

    I am using this to catch divide by zero errors and it's not working.  What am I doing wrong with this expression in the report?

     

    =iif(Fields.Price2 <>0,(Fields.Price1 /Fields.Price2) * 1000,0) 
  2. Steve
    Admin
    Steve avatar
    10940 posts

    Posted 12 Feb 2009 Link to this post

    Hello Mike,

    Can you explain what do you mean by "it's not working"? I've just tried your expression within a sample report and it works fine on my end (sample report attached). Simply change a record for the UnitPrice to return 0.

    Regards,
    Steve
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  3. Mike
    Mike avatar
    338 posts
    Member since:
    Jun 2008

    Posted 12 Feb 2009 Link to this post

    I use that formula and it returns a red box with the message:

    "An error occured while processing TextBox "TextBoxPrice":
     Attempt to deivide by zero.

  4. Steve
    Admin
    Steve avatar
    10940 posts

    Posted 12 Feb 2009 Link to this post

    Hello Mike,

    I've attached a sample report in my previous post and would appreciate it if you modify it to replicate the error, so that we can investigate.

    All the best,
    Steve
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  5. Mike
    Mike avatar
    338 posts
    Member since:
    Jun 2008

    Posted 12 Feb 2009 Link to this post

    Steve,
    It looks like both values are coming back zero! darn it! 

    =iif(Fields.Price1 <> 0, (iif(Fields.Price2<> 0, Fields.Price2, 1)/iif(Fields.Price1 <> 0, Fields.Price1 , 1) * 1000), 0)

  6. Jean
    Jean avatar
    12 posts
    Member since:
    Feb 2009

    Posted 20 Feb 2009 Link to this post

    It seems that telerik evalues the truepart and the falsepart whatever the condition (and not the false part if condition is false, the true part if condition is true). And if one of the part generate an exception (access member of null value, divide by zero, ...), there is an error message.

    For my example =IIf(ClassLibrary1.Test.CheckIsNull(Fields.Agent)=1,"Unknown !!", Fields.Agent.FirstName)
    => The expression contains object 'FirstName' that is not defined in the current context on row where Fields.Agent is null.
  7. Steve
    Admin
    Steve avatar
    10940 posts

    Posted 23 Feb 2009 Link to this post

    Hi Fabrice,

    The experienced behavior is correct and by design. As you can see IIF() is a function and you need to prepare the parameters prior to executing the function (as most of the programming languages work).

    That's why the reporting engine first evaluates the parameters and then pass the result values to the function. Checking for null values can be done either on database level or using our built-in IsNull() function.

    Regards,
    Steve
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  8. Jean
    Jean avatar
    12 posts
    Member since:
    Feb 2009

    Posted 23 Feb 2009 Link to this post

    I understand why but there is no workaround (except by code .net of course) for me because Fields.Agent.FirstName always generates an exception when Fields.Agent is null because it  is always evaluated.
  9. Mike
    Mike avatar
    338 posts
    Member since:
    Jun 2008

    Posted 23 Feb 2009 Link to this post

    Here is what I have done to keep from using the iif and have more control over the data I am showing in the reports.

    Code Behind of the report.aspx.cs page.
    1  private void RunReport()  
    2     {  
    3         ReportDetail thisReport = new ReportDetail();  
    4         thisReport.GetReport(Convert.ToDateTime(RadDatePickerStartDate.SelectedDate),  
    5                              Convert.ToDateTime(RadDatePickerEndDate.SelectedDate),  
    8                              Convert.ToString(RadComboBoxUser.SelectedValue));  
    9         ReportViewer1.Report = thisReport ;  
    10     } 

    Code Behind for the ReportDetail.cs which is in a separate Reports Project....
    1     using System;  
    2     using System.Data;  
    3     using System.Collections.Generic;  
    4     using System.Text;  
    5     using System.Data;  
    6     using System.Web.UI;  
    7  
    11     public partial class ReportDetail : Telerik.Reporting.Report  
    12     {  
    13         #region Fields  
    14         private ReportBLL _report = new ReportBLL();  // My Report Business Object in a seperate project
    15         #endregion  
    16  
    17         /// <summary> 
    18         /// Initializes a new instance of the <see cref="ReportDetail"/> class.  
    19         /// </summary> 
    20         public ReportDetail ()  
    21         {  
    22             /// <summary> 
    23             /// Required for telerik Reporting designer support  
    24             /// </summary> 
    25             InitializeComponent();  
    26         }  
    27  
    28  public void GetReport(DateTime StartDate, DateTime EndDate, int UserId)  
    29         {  
    30             _refreshList.StartDate = Convert.ToDateTime(StartDate);  
    31             _refreshList.EndDate = Convert.ToDateTime(EndDate);  
    32             _refreshList.UserId = Convert.ToInt(UserId);  
    33             DataTable dt = _report.UserActivity;  
    34             Report.DataSource = GetReportData(dt);  
    35         }  
    36 private List<ReportMain> GetReportData(DataTable dt)  
    37         {  
    38             IList<ReportMain> data = new List<ReportMain>();  
    39             foreach (DataRow dr in dt.Rows)  
    40             {  
    41                 // We had some bad test data (-12826613928.3934) so CheckInt32 is now used for Value1
    42                 string Value1 = ReportMain.CheckInt32(Convert.ToString(dr["Sales"]));  
    43                 ReportMain reportData = new ReportMain();  
    44                 reportData.Old = Convert.ToInt32(dr["OLD"]);  
    45                 reportData.New = Convert.ToInt32(dr["NEW"]);  
    46                 reportData.CalculatedValue = ReportMain.DivideValues(Convert.ToInt32(Value1), Convert.ToInt32(dr["Value2"]));  
    47                 data.Add(reportData);  
    48             }  
    49  
    50             return data as List<ReportMain>;  
    51         }  

    I just decided to make a ReportMain.cs class which has private fields, public properties public properties and statics for division.  The public properties are what I bind to the report.  Here is just a sample of that class and I use the "New" as a field in the report.  I also have the Stored Procedure use the same names so there is no confusion when creating the report.
    1 #region Fields  
    2  private int _new;  
    3 #endregion  
    4  
    5 #region Properties  
    6 public int New  
    7     {  
    8      get { return _new; }  
    9      set { _new = value; }  
    10      }  
    11 #endregion  
    12  
    13 public static decimal DivideValues(decimal Value1, decimal Value2)  
    14     {  
    15      try  
    16        {             
    17         return (Convert.ToDecimal(Value1) / Convert.ToDecimal(Value2));  
    18        }  
    19      catch  
    20        {  
    21         return 0;  
    22         }  
    23      } 
  10. Ryan
    Ryan avatar
    2 posts
    Member since:
    Oct 2010

    Posted 29 Jun 2011 Link to this post

    If you want a less robust and more "Hacky" workaround, just make each expression evaluate without an error.

    =iif(Fields.Balance1 = 0,'NA',(((Fields.Balance2-Fields.Balance1)/iif(Fields.Balance1=0,1,Fields.Balance1))))
  11. Vitrum
    Vitrum avatar
    3 posts
    Member since:
    Jun 2009

    Posted 07 Nov 2013 Link to this post

    =Fields.Price1 / iif(Fields.Price2<>0, Fields.Price2, Null)
  12. Javier
    Javier avatar
    16 posts
    Member since:
    Feb 2015

    Posted 28 May 2015 Link to this post

    Thanks Vitrum,

     

    I just modify it a little bit to return 0 instead of null

     =iif(Fields.New_customers<>0,Fields.adCost/ iif(Fields.New_customers<>0,Fields.New_customers,null),0)

     

    Thanks again

  13. Nasko
    Admin
    Nasko avatar
    1077 posts

    Posted 29 May 2015 Link to this post

    Hello Javier,

    The latest version of Telerik Reporting - Q1 2015 SP1 introduced logical operators allowing only the true or false part to be evaluated:

    • The conditional operator "?:" returns one of the two values depending on the value of a Boolean expression. Usage: = Fields.B <> 0 ? Fields.A / Fields.B : "N/A"

    • Null-coalescing operator "??". It returns the left-hand operand if the operand is not null, otherwise it returns the right hand operand. Usage: = Fields.A ?? "N/A"


    Regards,
    Nasko
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  14. Javier
    Javier avatar
    16 posts
    Member since:
    Feb 2015

    Posted 03 Jun 2015 in reply to Nasko Link to this post

    Thanks Nasko.

     

    Works like a charm

  15. Rod
    Rod avatar
    15 posts
    Member since:
    May 2010

    Posted 20 Jan in reply to Nasko Link to this post

    Thank you! You just saved me a bunch of time.
Back to Top