Simple update for an Entity Class

Posts   
 
    
Posts: 27
Joined: 27-Oct-2004
# Posted on: 09-Nov-2004 13:03:57   

Dear Otis,

I am getting an Error when trying to update an Entity. My code section is like this:

// [C#] BusinessesEntity QuickUpdate = new BusinessesEntity(); QuickUpdate.FetchUsingPK(7602); Response.Write( QuickUpdate.BusinessName ) ;

QuickUpdate.BusinessName = " Something " ; QuickUpdate.Save() ;

I want to get a record ( entity ) into the variable QuickUPdate and perfom a quick update on a field ( ex. BusinessName ) and I am using as the code given in the .Chm file right here:

// [C#] -- Note : Code taken from LLBL Gen Pro Help file exemples. CustomerEntity customer = new CustomerEntity("CHOPS"); customer.Phone = "(605)555-4321"; customer.Save();

The page does not display anything when I do a .Write for .BusinessName. And the .Save() method is generating an error.

Please advice and thanks in advance. flushed

wayne avatar
wayne
User
Posts: 611
Joined: 07-Apr-2004
# Posted on: 09-Nov-2004 13:28:20   

Hi LuiguiPepino

Check the entity fetchstatus - the FetchUsingPK might have failed - no record in DB?

Another interesting thing...

If you know the PK value and only want to update then you don't need to load it first. (No Need for FetchUsingPK)

Try the following:

// [C#] BusinessesEntity QuickUpdate = new BusinessesEntity(); QuickUpdate.PKFieldName = 7602; QuickUpdate.IsNew=false; QuickUpdate.BusinessName = " Something " ; QuickUpdate.Save() ;

**Note: **if you want to set an identity primary key column, you'll notice you can't do that because it is marked as read-only. Use the method entityObject.Fields[fieldindex or fieldname].ForcedCurrentValueWrite(value). See the reference manual for details about this method (EntityField.ForcedCurrentValueWrite).

Posts: 27
Joined: 27-Oct-2004
# Posted on: 09-Nov-2004 14:26:09   

Hi Wayne,

I have been taking you advice and it seems not working yet.

I am having i the table the description of the record of CompanyID = 7525 = " Original val ". based on your Help, I have been updating my code as following:

// C# Code BusinessesEntity QuickUpdate = new BusinessesEntity(); QuickUpdate.AccountId = 7525 ; QuickUpdate.IsNew = false; QuickUpdate.Description = " Something new " ; QuickUpdate.Save(); // Using to ensure that the entity is cleaned QuickUpdate = null ;

But it stills not working yet. Could you advice please ?

Best Regards

wayne avatar
wayne
User
Posts: 611
Joined: 07-Apr-2004
# Posted on: 09-Nov-2004 14:39:22   

Hi LuiguiPepino

What is the error message that you are getting?

If there is no error message check the return value of the Save method.

Posts: 27
Joined: 27-Oct-2004
# Posted on: 09-Nov-2004 14:47:35   

Hi Wayne,

I am not getting any Error and I can get into the code and the catch is not called. I am checking the save() resultat as following:

QuickUpdate.IsNew = false; QuickUpdate.TotalHits = 1234 ; bool Elem = QuickUpdate.Save(); Response.Write ( Elem.ToString() ) ; QuickUpdate = null ;

and it returns False. What could be the reason if there's no error that there's no update then. It's a Primary table and not a relation one ( ex. products, customers etc )

wayne avatar
wayne
User
Posts: 611
Joined: 07-Apr-2004
# Posted on: 09-Nov-2004 14:57:53   

I think, Frans might have to get involved here. I am not absolutely sure what can cause a Save method to fail without an exception.

Could it be that this is suppose to be a new record and because we are doing "QuickUpdate.IsNew = false;" it is not doing the save?

Maybe not all the "Not null" fields have been supplied.

Posts: 27
Joined: 27-Oct-2004
# Posted on: 09-Nov-2004 15:07:13   

Hi wayne,

I am back again to clarify a thing:

1.The field I am trying to update allows null and also has the default value = 0 ( int ). 2. Changing the value un Entreprise Manager manually does not generate any error. This means that the field in not mapped to any FK(s)s or contrainsts.

Hope this helps and thanks in advance.

wayne avatar
wayne
User
Posts: 611
Joined: 07-Apr-2004
# Posted on: 09-Nov-2004 15:14:33   

1.The field I am trying to update allows null and also has the default value = 0 ( int ).

What about the other fields in the table? Are there any Not nulls?

Otis!!!, Frans!!!! Help smile

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 09-Nov-2004 15:22:54   

I'll look into this issue shortly (approx 1 hour). simple_smile THanks for helping out Wayne. simple_smile

Frans Bouma | Lead developer LLBLGen Pro
Posts: 27
Joined: 27-Oct-2004
# Posted on: 09-Nov-2004 15:37:21   

wayne wrote:

1.The field I am trying to update allows null and also has the default value = 0 ( int ).

What about the other fields in the table?

LuiguiPepino wrote:

Quoting your question: Are there any Not nulls? 1. I have been setting allw null to all fields of the table execpt the PK. 2. The .Save() method still returning the false value. 3. Code still as following:

BusinessesEntity QuickUpdate = new BusinessesEntity(); QuickUpdate.AccountId = 7525 ; QuickUpdate.IsNew = false; QuickUpdate.TotalHits = 220 ; bool Elem = QuickUpdate.Save(); Response.Write ( Elem.ToString() ) ; QuickUpdate = null ;

Otis!!!, Frans!!!! Help smile

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 09-Nov-2004 17:19:52   

LuiguiPepino wrote:

Dear Otis,

I am getting an Error when trying to update an Entity. My code section is like this:

// [C#] BusinessesEntity QuickUpdate = new BusinessesEntity(); QuickUpdate.FetchUsingPK(7602); Response.Write( QuickUpdate.BusinessName ) ;

QuickUpdate.BusinessName = " Something " ; QuickUpdate.Save() ;

I want to get a record ( entity ) into the variable QuickUPdate and perfom a quick update on a field ( ex. BusinessName ) and I am using as the code given in the .Chm file right here:

// [C#] -- Note : Code taken from LLBL Gen Pro Help file exemples. CustomerEntity customer = new CustomerEntity("CHOPS"); customer.Phone = "(605)555-4321"; customer.Save();

The page does not display anything when I do a .Write for .BusinessName. And the .Save() method is generating an error. Please advice and thanks in advance. flushed

The FetchUsingPK method, does that method return true or false? If false, the entity isnot found and you have a NEW entity then you're trying to save, which probably requires other values to save correctly.

So could you please check what is returned from FetchUsingPK() ?

Frans Bouma | Lead developer LLBLGen Pro
Posts: 27
Joined: 27-Oct-2004
# Posted on: 10-Nov-2004 13:54:19   

The Fetch results: this code returns to me : FALSE

BusinessesEntity QuickUpdate = new BusinessesEntity(); bool Elem = QuickUpdate.FetchUsingPK( 7525 ) ;

What could make that a Fetch is not getting data even that after theses inscrutions I have other data requests using collections and filling a DataGrid that are working fine ?

Best regards.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 10-Nov-2004 19:07:23   

If a fetch returns false, the entity isn't there / it can't find it. Specifying a PK value will result in a SELECT tablefields FROM Table WHERE field = @fieldParam

where @fieldParam is the parameter holding the value you specified. So perhaps you specified the wrong value?

Frans Bouma | Lead developer LLBLGen Pro
Qpeg
User
Posts: 2
Joined: 30-Jun-2005
# Posted on: 01-Jul-2005 09:52:11   

using the adapter approach, what is the best practice to evaluate if a DataAccessAdapter.SaveEntity() succeeds, or if there already exists a row in the DB with the same PK (in case I wish to do an UPDATE)? My business logic does not implement EntityBase-derived classes atm, so I can't use them to check for changes in fields etc. as is suggested in the framework.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 01-Jul-2005 10:24:36   

You could try to execute adapter.GetScalar() to retrieve the PK value from the DB. If it returns null, it's not there and proceed with an insert. (IsNew==true). If it does return a value, it's there, so proceed with an update (IsNew==false)

Frans Bouma | Lead developer LLBLGen Pro
Qpeg
User
Posts: 2
Joined: 30-Jun-2005
# Posted on: 01-Jul-2005 12:33:17   

Thanks for the quick response!