0

I am having trouble passing data from one form to another. I have looked at several examples and tried them, but nothing is working.

In "Estimate" Form this is what opens the "EstimateTakeoffItemAdd" Form...

    private void TakeoffGridAdd(object sender, FormClosingEventArgs e)
    {
        MessageBox.Show(AutoID.ToString());

        int index = 0;

        if (AutoID > 0)
        {
        }
     }

    private void addDialog()
    {
        if (treeView1.SelectedNode.Name.ToString() != "c")
        {
            using (var et = new EstimateTakeoffItemAdd(estID, Convert.ToInt32(treeView1.SelectedNode.Name.ToString()), Convert.ToInt32(SheetSelector.SelectedValue), Convert.ToInt32(BidItemSelector.SelectedValue), Convert.ToInt32(SortCodeSelector.SelectedValue)))
            {
                et.FormClosing += new FormClosingEventHandler(TakeoffGridAdd);
                et.ShowDialog();
                AutoID = et.AddedID;
            }
        }
    }

In the "EstimateTakeoffItemAdd" Form this is the function that saves/closes the form...

    public int AddedID { get; set; }
    public bool ItemAdded { get; set; }

    private void radButton2_Click(object sender, EventArgs e)
    {
        pricemod = (Convert.ToDecimal(LaborText.Text) == dbLabor) ? 0 : 1;
        pricemod = (Convert.ToDecimal(MaterialText.Text) == dbPrice) ? 0 : 1;

        try
        {
            DBConnector db = new DBConnector();
            db.query = "SELECT Auto_increment FROM information_schema.tables WHERE table_name='tb_estimate_takeoff'";                
            this.AddedID = Convert.ToInt32(db.executeScalar());
        }
        finally
        {
            try
            {
                DBConnector db = new DBConnector();
                db.query = "INSERT INTO tb_estimate_takeoff (item, estimate, sheet, qty, hrs, mat, biditem, phase, matunit_mod, labunit_mod, guid) VALUES (" +
                           "'" + DescriptionText.Text + "', " +
                           estID + ", " +
                           sheet + ", " +
                           QtyText.Value + ", " +
                           LaborText.Text + ", " +
                           MaterialText.Text + ", " +
                           biditem + ", " +
                           phase + ", " +
                           pricemod + ", " +
                           labormod + ", " +
                           "'" + guid + "'" +
                          ")";
                db.executeNonQuery();
            }
            finally
            {
                ItemAdded = true;
                this.Close();
            }
        }
    }

What I need is to get the AddedID back to the main "Estimate" form. Using a MessageBox before closing "EstimateTakeoffItemAdd" I can see that AddedID is working on that form, however it shows a 0 on the "Estimate" form.

After opening the dialog and closing for the second time, the first AddedID is then available to the "Estimate" form.

What am I missing?

6
  • Looks fine. however it shows a 0 on the "Estimate" form. <-- Can you show us where you do this?. Also, when you step through your code in the debugger, and you look at the value of et.AddedID on the line AutoID = et.AddedID;, what do you see?
    – sstan
    Jul 3, 2015 at 19:33
  • By using the Message box in the FormClosing function...Edited above.
    – Mike
    Jul 3, 2015 at 19:35
  • Can you edit your post instead of putting it in the comment? That way, it will have proper formatting, and everyone else will see it as well. Thanks!
    – sstan
    Jul 3, 2015 at 19:37
  • Are you sure about this: SELECT Auto_increment FROM information_schema.tables WHERE table...? I can't see any Auto_Increment column in information_schema.Tables and this leads to unexpected behavior in the following code. What database are you using?
    – Steve
    Jul 3, 2015 at 19:44
  • 3
    The TakeoffGridAdd() method is buggy, it uses the AutoId variable before it is assigned. You shot your leg off by not using the dialog properly, get rid of the FormClosing event handler. Jul 3, 2015 at 19:46

2 Answers 2

1

It sounds like the value is getting passed just fine. You are simply testing the value at the wrong time from the wrong place.

It would appear that you have registered the TakeoffGridAdd event handler to execute when the EstimateTakeoffItemAdd form is closing.

If this is true, then the line:

et.ShowDialog();

... hasn't returned yet, because the form is not closed yet. That means that the assignment AutoID = et.AddedID; also hasn't executed yet.

Because your method EstimateTakeoffItemAdd is running before the assignment is made, you always appear to be reading an old value.

0
0

Thanks @Hans Passant and @sstan. Getting rid of the FormClosing event handler solved my problem.

    private void addDialog()
    {
        if (treeView1.SelectedNode.Name.ToString() != "c")
        {
            using (var et = new EstimateTakeoffItemAdd(estID, Convert.ToInt32(treeView1.SelectedNode.Name.ToString()), Convert.ToInt32(SheetSelector.SelectedValue), Convert.ToInt32(BidItemSelector.SelectedValue), Convert.ToInt32(SortCodeSelector.SelectedValue)))
            {
                if (et.ShowDialog() == DialogResult.OK)
                {
                    AutoID = et.AddedID;
                    TakeoffGridAdd();
                }
            }
        }
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.