Click here to Skip to main content
15,890,336 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am having issues for a project for work and was wondering if anybody can help me. I am trying to calculate The percentage for refurb for the day. The formula is
Refurb_Rate = (totalRefurb / totalUnits * 100)
The program is to supposed count the refurb in the db and return a value in a textbox. I initialized my values to 0, but my problem is how do I get it to calculate each time a user scans in a unit to be refurbed. Anything I try to do gives me an error in my program. Right now it's returning a value of of 1 because I added RefurbRate++. Can anyone help me? Thanks.


Justin

Here is what I've done with my code:

C#
private int GetRefurbRate()
{
     string sql = "";
     int Refurb_Rate = 0;
     int totalRefurb = 0;
     int totalUnits = 0;
     string error_msg = "";

     sql = "SELECT COUNT(*) " +
            "FROM " + schema + ".repair_part rp  " +
            "WHERE rp.repair_ord = '" + txtRO.Text + "' ";
     while (true)
     {
          if (!myDb.RunSql(sql, true))
          {
             error_msg = "DBError for getting Refurb Rate";
             break;
          }
          if (myDb.dbRdr.HasRows)
          {
              if (myDb.dbRdr.Read())
              {
                 try
                 {
                     Refurb_Rate = (totalRefurb / totalUnits * 100);
                 }
                 catch (Exception e)
                 {
                      Console.WriteLine(e);
                 }
               }
              //Refurb_Rate= Convert.ToInt32(myDb.dbRdr.GetValue(10));
              Refurb_Rate++;
          }
          break;
     }
     myDb.dbRdr.Close();
     if (error_msg != String.Empty)
     {
         MessageBox.Show(error_msg, "Get Refurb Rate",
                          MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
     }
     return Refurb_Rate;
}
Posted
Updated 8-Apr-11 11:01am
v2

I can see few problems. You don't need this exception processing here. Instead, do this:

C#
double refurbRate;
int totalRefurb = //?
int totalUnits = //?
System.Diagnostics.Debug.Assert(totalUnits >= 0 && totalRefurb >= 0 && totalUnits >= totalRefurb);
refurbRate = totalUnits > 0 ? (double)totalRefurb * 100 / totalUnits : 0;


Even better:

C#
double refurbRate;
uint totalRefurb = //?
uint totalUnits = //?
System.Diagnostics.Debug.Assert(totalUnits >= totalRefurb);
refurbRate = totalUnits > 0 ? (double)totalRefurb * 100 / totalUnits : 0;


Zero percentage for zero total units is quite logical. It should not be the exception.

You should not use integer percentage (if fact, never). In you code, you get wrong coding due to rounding, you could improve it by multiplying by 100 first, but double type is better. If you need integer on output, it will be shown via appropriate format in string.Format.

You also misuse exceptions: handle it too locally, to early.

Find some instructions of better exception handling here:
How do i make a loop that will stop when a scrollbar reaches the bottom[^]
When i run an application an exception is caught how to handle this?[^]

Good luck,
—SA
 
Share this answer
 
v4
Comments
JOAT-MON 8-Apr-11 23:03pm    
5 - I agree with your answer, however, OP still needs to assign a value to his variables or his results will never reflect anything practical.
Sergey Alexandrovich Kryukov 8-Apr-11 23:23pm    
Thank you.
What do you mean "still needs to assign..."? Who says he does not? It will always be assigned (except Assert fails, but that's only in the case of bug, that's why it is Assert, not throw).
(I hope you don't mean my "//?", the meaning is that some value is to be supplied via a parameter of a field...)
--SA
JOAT-MON 8-Apr-11 23:30pm    
Lol...yep, missed it. My brain filtered out the comments in your answer. :)
Sergey Alexandrovich Kryukov 9-Apr-11 0:13am    
Cool. No problem.
Cheers.
--SA
Albin Abel 9-Apr-11 0:02am    
Good advice. My 5. I have given my answer through a simple math formula which tweaks the exception.
SAKryukov given you a good explanation. One thing it stresses don't handle the exception in this way.

I think little mathematics helps you c = a/b is equivalent to c= a* ( b power -1)

So to tweak .net you can use Refurb_Rate = (totalRefurb * System.Math.Pow(totalUnits,-1))*100.

Now it won't throw exceptions, but if divided by zero the result will be a NAN or Infinity. After assignment check for a valid value Refurb_Rate, if not valid value then as your wish, assign a zero?.

Cheers.
 
Share this answer
 
Firstly I see here a division of type 0/0: Refurb_Rate = (totalRefurb / totalUnits * 100), which is illegal.
Second, it would be nice to see if what is the value of: myDb.dbRdr.GetValue(10) (as I've understood that's the line where you get the error). If you get an exception here, then most probably the value that you get from the database is in a wrong format.
Regards
 
Share this answer
 
Comments
Justiin1265 8-Apr-11 17:34pm    
I know you can't 0/0, but I was told that you intialize your valribles by 0. My problem is how do I get them to recognize other numbers to get the daily refurb rate. myDb.dbRdr.GetValue(10) has no importance to the code I received the errors after I commented it out.
JOAT-MON 8-Apr-11 19:01pm    
You have not assigned any value to totalUnits or totalRefurb after initializing them to 0. You need to assign a value to them from the rows that you read in.

For instance, right now you are running a scalar query that returns the total refurbs for a particular order. If this is the intended value that you are trying to get a rate off of, then it needs to be assigned to totalRefurb before the calculation. Also before the calculation, you need to get the total units from that order and assign to totalUnits.
Sergey Alexandrovich Kryukov 8-Apr-11 22:13pm    
Unfortunately, there is no correct explanation on how to fix the error in first line.
The type for percentage is wrong, order of operation is wrong, too (if integers are used).
Also, uint is better then int.

Please see my answer for the explanations.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900