Reputation: 163
close to 3 months into powerbuilder classic 12.5 and sql server 2008 and am working out well.
I am creating a car hire system using car registration_number as the primary key.
i need to capture the car's details and make sure that the registration_number is not available. The code here is not working but that of the capacity works out...
What is wrong with the code? please give another way that works.
if i dont click on the column 'capacity' and click on save, the program goes on and saves the record even when it is an empty field. how to i avoid that.
String car_registration_number
car_registration_number = dw_newvehicle.GetItemString( Row, "registration_number" )
long error_code = 0
string column
column = dwo.name
choose case column
case "registration_number"
if data = car_registration_number THEN
messagebox("validation error", "You cannot available regno")
error_code = 1
end if
case "capacity"
if integer(data) >10 then
messagebox("validation error", "a car's capacity cannot be more than 10...")
error_code = 1
end if
end choose
return error_code
Upvotes: 1
Views: 1857
Reputation: 2706
Instead of GetItemString
at the top, you would need to do one or two finds in the DataWindow for the registration number in data. If the first find
returns 0 you are OK. If it returns the current row, you need to search from row to the last row unless you are on the last row, which you may well be if you are inserting. However, I wouldn't do it that way because it won't work in a multi-user environment. Instead, you should go ahead and insert the row and then if you get a duplicate key error, tell the user the registration number is already in the system. If the user is going to enter a lot of data you could try to SELECT
the registration number from the database when the user enters it, but you still have to be able to handle the duplicate key error.
Upvotes: 2
Reputation: 11465
I don't understand what kind of check you are doing here for registration_number
: you are getting the current value of the column in the DW into car_registration_number
then you are comparing to the value that was modified (the code comes from the itemchanged
event ?). Do you expect to enter a value that is different from the current one ?
Also beware of the GetItemString
if the type of the column is not a text (as it is called ..._number) as PB may crash, return some null value or silently fail (depending on the PB mood of the moment ;). If you get a null value, your if
check will always fail.
If you want to get numerical values, use GetItemNumber
instead.
you can set the capacity
column to be not null
in you database. When saving the DW, you will get an error telling that there is some required value that was not set.
A better idea would be to iterate in the UpdateStart
event on the columns that are member of the table primary key and to check if they are set or not.
To write some dynamic code, you can get at runtime the columns from the DW by describing the datawindow.column.count
then checking the pk members with #1.key
(replace #1 by #2, #3, ...) if the check must be done on that column.
Upvotes: 1