clearing a subobject...

Posts   
 
    
HcD avatar
HcD
User
Posts: 214
Joined: 12-May-2005
# Posted on: 26-May-2005 15:29:19   

hi,

I have an issue with databinding but i don't know if it's really LLBL or .Net who is the culprit :-)

following situation : I have an ArticleEntity which is linked m:1 to a Supplier (using the nullable foreign key SUP_ID, because an article doesn't have to be linked)

In my GUI i have a combobox with all exisiting suppliers to chose from and a "clear" button.

In my initialize code i fill the combobox using suppliercollection.getmulti(null), setting the Datasource = suppliercollection, DisplayMember = "SupName" and ValueMember = "SupId" I also add the following databinding statement to bind it to my currentArticle

comboSuppliers.Databindings.Add("SelectedValue",currentArticle,"SupId");

When i don't select a supplier at first, the SUP_ID in the article is NULL, as it should be. Also selecting a supplier works, and when i save my article it has the correct SUP_ID in the DB. The problem lies in pressing the "clear" button once you allready selected a supplier.

If i use

currentArticle.SetNewFieldValue((int)ArticleFieldIndex.SupId, null);

the data is persisted correctly, but the combobox gui is not updated. If i use

currenctArticle.SupId = 0;

the GUI is updated, but not as it should be, the first item is selected instead of no selection at all. And also persisting fails because of FK-constraint, which is normal because there is no supplier with SUP_ID = 0

I found that if i use

currenctArticle.SupId = 0;
currenctArticle.SupId = 0;

the GUI is updated correctly (no supplier selected, blank combobox). Anyone knows if this is a known issue that you have to do the statement twice ??

So i ended up with

currenctArticle.SupId = 0;
currenctArticle.SupId = 0;
currentArticle.SetNewFieldValue((int)ArticleFieldIndex.SupId, null);

this will update the GUI, and set the SupId to NULL so the article.Save works.

It looks kinda fishy code though ... is there a better way ??

oh and i DID try currentArticle.Supplier = null (no gui update), and removing and readding the bindings is no option (it is a huge form, and it would get too complex if i have to do this for multiple controls)

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 27-May-2005 10:50:26   

It COULD be related to the scheduling of events that was mentioned in another thread and which will be fixed later today (it's fixed, but the new installer is out later today). So event fires, controls update and the dereferencing is done after that. This explains why using the statement TWICE works.

When you grab the new installer later today, please let me know if that fixes your problem simple_smile

Frans Bouma | Lead developer LLBLGen Pro
HcD avatar
HcD
User
Posts: 214
Joined: 12-May-2005
# Posted on: 28-May-2005 01:20:55   

Nopes, still the same (i did a regenerate Full/Safe of my entire datalayer)

i experimented a bit further and got the following strange result:

When i set the id of the subobject to 0, the bound combobox selects the first item in the list. Whet setting the id again to 0, then the combobox selection is cleared.

But it gets weirder, in my forms i have two of those comboboxes (one for 'loonwerker', one for 'transporteur')so in my code, i first fill my two comboboxes (each has a separate list), then bind the "selectedvalue" of the combobox to the corresponding subobject id of my main entity and i have for each combobox a "clear" button with the following code:


        private void btnGeenTransporteur_Click(object sender, System.EventArgs e)
        {
            //due to a strange databinding issue, i have to call the following line twice to update the gui before setting the value to NULL
            m_currentEntity.RecTransportId = 0;
            m_currentEntity.RecTransportId = 0;
            m_currentEntity.SetNewFieldValue((int)ReceptionFieldIndex.RecTransportId, null);
        }

        private void btnGeenLoonwerker_Click(object sender, System.EventArgs e)
        {
            //due to a strange databinding issue, i have to call the following line twice to update the gui before setting the value to NULL
            m_currentEntity.RecLoonwerkId = 0;
            m_currentEntity.RecLoonwerkId = 0;
            m_currentEntity.SetNewFieldValue((int)ReceptionFieldIndex.RecLoonwerkId, null);
        }

If i now change the code so the '=0' assignments only appear once, i get the following behavior : -> i first select for both comboboxes a value -> then i click btnGeenTransporteur. The transporteur combobox selecteditem goes to the first item. If i would click the button again, the combobox would blank (what i want), but suppose i first click the btnGeenLoonwerker. The loonwerker combobox selecteditem goes to the first item, AND the transporteur combobox blanks. If i then click EITHER of the two buttons, the loonwerker combo also blanks. Now that's what i call weird behavior stuck_out_tongue_winking_eye

Anyway, i'll just stick to doing the '=0' assignment twice, no problems with it, but it's just weird.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 28-May-2005 11:51:59   

It's a bit hard to track down when the combo box exactly reacts and based on what event... It could be it reacts on the OnfkfieldChanged event, not sure... I'm pretty sure it's about an event that's fired at the wrong time... Now we just have to track down which one wink

To test something out, in the m_currentEntity.RecLoonwerkId property Set there is a call to OnRecLoonwerkIdChanged(). Just for test, for this particular routine, if you move that call to SetNewFieldValue() in the same entity, after the value is set and BEFORE the related entity's dereferenced, Does it work then? (to see if that's the cause).

Frans Bouma | Lead developer LLBLGen Pro
HcD avatar
HcD
User
Posts: 214
Joined: 12-May-2005
# Posted on: 28-May-2005 13:30:04   

Just to see if i'm on the right track here, the Set property of that RecLoonWerkId property is :


            set
            {
                if(SetNewFieldValue((int)ReceptionFieldIndex.RecLoonwerkId, value))
                {
                    OnRecLoonwerkIdChanged();
                }
            }


So i copied the OnRecLoonWerkIdChanged() call into the same file's SetNewFieldValue method, i have put it at the last line before "return toReturn;"

In my GUI, now i only have to put the "=0" assigment code once instead of twice for blanking the combobox confused

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 28-May-2005 13:52:25   

Right above the return? Ok, but also comment out the event call in the set clause. Now you get the event twice so it is the same as setting it twice.

Frans Bouma | Lead developer LLBLGen Pro
HcD avatar
HcD
User
Posts: 214
Joined: 12-May-2005
# Posted on: 28-May-2005 15:39:31   

Ok, i commented it out in the SET, now in my GUI code i can clear the combobox by calling

            m_currentEntity.RecTransportId = 0;
            m_currentEntity.SetNewFieldValue((int)ReceptionFieldIndex.RecTransportId, null);

instead of

            m_currentEntity.RecTransportId = 0;
            m_currentEntity.RecTransportId = 0;
            m_currentEntity.SetNewFieldValue((int)ReceptionFieldIndex.RecTransportId, null);

It's still odd though that i HAVE to first do the =0 assignment ...

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 28-May-2005 16:21:57   

HcD wrote:

Ok, i commented it out in the SET, now in my GUI code i can clear the combobox by calling

            m_currentEntity.RecTransportId = 0;
            m_currentEntity.SetNewFieldValue((int)ReceptionFieldIndex.RecTransportId, null);

instead of

            m_currentEntity.RecTransportId = 0;
            m_currentEntity.RecTransportId = 0;
            m_currentEntity.SetNewFieldValue((int)ReceptionFieldIndex.RecTransportId, null);

It's still odd though that i HAVE to first do the =0 assignment ...

SetNewFieldValue(...null) will call the changed event now as well. SetNewFieldValue(...null) will set the RecTransportId value to null, but not fire the OnRecTransportIdChanged event normally.

Frans Bouma | Lead developer LLBLGen Pro
HcD avatar
HcD
User
Posts: 214
Joined: 12-May-2005
# Posted on: 29-May-2005 00:36:50   

And now i know it for sure ...it has nothing to do with LLBLGen, it's a genuine combobox "bug(?)"

I tested it with an app with a form that has a combobox and a button, and the following source


        private void button1_Click(object sender, System.EventArgs e)
        {
            comboBox1.SelectedIndex = -1;
        }

        private void Form1_Load(object sender, System.EventArgs e)
        {
            string[] names = {"test1","test2","test3","test4","test5","test6"};
            comboBox1.DataSource = names;
        }

If you select an item in the combobox, and then click button1, it goes to the first item in the combobox. Clicking button1 again finally blanks the combobox.

Weird stuff rage

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 29-May-2005 10:36:25   

thanks for clearing this up! simple_smile

Very weird indeed... databinding IS sometimes doing weird things, but this is indeed VERY weird.

If you set SelectedItem to null, does that make a difference (i.e.: instead of setting SelectedIndex to -1 )?

Frans Bouma | Lead developer LLBLGen Pro
HcD avatar
HcD
User
Posts: 214
Joined: 12-May-2005
# Posted on: 29-May-2005 16:45:13   

i replaced the buttonclick handler with

        comboBox1.SelectedItem = null;

and i get the exact same behavior ...going to the first item on the first click, then clearing on the second click.

The strange thing is, when i report the SelectedIndex


        private void button1_Click(object sender, System.EventArgs e)
        {
            comboBox1.SelectedIndex = -1;
            MessageBox.Show(comboBox1.SelectedIndex.ToString());
        }

it says the first time it's 0, and the second time it's -1. What has this world come to if you can't trust an assignment anymore stuck_out_tongue_winking_eye

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 30-May-2005 09:49:31   

HcD wrote:

i replaced the buttonclick handler with

        comboBox1.SelectedItem = null;

and i get the exact same behavior ...going to the first item on the first click, then clearing on the second click.

The strange thing is, when i report the SelectedIndex


        private void button1_Click(object sender, System.EventArgs e)
        {
            comboBox1.SelectedIndex = -1;
            MessageBox.Show(comboBox1.SelectedIndex.ToString());
        }

it says the first time it's 0, and the second time it's -1. What has this world come to if you can't trust an assignment anymore stuck_out_tongue_winking_eye

heh simple_smile

If you bind an event handler to the comboBox1.SelectedIndexChanged event, I'm pretty sure it's called twice: once for -1 and once for 0. Still weird, but that's databinding: dark side mistery wink

Frans Bouma | Lead developer LLBLGen Pro
zaad
User
Posts: 19
Joined: 13-Sep-2005
# Posted on: 28-Sep-2005 17:51:45   

Hello,

I had the same problem and I found this post.

Meanwhile I found a solution. (VS 2005)

It seems that when you add a bindingSource component to your form and use a collection instead of an entity, set the bindingSource.DataSource to the collection and bind your controls to the bindingSource, it work as expected.

I use DevExpress XtraEditors' lookupEdit with an extra Delete-button. On the click of that button I use SetNewFieldValue (null), and the combo gets cleared.

Regards,

zaad