Setting field value to null for an existing entity,

Posts   
 
    
usus avatar
usus
User
Posts: 27
Joined: 07-Sep-2006
# Posted on: 08-May-2007 14:28:37   

Hi, I am using the latest build of llblgenpro 21 March 2007, After upgrading, Although I am not sure if it existed before the upgrade but I faced the following problem with the following sample code.

        PersonelEntity personel = new PersonelEntity();
        personel.IsNew = false;
        personel.Serviskod = "1001";
        personel.Fields["Id"].ForcedCurrentValueWrite(323);
        personel.AdSoyad = "ARZU"; // String
        personel.Tckimlikno = null; // Decimal? nullable
        personel.IsBaslangic=null; // DateTime? nullable
        personel.SetNewFieldValue("PersonelEkipId", null); // Decimal?
        personel.Save();

It produces the following UPDATE SQL "UPDATE "DIGIBIZ"."TBLPERSONEL" SET "AD_SOYAD"=:AdSoyad1 WHERE ( "DIGIBIZ"."TBLPERSONEL"."SERVISKOD" = :Serviskod2 AND "DIGIBIZ"."TBLPERSONEL"."ID" = :Id3)"

Either I used to set null personel.IsBaslangic=null; or with setnewfieldvalue like below personel.SetNewFieldValue("PersonelEkipId", null);

None of the above approaches produces the required sql setting the given field to null

Any help please What I am doing wrong or what shoul i do? Best Regards Köksal

Aurelien avatar
Aurelien
Support Team
Posts: 162
Joined: 28-Jun-2006
# Posted on: 08-May-2007 16:49:37   

Hi,

why don't you create the entity with the pk in the constructor ?


PersonelEntity personel = new PersonelEntity(323);

by doing that, there is no need to do these 2 lines :


 personel.IsNew = false;

and 

personel.Fields["Id"].ForcedCurrentValueWrite(323);

In your code, I think the fields you are setting to null are already null, so the fields IsDirty flag is not set to True and there are no update emitted fot these fields.

usus avatar
usus
User
Posts: 27
Joined: 07-Sep-2006
# Posted on: 08-May-2007 17:45:14   

Hi, I did this, because I want to update directly without fetching the record first. If I did your way (i.e, PersonelEntity personel=new PersonelEntity("1001",123) there is no problem. My question is How to make a field to null with direct update without fetching the record first? In my way, the field is already null at the beginning, and making null again does not actually change the field value, as a result of this it is not been considered in the sql. Best Regards Köksal

Aurelien avatar
Aurelien
Support Team
Posts: 162
Joined: 28-Jun-2006
# Posted on: 08-May-2007 17:55:54   

Hi,

maybe try to set IsChanged property of the field to True, but i'm not sure it will work.

vmorikawa
User
Posts: 5
Joined: 19-Jan-2007
# Posted on: 08-May-2007 18:29:48   

Try this:

personel.SetNewFieldValue((int)personelFieldIndex.PersonelEkipId, null);

usus wrote:

Hi, I am using the latest build of llblgenpro 21 March 2007, After upgrading, Although I am not sure if it existed before the upgrade but I faced the following problem with the following sample code.

        PersonelEntity personel = new PersonelEntity();
        personel.IsNew = false;
        personel.Serviskod = "1001";
        personel.Fields["Id"].ForcedCurrentValueWrite(323);
        personel.AdSoyad = "ARZU"; // String
        personel.Tckimlikno = null; // Decimal? nullable
        personel.IsBaslangic=null; // DateTime? nullable
        personel.SetNewFieldValue("PersonelEkipId", null); // Decimal?
        personel.Save();

It produces the following UPDATE SQL "UPDATE "DIGIBIZ"."TBLPERSONEL" SET "AD_SOYAD"=:AdSoyad1 WHERE ( "DIGIBIZ"."TBLPERSONEL"."SERVISKOD" = :Serviskod2 AND "DIGIBIZ"."TBLPERSONEL"."ID" = :Id3)"

Either I used to set null personel.IsBaslangic=null; or with setnewfieldvalue like below personel.SetNewFieldValue("PersonelEkipId", null);

None of the above approaches produces the required sql setting the given field to null

Any help please What I am doing wrong or what shoul i do? Best Regards Köksal

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 09-May-2007 08:53:16   

Yeah, try to set the IsChanged property of each field you want to be updated to true. This is only needed when you set an entityField to a value that is equal to its current value.

usus avatar
usus
User
Posts: 27
Joined: 07-Sep-2006
# Posted on: 10-May-2007 11:00:42   

Hi, I did as follows but nothing changed. What am I doing wrong?

        PersonelEntity personel = new PersonelEntity();
        personel.IsNew = false;
        personel.Serviskod = "1001";
        personel.AdSoyad = "ARZU";
        personel.Fields["Id"].ForcedCurrentValueWrite(323);
        personel.Tckimlikno = null;
        **PersonelFields.Tckimlikno.IsChanged = true**; // I added this line as you said.
        personel.Save();
        Console.ReadLine();

On the other hand, As I remember with the previous version of LLBLGEN I did not noticed this problem.

Best Regards Köksal

usus avatar
usus
User
Posts: 27
Joined: 07-Sep-2006
# Posted on: 10-May-2007 12:04:08   

Hi, I solved the problem as follows. But I am not sure that is the best solutions. I think in the initialization part of an entity, the fields other than string like Decimal? or DateTime? thay are being initialized to other than value null. I mean for decimal? it shoul be initialized to 0, and for DateTime? it should be initialized to for example DateTime.MinValue.

        PersonelEntity personel = new PersonelEntity();
        personel.IsNew = false;
        personel.Serviskod = "1001";
        personel.AdSoyad = "ARZU";
        personel.Fields["Id"].ForcedCurrentValueWrite(323);
        personel.Tckimlikno = null;

** personel.PersonelEkipId= 0; personel.PersonelEkipId = null;** ** personel.IsBaslangic = DateTime.MinValue; personel.IsBaslangic = null;** personel.Save(); Console.ReadLine();

Any ideas would be appriciated

Best Regards Köksal

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 10-May-2007 16:20:50   

There is nothing wrong with your solution, if it's working correctly. I thought setting the IsChanged flag should solve this.

A discussion about setting a field to null, where its initial value was null shouldn't change the field's IsChanged state led to a change in the logic of setting the IsChanged flad The change was made on the build v2.0.07.0317 of the runtime lib. Please refer to the discuassion here: http://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=9328

usus avatar
usus
User
Posts: 27
Joined: 07-Sep-2006
# Posted on: 11-May-2007 11:43:32   

I read the the thread but I did not understand why you changed the logic build v2.0.07.0317. What about my scenerio. I want to make an **direct update **with my entity and set the field value to either null or a value according to the function.

myentity.testfield=myfunction(txtTest.text);

testfield is decimal?. Myfunction returns null if txtTest.Text=="" if not returns Convert.toDecimal(txtTest.Text).

For every pice of code like above Do I have to put a line myentity.testfield=0 in order to set the field null like below.

myentity.testfield=0; myentity.testfield=myfunction(txtTest.text);

I hope I clearly explained my problem Best regards Köksal

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39916
Joined: 17-Aug-2003
# Posted on: 11-May-2007 15:01:37   

I think I know why it happens what you see.

First your original code:



            PersonelEntity personel = new PersonelEntity();
            personel.IsNew = false;
            personel.Serviskod = "1001";
            personel.Fields["Id"].ForcedCurrentValueWrite(323);
            personel.AdSoyad = "ARZU"; // String
            personel.Tckimlikno = null; // Decimal? nullable
            personel.IsBaslangic=null; // DateTime? nullable
            personel.SetNewFieldValue("PersonelEkipId", null); // Decimal?
            personel.Save();

You set IsNew to false immediately after the constructor call. This means that the entity logic itself now thinks it's a fetched entity. HOWEVER, the fields are all null (as it's a new entity!)

So, you then set fields to null: personel.Tckimlikno = null; // Decimal? nullable personel.IsBaslangic=null; // DateTime? nullable

however as these fields are already null, and the db value of the field is also null (as it's not set due to a fetch), this has no effect, the IsChanged flag of the fields stays false.

If you move the IsNew setting to right before the Save call, it will work, because the entity then thinks it's new, so it will change the fields' IsChanged flags to true.

it's a bit of a problem, as the entity doesn't take into account YOU set IsNew to false or a fetch routine, as it can't know who called IsNew's setter.

So the routine:



            PersonelEntity personel = new PersonelEntity();
            personel.Serviskod = "1001";
            personel.Fields["Id"].ForcedCurrentValueWrite(323);
            personel.AdSoyad = "ARZU"; // String
            personel.Tckimlikno = null; // Decimal? nullable
            personel.IsBaslangic=null; // DateTime? nullable
            personel.SetNewFieldValue("PersonelEkipId", null); // Decimal?
            personel.IsNew = false;
            personel.Save();

should work. You can also change the SetNewFieldValue call to PersonelEkipId=null;

A bugfix on 17-mar-2007 (see changelog viewer in the customer area) causes this as you made advantage of a bug simple_smile ->

Fixed an issue with fields which are null and which are set again to null, this would succeed, making the field dirty, which shouldn't be the case.

Fields which are null in a non-new entity which are set to null again shouldn't be marked as changed as they haven't changed at all (they were already null).

Frans Bouma | Lead developer LLBLGen Pro
usus avatar
usus
User
Posts: 27
Joined: 07-Sep-2006
# Posted on: 11-May-2007 15:41:37   

Hi Otis, Yes, I tried your way and It works such that I put the myentity.IsNew=false just before the myentity.Save() line. Thanks a lotsimple_smile Best regards Köksal