Primary Key Retrieval After Save

Posts   
 
    
Posts: 8
Joined: 28-Jul-2005
# Posted on: 06-Aug-2005 06:37:11   

Hello,

I used LLBLGen Pro to generate the code for my MYSQL database. I am using Asp.NET, C#, and CoreLab.MySQL.

Upon a EntityClass.Save function, I would like to grab the Primary Key generated by the database. Is there a way to do this?

 TutorApp.EntityClasses.SubjectsEntity theSubject = new TutorApp.EntityClasses.SubjectsEntity();
                theSubject.Sublevel="Elementary";
                theSubject.Subname="Math";
                
                theSubject.Save();

                //write the primary key 
//This causes the app to bomb, no primary key sent back
                Response.Write(theSubject.Subid.ToString());
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 06-Aug-2005 11:19:15   

It should read the ID back for you. Is the field defined as an identity field ? (in the designer, so open the entity editor for the particular entity and check if the 'Is identity' value is true for the PK field)

Frans Bouma | Lead developer LLBLGen Pro
Posts: 8
Joined: 28-Jul-2005
# Posted on: 06-Aug-2005 21:34:56   

I've checked the editor, and yes, 'subid' is checked as the primary key and as the identity field. Any suggestions? Thanks

Posts: 8
Joined: 28-Jul-2005
# Posted on: 06-Aug-2005 22:43:05   

Could it be because I am using Self Servicing, 2 class scenario rather than Adapter scenario when I generate?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 07-Aug-2005 11:40:07   

rebornstudio wrote:

Could it be because I am using Self Servicing, 2 class scenario rather than Adapter scenario when I generate?

No that should be ok.

The save action marks the entity 'outofsync', and the first read from a property refetches the entity.

Now, my questions: 1) what does theSubject.Save() return: true or false? 2) what exactly do you get when this line is called: Response.Write(theSubject.Subid.ToString()); an exception? If so, which one and more importantly: the stacktrace? thanks.

Frans Bouma | Lead developer LLBLGen Pro
Posts: 8
Joined: 28-Jul-2005
# Posted on: 07-Aug-2005 12:12:29   

The function returns True and the entry does show up when I check the database. Here is the stacktrace:

 Specified cast is not valid. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.InvalidCastException: Specified cast is not valid.

Source Error: 


Line 485:
Line 486:               //write the primary key
Line 487:               Response.Write(theSubject.Subid.ToString());
Line 488:
Line 489:               


Source File: c:\inetpub\tutorsclubv1\uc_tutorapp.ascx.cs    Line: 487 

Stack Trace: 


[InvalidCastException: Specified cast is not valid.]
   TutorApp.EntityClasses.SubjectsEntityBase.get_Subid()
   tutorsclub.UC_TutorApp.S1Proceed_btn_Click(Object sender, EventArgs e) in c:\inetpub\tutorsclubv1\uc_tutorapp.ascx.cs:487
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +108
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +57
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +18
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
   System.Web.UI.Page.ProcessRequestMain() +1292



Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 07-Aug-2005 16:49:57   

The property should be of type System.Int64, so the cast won't fail, as the value coming back from the db is also an Int64. Very strange. Could you check for me if the property is actual of type Int64? You're using CoreLab's MySql v3.0 I pressume?

Frans Bouma | Lead developer LLBLGen Pro
Posts: 8
Joined: 28-Jul-2005
# Posted on: 08-Aug-2005 02:05:07   

In the Database, 'subid' is a tinyint. Could that be the problem?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 08-Aug-2005 08:29:41   

rebornstudio wrote:

In the Database, 'subid' is a tinyint. Could that be the problem?

Please define it as an int or bigint, bigint prefered, and refresh the catalog. TinyInt is too small for identity values, as these are 64 bits. Nevertheless, the type of the property / field in LLBLGen pro should be int64 and it should work... disappointed

The value in theSubject.Fields["SubId"].CurrentValue should be an int64 and theSubject.SubId should be of type Int64

Frans Bouma | Lead developer LLBLGen Pro
Posts: 8
Joined: 28-Jul-2005
# Posted on: 08-Aug-2005 23:16:31   

I've defines it as regular Int instead of TinyInt. It worked perfectly. Thanks

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 09-Aug-2005 09:36:16   

Ok, glad it's solved. simple_smile

Frans Bouma | Lead developer LLBLGen Pro