DataPortal_Create Method Failed - newbie help

DataPortal_Create Method Failed - newbie help

Old forum URL: forums.lhotka.net/forums/t/1139.aspx


coss74 posted on Wednesday, September 06, 2006

Hi,

I have created a client class that has an integer as it's primary key that is named id. The class therefore is suppose to check the last id and add one onto this number for a new client. Therefore i created a clientlist class and a clientinfo class ( based upon the ProjectList and ProjectInfo class from the book). However i get a error when trying to create the new client object.

Here is the DataPortal_Create Method from the Client class:

[RunLocal]

private void DataPortal_Create(Criteria criteria)

{

//----------------------------------

// Insert the Lookup Code Here

//----------------------------------

ClientList parent = ClientList.GetClientList();

int max = 0;

foreach (ClientInfo item in parent)

{

if (item.Id > max)

max = item.Id;

}

_id = max + 1;

_idSet = true;

//----------------------------------

//ValidationRules.CheckRules();

}

Can anyone help???

NOTE: i hae attached the full class if you need to see the whole thing..

DeanG replied on Wednesday, September 06, 2006

I don't agree w/ your method of calculating the id. What if there are several instances of your list class... you risk assigning identical values to different instances.

At any rate, you did not specify the error you are getting.

Dean

Bonio replied on Wednesday, September 06, 2006

As previously mentioned, this may not be the right place to increment your ID - have you looked at doing this via a stored procedure instead? As for the problem with the DataPortal_Create, I often have issues with it for one reason or another. I don't know particularly what error you are receiving, but you may want to try overriding it such as:

protected override DataPortal_Create(object Criteria)

Don't know whether this will help, but give it a try (notice that the 'criteria' parameter is now an object)

Bonio

coss74 replied on Wednesday, September 06, 2006

thanks for your responses.

bonio: the protected override changed worked, tnaks.

As to the technique of assigning the key. This method was used in the book for the role.id. Can you explain the reasonns why this is not good especialy if the stored procedure handles the duplicate key problem?

guyroch replied on Wednesday, September 06, 2006

Well, for one thing you are iterating through a list. 

1. First you load the entire list, which will take a whole lot of unecessary resources (memory, cpu cycles, etc) for nothing. 

2. Second, if there is a lot of objects is your list it will take some time to load and to iterate through it. 

3. Third, this is not thread safe.

If your using SQL Server as your database then you should look into having an auto increment integer as your primary key.  This way the database will take care of it all.  The drawback of doing this is that you will not know what the pk value is until you save the object the first time.  If this is an issue, the you might want to look into GUID as a pk.

Cheers

Copyright (c) Marimer LLC