What if PK does not exist?

Posts   
 
    
nmarun
User
Posts: 27
Joined: 31-Jul-2006
# Posted on: 01-Sep-2006 18:18:22   

Hi Folks,

I'm having a question on the pre-fetch kinda issue. I'm not sure if this is a bug (I don't think it is).

Alright... I'll get to the point. I have a piece of code here:


ContainerEntity cEntity = new ContainerEntity(200);

Per my understanding, this loads the Container from the table whose primary key is 200. Now if the primary key does not exist in the table, object cEntity gets initialized with default values. Shouldn't the cEntity object be null?

I ask this question because I get an error message (something like 'ContainerID cannot be null') when I do this:


//If PK exists, perform udpate
//else add new row.
//ContainerID 200 does not exist.
ContainerEntity cEntity = new ContainerEntity(200);
if(cEntity!=null)
{
    cEntity.Description = "Description";
    cEntity.Save();
}
else
{
    cEntity = new ContainerEntity();
    cEntity.ContainerID = 200;
    cEntity.Description = "Description";
    cEntity.Save();
}

What's a better way to do this?

Thanks in advance for helping me out. Arun

bclubb
User
Posts: 934
Joined: 12-Feb-2004
# Posted on: 02-Sep-2006 03:02:28   

check for cEntity.IsNew instead. If it is you should set the primary key again since it will not be marked as dirty. cEntity.ContainerID = 200;

nmarun
User
Posts: 27
Joined: 31-Jul-2006
# Posted on: 03-Sep-2006 16:29:20   

thnx bclubb.

Arun