1

I have a problem in refreshing my listview after going back to main form ! This is my listview : ID -- number -- supplier -- total -- Note

I have form Called Bills, when I press on New bill button, a new form called bill_Details open .. I added new bills and close it. after closing I added load_Values() function to refresh the listview in bills form.

private void btn_add_Click(object sender, EventArgs e)
    {
        Bill_Details b = new Bill_Details();
        b.ShowDialog();

        load_Values();//this to load the listview
    }

the problem is, if total in my new bill is 10.02, and I returned from bill_Details form to bills form, a new bill record added to listview, but the total is not right ! it may appear 0 ! This is load_Values function:

public void load_Values()
    {
        try
        {
            txbSearch.Text = "";

            objConn.Open();
            listView1.Items.Clear();
            OleDbCommand cmd = new OleDbCommand("select a.bill_Id,a.bill_Number,a.bill_Date,c.sup_Name,Sum(b.de_NetPrice),a.bill_Note from (suppliers c right JOIN bills a on c.sup_Id = a.bill_From) LEFT JOIN  bill_Details b on a.bill_Id = b.bill_Id  group by a.bill_Id,a.bill_Number,a.bill_Date,c.sup_Name,a.bill_Note order by a.bill_Date desc", objConn);
            OleDbDataReader dataReader = cmd.ExecuteReader();

            float valueAfterTax = 0, valueAfterDis = 0, summ = 0;
            string filename = Application.StartupPath + "\\config.ict";
            Serializer serializeFromFile = new Serializer();
            settings = serializeFromFile.DeSerializeObject(filename);
            valueAfterTax = float.Parse(settings.taxRate) / 100;
            valueAfterDis = float.Parse(settings.disRate) / 100;

            int i = 0;
            while (dataReader.Read())
            {
                listView1.Items.Add(dataReader.GetValue(0).ToString());
                listView1.Items[i].SubItems.Add(dataReader.IsDBNull(1) ? "-" : dataReader.GetValue(1).ToString());

                DateTime dt = DateTime.Parse(dataReader.GetValue(2).ToString());
                listView1.Items[i].SubItems.Add(dt.ToShortDateString());
                listView1.Items[i].SubItems.Add(dataReader.IsDBNull(3) ? "0" : dataReader.GetString(3));

                summ = (float.Parse(dataReader.IsDBNull(4) ? "0" : dataReader.GetDouble(4).ToString("n2")) * valueAfterTax) + float.Parse(dataReader.IsDBNull(4) ? "0" : dataReader.GetDouble(4).ToString("n2")) - (float.Parse(dataReader.IsDBNull(4) ? "0" : dataReader.GetDouble(4).ToString("n2")) * valueAfterDis);
                listView1.Items[i].SubItems.Add(summ.ToString("n2"));
                listView1.Items[i].SubItems.Add(dataReader.IsDBNull(5) ? "-" : dataReader.GetString(5));


                i++;
            }
            objConn.Close();
        }
        catch (Exception er)
        {
            MessageBox.Show(er.Message);
        }

after that it I reload bill form, the total for this record appears correctly.

Belal_G
  • 75
  • 1
  • 2
  • 10
  • 1
    When working with money, its *highly* recommended to use the `Decimal` type, not float or double due to problems with rounding errors and type accuracy. Not that Decimal doesn't have the problems, but its the highest accuracy built-in type. – Ron Beyer Jun 10 '15 at 20:54
  • let's make that __H-I-G-H-L-Y__ recomended – TaW Jun 10 '15 at 20:57
  • yeah, I heard this info. , but in my application I didn't use rounding or.. I'm using money just like numbers. – Belal_G Jun 10 '15 at 20:58
  • @TaW, I don't think this is the problem !! in form loading the TOTAL appear correctly !! – Belal_G Jun 10 '15 at 21:00
  • 1
    _I'm using money just like numbers_ That is not possible . Binary numbers can only store most numbers as approximations. You can't e.g. store 0.7 in a float.. Just as you can't store 1/3 in a decimal. (Or float..) – TaW Jun 10 '15 at 21:03
  • But the problem is not in rounding ! , as ex. if the real total 11.67, it's appear 0.00 !! – Belal_G Jun 10 '15 at 21:09
  • Indeed it is not rounding. It is the fact that you just __can't store fractional decimal numbers in binary variables__. This is a mistake, fix it and if you still have a problem we can look again. You divide by 100. Bang, error..not all results will look wrong, but that won''t help, will it? You need every number right, so you need to use a data type that can get it right. float and double simply can't. (Unless your fraction can be written as a power of 2..) – TaW Jun 10 '15 at 21:23
  • I add Thread.Sleep(1000); before load_Values(); and it's work now !! but I will try decimal – Belal_G Jun 10 '15 at 21:25
  • You have some very complicated calculations in the sub items, try breaking them out to individual variables (longer code does NOT mean slower code) and debugging. I'm guessing that the result of a math operation is not what you expect it to be. – Ron Beyer Jun 10 '15 at 21:26
  • @Taw , I convert them to decimal , same problem ! – Belal_G Jun 10 '15 at 21:32
  • @RonBeyer, all calculations are correct , I don't have problem in it. the problem in loading after go back to main form. if I reload the form manually it show me the value. – Belal_G Jun 10 '15 at 21:34
  • Try instead of calling `load_Values()` directly in the button click, wrap it in an Invoke, `this.Invoke((MethodInvoker) delegate { load_Values(); });`. I think you are experiencing a cross-threading and race condition issue. Also at the top of the load_Values method, call `listview1.BeginUpdate()` and then at the end of the method call `listview1.EndUpdate()`, which suspends drawing until you finish updating/adding all the items. – Ron Beyer Jun 11 '15 at 01:14
  • Hm looks like the floats are not the problem. (They still are wrong! As is the double!! Convert all to decimal and you have onel less error in your code!!) - But the real problem does indeed look like a timing/race(threading problem, as Ron noted. But: I see no threads in the code you show.. so that is still a mystery! - What is happening in the `b.ShowDialog();` form? Anything you write to the DB there?? – TaW Jun 11 '15 at 07:31
  • See [here](http://stackoverflow.com/questions/30780566/how-to-explain-8-2-8-0-19999999999-in-c-sharp) for a recent post wrt float vs decimal.. – TaW Jun 11 '15 at 13:13

0 Answers0