CopyrightIBMCorporation2006TrademarksIBMDB2databaseswithEnterpriseCoreObjectsandASP.
NETPage1of17IBMDB2databaseswithEnterpriseCoreObjectsandASP.
NETHowtouseEnterpriseCoreObjectstocreatemodel-drivenASP.
NETapplicationswithIBMDB2UDBBobSwartJanuary26,2006UseBorlandDelphi2005andtheEnterpriseCoreObjects(ECO)toimportanIBMDB2UniversalDatabase(DB2UDB)databaseandcreateaUMLobjectmodelforit,whichisthebasisforanASP.
NETWebapplication.
FollowinguponthepreviousECOarticle(whichproducedaWinFormsapplication),thisarticleprovidesexamplesthatuseIBMDB2UDBasthebasisfortheEcoSpace,presenttheUMLmodeloftheEmployeeandDepartmentclasseswithassociations,andgenerateASP.
NETWebforms.
UsingEnterpriseCoreObjectsIILasttime,wecreatedanECOWinFormsApplicationinDelphi2005,butthistime,we'llmakeanECOASP.
NETWebApplication.
StartDelphi2005,selectFile>New>Other,andfromtheDelphifor.
NETProjects,double-clicktheECOASP.
NETWebApplicationicon,asshowninFigure1.
developerWorksibm.
com/developerWorks/IBMDB2databaseswithEnterpriseCoreObjectsandASP.
NETPage2of17Figure1.
Delphi2005ObjectRepositoryThisstartstheECOASP.
NETWebApplicationwizard,whereyouonlyhavetospecifythenameoftheproject,likeECO2DB2ASPinthiscase(seeFigure2).
Optionally,youcanspecifythelocation,althoughbydefaultthenameoftheprojectwillbeusedasthenameofthenewASP.
NETvirtualdirectoryinIISaswell.
Figure2.
NewECOASP.
NETWebApplicationClickOKtocreatetheECOASP.
NETWebApplication,whichconsistsofanumberofgeneratedsourcefiles.
Beforeyoucontinue,compileandsavetheproject,closetheprojectandreopenitagain.
ThiswillensurethattheECOcomponentsthatarealreadyplacedinthedifferentformsanddesignersstartwiththecorrectvalue(thetypeoftheEcoSpaceforexample),whichisonlyknownaftercompilingtheproject.
ibm.
com/developerWorks/developerWorksIBMDB2databaseswithEnterpriseCoreObjectsandASP.
NETPage3of17Afteryou'vereopenedtheproject,youhavetoperformthreesteps:designyourUMLmodel,createandspecifythe(empty)IBMDB2UDBdatabasetostoretheUMLmodel,anddesigntheASP.
NETWebuserinterfaceforthemodel.
DesigningtheUMLmodelLikelasttime,IwillfocusontheEMPLOYEEandDEPARTMENTtablesintheIBMDB2UDBSAMPLEdatabase,andrecreatethemasEmployeeandDepartmentclassesintheECOUMLmodel.
ClicktheModelViewtabintheProjectManager,andopentheCoreClassesnode.
Thendouble-clicktheCoreClasseschildnode,whichwillstarttheUMLDesigner.
HereyoucanuseUMLdesignelementstodrawyourdesignfortheEmployeeandDepartmentclasses,likewedidlasttimefortheWinFormsexample(sothiswillbeabitshorterandquickerthaninthepreviousarticle).
TheIBMDB2UDBSAMPLEdatabasecontainsanEMPLOYEEtablewithanEMPNOfieldthatisusedasidentifyingfield,bothforitself(askeyfield,althoughit'snotspecifiedtobethekeyfield),andfortheDEPARTMENTtablewhichusesitfortheMGRNOfield.
TheEMPLOYEEtablealsohasaWORKDEPTfield,whichcanbeusedtomapwiththeDEPTNOfieldintheDEPARTMENTtable.
Andfinally,theDEPARTMENTtablehasaADMRDEPTfieldwhichisacircularlinktoitself(identifyingdepartmentsthatmanageotherdepartments).
ThesekeyandforeignkeyfieldsareuselessinUMLobjectmodels,sowewillnotaddthemtotheclassdesigns,butwillimplementtheimplicitfunctionalitythattheystandforusingexplicitassociations.
DesigningtheEmployeeclassRight-clickthedesignerareaoftheCoreClassesdesignerandusethepop-upmenutoaddanewECOClass.
NameitEmployee.
TheIBMDB2UDBSAMPLEdatabaseusedtheEMPNOfieldasuniquevaluetodeterminetheemployee,whichwasusedbytheDEPARTMENTtableaswell,butyoucanskipthisfield(justliketheWORKDEPTfield),sinceECOitselfwilladduniqueindexfieldstoidentifyclassinstances.
Right-clicktheEmployeeclassandusethepop-upmenutoaddattributes.
AddtheFirstNameattributeoftypeString,theMidInitattributeoftypeWideChar,theLastNameattributeoftypestring,thePhoneNoattributeoftypeString,theHireDateattributeoftypeDateTime,theJobattributeoftypeString,theEdLevelattributeoftypeInteger,theSexattributeoftypeWideChar,theBirthdateattributeoftypeDateTime,andtheSalary,Bonus,andCommattributesoftypeCurrency.
DesigningtheDepartmentclassRight-clickonthedesignerareaandaddanewECOClasscalledDepartment.
YoucanskiptheDEPTNOfield(justliketheEMPNOfield),sostartwiththeDeptNamefieldoftypeString.
TheMGRNOfieldisactuallyalinkbacktotheEMPLOYEEtable,andwe'lldealwiththatinaminutewhenaddingassociated,soskipthatfieldaswell.
SamewiththeADMRDEPTfield,whichdeveloperWorksibm.
com/developerWorks/IBMDB2databaseswithEnterpriseCoreObjectsandASP.
NETPage4of17isactuallyalinkfromtheEMPLOYEEtablebacktoitself.
TheonlyfieldyouneedtoaddistheLOCATIONfieldoftypeString.
AddingAssociationRelationsWiththeEmployeeandDepartmentclassesdesigned,youmustnowdesigntheassociationsbetweenthem(whichwereimplicitlydefinedusingtheEMPNOandWORKDEPTfieldsoftheEMPLOYEEtable,andtheDEPTNO,MGRNO,andADMRDEPTfieldsoftheDEPARTMENTtable).
TheWORKDEPTfieldoftheEMPLOYEEtableobviouslypointedtotheDEPARTMENTtable,usingtheDEPTNOfieldtolinkto.
ThiscanbereplacedbyanassociationfromEmployeetoDepartment.
IntheToolPalette,selecttheAssociationelement,thenclickontheEmployeeclass,holddownthemousebutton,anddragtotheDepartmentclass.
ThiswilldrawanassociationbetweenEmployeeandDepartment.
UsetheObjectInspectortosetthenameofthenewassociationtoEmployment.
Anhastwoends:oneattheEmployeesideandoneattheDepartmentside,bothsetto0.
.
1multiplicitybydefault.
BasedonthefactthattheoriginalWORKDEPTfieldintheEMPLOYEEtablepointedtotheDEPTNOfieldintheDEPARTMENTtable,youcanassumethatanEmployeecanlinktoonlyoneDepartment(ornone,sinceWORKDEPTisanullablefield),sothemultiplicitycanremain0.
.
1.
FortheDepartment,itmeansthatzeroormoreEmployeescanbelinkedtoit.
SoattheEmployeeside,themultiplicitymustchangefrom0.
.
1to0.
.
*.
NowyoumustaddthelinkbackfromtheDepartmentclasstotheEmployeeclass,implementingtheMGRNOfield.
ThistimethemultiplicitymustbesetsothataDepartmentismanagedby0or1Employees(MGRNOisnullable),andanEmployeecaninfactmanagezeroofmoreDepartments.
Onlyonerelationshipleft:replacingtheADMRDEPTfield,whichisbetweentheDepartmentclassanditself(oneDepartmentismanagedbyanotherDepartment,whichinturncanmanagemultipleDepartments).
ThisisdonebyanassociationbetweenDepartmentanditself.
ThefinalUMLmodelcanbeseeninFigure3.
Figure3.
Employee-DepartmentUMLmodelAlthoughyounolongerhavetheEMPNOandWORKDEPTfields(intheEMPLOYEEtable)northeDEPTNO,MGRNO,andADMRDEPTfields(intheDEPARTMENTtable),thenewUMLmodelcanstorethesameinformationthroughtheexplicitassociationrelations.
ibm.
com/developerWorks/developerWorksIBMDB2databaseswithEnterpriseCoreObjectsandASP.
NETPage5of17CreatinganemptyIBMDB2ECODatabaseNormally,youwouldneedanemptydatabaseasacontainertostoretheEcoSpace.
Delphi2005comeswithaCD-ROMthatcontainstheIBMDB2UDBDeveloperedition.
YoucanusetheControlCenter(foundintheIBMDB2GeneralAdministratorTools),andintheControlCenterusethetreeviewtobrowseyoursystem,DB2instancesanddatabases.
Createanewdatabaseusingthewizard,whichwillgiveyoua7-stepwizardwhereyouonlyhavetospecifythenameofthenewemptydatabase(somethinglikeECO2ASP).
TheSQLDDLwhichisusedtocreatethedatabaseisasfollows:CREATEDATABASEECO2ONC:USINGCODESETIBM-1252TERRITORYUSCOLLATEUSINGSYSTEM;AftertheemptyIBMDB2UDBECO2databasehasbeencreated,youneedtogototheDataExplorerandaddanewDB2ConnectiontotheECO2database.
Makesuretospecifytherightdatabasename(ECO2)andavalidusernameandpasswordtoconnecttoit.
ConfiguringtheEcoPersistenceMapperProviderWiththeUMLmodelreadyandtheemptyIBMDB2UDBECO2databasecreated,it'stimetomakethemodelpersistent.
Lasttime,inasingle-userWinFormapplication,youcouldplacethePersistenceMapperBdpcomponentintheEcoSpaceunit.
However,thistimeyou'rebuildingamulti-userASP.
NETWebFormsapplication,whichmeanstheapplicationmustbemulti-useraware,sharingthemodelindifferentthreads.
ThisishandledbyaspecialEcoPersistenceMapperProviderunit.
IntheProjectManager,double-clicktheEcoPersistenceMapperProvider.
pasnode,andthenclickthedesigntabsoyoucanplacetherequiredcomponents.
First,placeaPersistenceMapperBdpcomponentontheEcoPersistenceMapperProviderdesignarea,andclickontheDB2SetuplinkatthebottomoftheObjectInspector.
ThendragtheBDPConnectioncomponentfromtheDataExplorer(theonepointingtotheemptyIBMDB2UDBECO2database)andplaceitnexttothePersistenceMapperBdpcomponent.
Ifyoudoitinthisorder,thenallthenecessarypropertieswillbelinkedautomatically(thatis,thePersistenceMapperpropertyoftheEcoPersistemceMapperProviderwillbepointingtothePersistenceMapperBdpcomponent,andtheConnectionpropertyofthePersistenceMapperBdpwillbepointingtotheBdpConnectioncomponent).
There'sonepropertyyoumustsetmanually,andthat'stheSyncActivepropertyofthePersistenceMapperBdpcomponent.
SetthistoTrue.
YoumustnowgeneratetherequiredtablesfortheEcoSpaceandaddthemtotheemptyIBMDB2UDBECO2database.
Todothis,clicktheleftmostbuttonatthebottomoftheEcoPersistenceMapperProviderdesigner,whichsaysGenerateSchema.
Afteryouclickthebutton,theapplicationisrecompiled,andadialogboxdisplayswhichtablescan(optionally)bedeleted,whichtablesmustbedroppedandrecreated,andwhichnewtableswillbecreatedinthedatabase,asshowninFigure4.
developerWorksibm.
com/developerWorks/IBMDB2databaseswithEnterpriseCoreObjectsandASP.
NETPage6of17Figure4.
GenerateSchemaAfteryouclicktheOKbutton,thesevennewtablesareaddedtotheIBMDB2UDBECO2database.
ApartfromtheEMPLOYEEandDEPARTMENTtables,yougetfiveECOspecifictablesthatareneededfortheEcoSpacemanagementitself.
Thereisonlyonestepleft:buildingauserinterfaceontopoftheEcoSpace,thistimeusingASP.
NETWebFormsinsteadof.
NETWinForms.
BuildingtheASP.
NETuserinterfaceTheASP.
NETuserinterfaceisbuiltintheWebForm1.
aspxfile.
ToopenthisunitintheHMTLdesigner,double-clicktheWebForm1.
aspxnodeintheProjectManager.
WheretheWinForm(fromlasttime)hadfivecomponentsalreadypresentinthenon-visualcomponentareaoftheWinFormsDesigner,theASP.
NETWebFormsDesigneronlyshowsonecomponent:therhRootReferenceHandletotherootoftheEcoSpace(andallobjectinstancesinsideit).
YouneedtoaddanadditionalcomponentfromtheEnterpriseCoreObjectscategory,namelyanExpressionHandler.
AnExpressionHandleislinkedtoeitheraReferenceHandle(likerhRoot)oranotherExpressionHandle.
YoucanuseittoevaluateanOCL(ObjectConstraintLanguage)expression.
Inthiscase,theExpressionHandlewillbecalledehEmployeeandconnectedwithitsRootHandlepropertytorhRoot.
IntheExpressionproperty,youcanusetheOCLExpressionEditoragaintobuildtheEmployee.
allInstancesexpression,justlikelasttime.
What'sdifferentthistimeisthatyoualsoneedtosettheAddExternalIdpropertyoftheehEmployeeExpressionHandlertoTrue(you'llusetheExternalIdfieldinaminute).
Now,placeaDataGridcomponentontheWebForm,andassignitsDataSourcetotheehEmployeeExpressionHandler.
AlsosettheDataKeyFieldpropertyoftheDataGridcomponenttoExternalId(thisisimportant),soitwillusetheinternalECO"key"asanIDforexternaluse(sowecanidentifywhichitemintheDataGridisthecurrentone,forexample).
ibm.
com/developerWorks/developerWorksIBMDB2databaseswithEnterpriseCoreObjectsandASP.
NETPage7of17TheresultisthattheDataGridwillshowthemetadata--thenamesandtypesoftheattributesoftheEmployeeclass,includingtheExternalIdfield(aninternalfieldaddedbyECOthatyoudon'treallyneedtoshow).
InordertomakesuretheExternalIdfieldisnotshown,clickthePropertyBuilderverbatthebottomoftheObjectInspector.
ThiswillstarttheDataGridPropertiesdialogbox.
Here,youcanspecifywhichfieldsyouwanttosee,andwhichtohide.
Theleftsideofthisdialogboxshowsfivecategories.
Youneedthesecondone,forthecolumns.
Inthispage,youcanselectthecolumnsthatyouwanttosee(movethemfromthelistboxonthelefttotheright),forexampletheFirstName,MidInit,LastName,andPhoneNobutnoneoftheotherfields(seeFigure5).
Figure5.
DataGridPropertiesThere'sonemorethingyouneedtodobeforeclosingtheDataGridPropertieseditor:atthetopoftheColumnspageisthecheckboxfortheCreatecolumnsautomaticallyatruntimeoption.
Ifyoudon'tuncheckthatoption,thenyougetyourselectedcolumnsaswellasallnormalcolumns(someofthemtwice).
Souncheckthatoptiontomakesureyouonlygetthecolumnsyou'veselected.
That'safeatureofMicrosoft'sASP.
NETDataGrid,bytheway,notspecificallyincombinationwithECOorDB2.
Afteryou'veuncheckedthisoption,youcanclickOKtoclosetheDataGridPropertiesdialogbox.
developerWorksibm.
com/developerWorks/IBMDB2databaseswithEnterpriseCoreObjectsandASP.
NETPage8of17Now,atthebottomoftheObjectInspectorthereisanotherverbcalledAutoFormatwhichyoucanusetogivetheDataGridaquicklook.
IfyouclicktheAutoFormatverb,youcanselectalayoutfortheDataGrid,likeColorFul3(youcanseetheresultinoneofthenextscreenshots).
ViewingthefirstresultinthebrowserYoucanalmostviewthefirstresultinabrowser.
There'sonethingmissing:theabilitytoaddnewEmployeeobjectstotheEcoSpace.
Forthis,youneedaButtoncontrol,placedsomewherenexttotheDataGrid,withtheTextpropertysettoNewEmployeeandthefollowingimplementationforitsClickeventhandler:Listing1.
NewEmployeeObjectprocedureTWebForm1.
Button1_Click(sender:System.
Object;e:System.
EventArgs);varNewEmployee:Employee;beginNewEmployee:=Employee.
Create(EcoSpace);NewEmployee.
Firstname:='Robert';NewEmployee.
MidInit:='E';NewEmployee.
Lastname:='Swart';NewEmployee.
PhoneNo:='42';UpdateDatabase;DataBind;end;ThisexamplecreatesanewinstanceofanEmployeewhilepassingtheEcoSpaceasanargumenttotheconstructor,inordertomakesurethattheEmployeeislivinginourEcoSpace.
ItalsoassignssomeinitialvaluestotheFirstName,MidInit,LastName,andPhoneNoattributes,althoughit'sgenerallyOKtoleavetheseemptyfornewobjects.
Youcaneditthemlaterandenterthecorrectvalues.
ThecalltoUpdateDatabaseensuresthattheunderlyingdatabaseisupdatedwiththecontentsofthemodifiedEcoSpace.
ThecalltoDataBindmakessurethattheASP.
NETcontrolsareupdated(specificallythattheDataGriddisplaysthenewresultoftheehEmployeeExpressionHandler).
Ifyoucompileandruntheapplication,however,you'llquicklynoticethatyoucanenternewEmployees,butthereisnowaytomodifytheminanyway,ortodeletethemforthatmatter.
AndalthoughIamahardworker,itdoesn'tseemverysensibletohaveanEcoSpacewithRobertE.
SwartastheonlyEmployeeavailable.
WorkingwiththeECODataintheBrowserSo,youneedtoaddtheabilitytoeditthecontentsoftheDataGrid.
SelecttheDataGrid,andclickthePropertyBuilderverbagaintostarttheDataGridPropertiesdialogbox(likeinFigure5).
GototheColumnspageagain.
Apartfromregularcolumns,youalsoneedtohaveButtons.
IntheAvailablecolumnslistbox,scrolldowntotheButtonColumn.
SelecttheEdit,Update,Cancel,andDeletebuttons,andclickonthearrowtoaddthemtotheSelectedcolumnslistbox.
Youcancustomizethebuttons,forexamplechangingtheirtypetoPushButtoninsteadofLinkButton.
Whenyou'refinished,closethedialogbox.
ibm.
com/developerWorks/developerWorksIBMDB2databaseswithEnterpriseCoreObjectsandASP.
NETPage9of17NowyouneedtoassigntheeventsthattheDataGridfiresfortheEdit,Update,Cancel,andDeletecommands.
GototheEventstaboftheObjectInspector,andopenupthedrop-downcomboboxfortheEditCommandeventhandler.
Itdisplaysanumberofoptions(whichweregeneratedbytheECOwizardswhenyoucreatedtheASP.
NETECOproject).
FortheEditCommand,connecttheeventhandlertotheDataGrid_EditCommandmethod.
DothesamethingfortheUpdateCommand(toDataGrid_UpdateCommand),theCancelCommand,andtheDeleteCommand.
YoucannowruntheASP.
NETECO2applicationandusethebuttonnexttotheDataGridtocreatenewEmployeeobjects,andthebuttonsintheDataGridtoedit,update,cancel,ordeletetheEmployeeobjectsagain(asshowninFigure6).
Figure6.
ECOEmployeesinASP.
NETThereisoneproblem:theDataGriddoesn'tcontainallattributesoftheEmployeeclass(otherwisetheDataGridwouldbetoowideforthescreen).
Sowecannoteditallfields.
Cometothinkofit,wouldn'titbenicertobeabletoselectanEmployeeintheDataGridandclickonaButtonorLinktogotoadetailpagewhereyoucaneditallfieldsAbitliketheAutoFormsintheWinFormsversionoftheECOproject.
Andwhileyou'reatit,youcouldaddtheabilitytoaddandlinktoDepartmentobjectsaswell.
CreatinghyperlinkdetailslinksGobacktoDelphi2005,gototheHTMLdesigner,selecttheDataGrid,clickthePropertyBuilderlinkagain,andaddanothernewcolulmn:thistimeaHyperLinkcolumn.
SettheTextpropertytoDetails,theURLfieldtotheExternalId,andtheURLformatpropertyto:EmployeeDetails.
aspxRootId={0}Thiswillmakesurethatforagivenrow,theDetailslinkwillpointtotheEmployeeDetails.
aspxpage(whichyouwillneedtocreate),passingasRootIdthevalueoftheExternalIdinternalkeyfield.
Neat,huhdeveloperWorksibm.
com/developerWorks/IBMDB2databaseswithEnterpriseCoreObjectsandASP.
NETPage10of17AddinganemployeedetailspageNow,selectFile>New-Other,gototheASP.
NETFilescategoryintheObjectRepository,andaddanECOASP.
NETPagetotheproject.
Makesureyoudon'taddaregularASP.
NETPage,butanECOASP.
NETPage.
OnlythelatterhasthecodetosupportECO(hookingintotheDataGrid'sevents,andretrievingtheRootIdvalueasstartingpointfortherhRootReferenceHandle,pointingtotherightEmployeeclassinstance).
NowyouneedtosavethenewECOASP.
NETPageinEmployeeDetails.
aspx(sincethat'sthepagenamethatyouusedintheURLformatpropertyoftheDetailsHyperlink).
InthecodeeditorfortheEmployeeDetails.
pasfile,addtheCoreClassesUnittotheusesclause,sothatthenewECOASP.
NETPageknowsabouttheclasstypesintheEcoSpace.
Then,gototheDesignviewagain,andclickontherhRootandsettheEcoSpaceTypetoECO2DB2ASPEcoSpace.
TECO2DB2ASPEcoSpace(justopenupthedrop-downcomboboxfortheEcoSpaceTypepropertyandselecttheonlychoiceavailable).
SettheStaticValueTypetoEmployee(thisyouwillhavetoentermanually),toindicatethattheelementthattherhRootpointstoisanEmployee,sothatyougetvaliddesign-timesupport.
Now,placeanExpressionHandlercomponentnexttotheRhRoot,callitehEmployee,setitsRootHandlepropertytorhRoot,theAddExternalIdpropertytoTrue,andtheExpressionpropertytoself(whichwillbethecurrentEmployeeclassinstancethatwasselectedinthepreviousASP.
NETpage).
Now,insteadofaddingaDataGrid,whichwillagainbetoowideifyouaddallcolumnstoit,youshouldinsteadaddindividualASP.
NETTextBoxcontrolstobindtotheattributesoftheEmployee.
PlaceaTextBoxcontrolontheECOASP.
NETWebForm,andclickontheellipsis(.
.
.
)fortheDataBindingproperty.
ThiswillopenadialogboxthatyoucanusetoconnecttheTextpropertytotheehEmployee[0]'sFirstNamefield,asshowninFigure7.
ibm.
com/developerWorks/developerWorksIBMDB2databaseswithEnterpriseCoreObjectsandASP.
NETPage11of17Figure7.
SimpleDataBindingtoECOExpressionHandleYoucandothisforallindividualfields,andendupwithalongdetailpageshowingtheTextBoxesoneundereachother.
Inordertogetanicelayout,it'srecommendedtouseanHTMLtablethatyoucaninsertusingTable>Insert-Table.
AnHTMLtableof12rowswith2columnsisenoughfortheFirstName,MidInit,LastName,PhoneNo,HireDate,Job,EdLevel,Sex,Birthdate,Salary,Bonus,andCommattributes.
IthelpsifyougivetheTextBoxcontrolssensiblenamesliketbFirstName,tbMidInit,andsoon.
Afteryou'veaddedthe12ASP.
NETTextBoxcontrolsintherightcolumnoftheHTMLtable,youcanenterthelabelsintheleftcolumn,andaButtonundertheHTMLtablewiththecaptionSaveandReturnonit,whichyouneedtoimplementtosavethecontentsoftheTextBoxcontrolsbackinthecurrentEmployee,andreturntotheoriginalASP.
NETpage(whichisprobablystillcalledWebForm1.
aspx).
ThecodefortheButtonClickeventisasfollows,notetheuseofthesensiblenamesfortheTextBoxcontrols:Listing2.
NewEmployeeObjectprocedureTWebForm2.
Button1_Click(sender:System.
Object;e:System.
EventArgs);varEmp:Employee;begintryEmp:=(rhRoot.
Element.
AsObjectasEmployee);Emp.
FirstName:=tbFirstName.
Text;Emp.
MidInit:=tbMidInit.
Text[1];Emp.
LastName:=tbLastName.
Text;Emp.
PhoneNo:=tbPhoneNo.
Text;Emp.
HireDate:=Convert.
ToDateTime(tbHireDate.
Text);Emp.
Job:=tbJob.
Text;Emp.
EdLevel:=Convert.
ToInt32(tbEdLevel.
Text);developerWorksibm.
com/developerWorks/IBMDB2databaseswithEnterpriseCoreObjectsandASP.
NETPage12of17Emp.
Sex:=tbSex.
Text[1];Emp.
Birthdate:=Convert.
ToDateTime(tbBirthdate.
Text);Emp.
Salary:=Convert.
ToDouble(tbSalary.
Text);Emp.
Bonus:=Convert.
ToDouble(tbBonus.
Text);Emp.
Comm:=Convert.
ToDouble(tbComm.
Text);UpdateDatabase;DataBind;Response.
Redirect('WebForm1.
aspx');exceptonE:ExceptiondoResponse.
Write(E.
Message);end;end;Atdesigntime,theECOASP.
NETDetailPagefortheEmployeecanbeseeninFigure8.
Figure8.
EmployeeDetailPageSaveAll,Compile,andRuntheapplication.
YoucannowaddnewEmployeeobjects,andclickontheDetailshyperlinktogototheEmployeeDetailspagewhereyoucaneditthedetails,andclickonSaveandReturntocontinue.
Obviously,ifyoudon'twanttosavethechanges,youneedabuttonthatonlydoestheResponse.
RedirectinitsClickevent(Ileavethataseasyexerciseforthereader).
AddingDepartmentsintothemixTimetothrowtheDepartmentclassintothemix.
Ontheoverviewpage,wherewehavetheDataGridshowingthefouroftheEmployeefieldswithButtonsandDetailhyperlinks,plusanAddButtontoaddnewEmployeeobjects,youcandothesamethingforDepartments.
ibm.
com/developerWorks/developerWorksIBMDB2databaseswithEnterpriseCoreObjectsandASP.
NETPage13of17PlaceanotherExpressionHandle,callitehDepartment,pointitsRootHandlepropertytorhRoot,settheAddExternalIdpropertytoTrue,andasExpressionpropertywriteDepartment.
allInstances.
PlaceaDataGridontheASP.
NETWebForm,connectitsDataSourcepropertytoehDepartment,settheDataKeyFieldtoExternalId,andmakesureonlytheDeptNameandLocationpropertiesarevisible.
Thistime,thereareonlytwocolumns,sowecanEdit,Update,Cancel,andDeletefromtheDataGridwithouttheneedforadetailpage.
ButweneedaspecialAddButton,tocreatenewDepartmentinstances,implementedasfollows:Listing3.
NewDepartmentObjectprocedureTWebForm1.
Button2_Click(sender:System.
Object;e:System.
EventArgs);varNewDept:Department;beginNewDept:=Department.
Create(EcoSpace);NewDept.
DeptName:='DeptName';NewDept.
Location:='Location';UpdateDatabase;DataBind;end;NotethattheDeptNameandLocationvaluesareonlydefaultvaluesthatneedtobechangedusingtheEditandUpdatebuttonsintheDataGrid,asyoucanseeinFigure9.
Figure9.
EmployeeandDepartmentsatruntimedeveloperWorksibm.
com/developerWorks/IBMDB2databaseswithEnterpriseCoreObjectsandASP.
NETPage14of17ShowingrelateddepartmentsThere'sonefinalthinglefttodo:oncealistofvalidDepartmentsexists,youcanassigntheEmployeetooneoftheexistingDepartments(thistakescareoftheEmployeesassociationbetweenzeroormoreEmployeesandoneDepartment--seeFigure3).
Forthis,youneedtogobacktotheEmployeeDetails.
aspxpage.
BelowtheTextBoxcontrolsfortheselectedEmployee,youwanttoshowaDataGridwithallavailableDepartments,allowingyoutoassigntheEmployeetooneoftheseDepartments.
Firstofall,showthecurrentDepartmentassignedtotheEmployeeusingtheWorksAtassociation.
Forthese,youcanuseanewExpressionHandlecalledEmployeeDepartment,withitsRootHandlepointedtoehEmployee(theExpressionHandle)andasExpressionpropertyself.
WorksAt.
ThiswillensurethattheDeptNamebelongingtotheDepartmentassociatedviatheWorksAtlinkcanbeobtainedusingtheehEmployeeDepartmentExpressionHandle.
Notethatitcanhavezerooronevalues,soyoucannotusesimpledatabindingtoshowtheresult.
Instead,youcanwriteonelineofcodeinthePage_Loadtodeterminethevalue,andthenprintit:Listing4.
SelectRightEmployeeprocedureTWebForm2.
Page_Load(sender:System.
Object;e:System.
EventArgs);varId:string;beginEcoSpace.
Active:=True;Id:=Request.
Params['RootId'];ifAssigned(Id)and(Id'')thenrhRoot.
SetElement(ObjectForId(Id));ifAssigned(ehEmployeeDepartment.
Element)thenResponse.
Write('Worksfor:'+(ehEmployeeDepartment.
Element.
AsObjectasDepartment).
DeptName);ifnotIsPostBackthenDataBind;end;LinkingEmployeeswithDepartmentsThelaststepshowstheassociatedDepartment,butdoesn'tactuallymaketheassociation.
Forthat,weneedasecondReferenceHandle,calledrhDepartments.
SetitsEcoSpaceTypepropertytoECO2DB2ASPEcoSpace.
TECO2DB2ASPEcoSpace.
ThenplaceathirdExpressionHandlecomponent,setrhDepartmentasitsroot,theAddExternalIdpropertytoTrue,andsetitsExpressionpropertytoDepartment.
allInstancesinordertolistallavailableDepartmentsintheEcoSpace.
Then,placeaDataGridtodisplaytheDepartmentclasses,setitsDataSourcetoehDepartments,DataKeyFieldtoExternalId,andusetheDataGridPropertyBuildertomakesureonlytheDeptNameandLocationfieldsareshown.
Then,addaSelectbuttonwithTextpropertysettoWorkHere.
TheimplementationoftheSelectedIndexChangeeventisasfollows:ibm.
com/developerWorks/developerWorksIBMDB2databaseswithEnterpriseCoreObjectsandASP.
NETPage15of17Listing5.
LinkEmployeetoDepartmentprocedureTWebForm2.
DataGrid1_SelectedIndexChanged(sender:System.
Object;e:System.
EventArgs);varIDep:IObject;Dep:Department;beginId:=DataGrid1.
DataKeys[DataGrid1.
SelectedIndex].
ToString;IDep:=ObjectForId(Id);Dep:=(IDep.
AsObjectasDepartment);(rhRoot.
Element.
AsObjectasEmployee).
WorksAt:=Dep;UpdateDatabase;DataBind;end;Forallthistowork,youmustensurethatthenewrhDepartmentroothandleisassignedtotheEcoSpace,whichcanbedoneinthePage_Loadeventagain.
ThefinalEmployeeDetailpagecanbeseeninFigure10(whereI'vealreadyassignedRobertE.
SwarttoworkintheTrainingDepartment).
developerWorksibm.
com/developerWorks/IBMDB2databaseswithEnterpriseCoreObjectsandASP.
NETPage16of17Figure10.
EmployeedetailandDepartmentpageTheotherassociations--fromDepartmenttoManagerandfromDepartmenttoDepartment--areleftasexerciseforthereader.
Withtheexamplesshowninthisarticle,I'msureyoucandesignandimplementtherestofthisprojectforyourself.
SummaryInthisarticle,you'veusedDelphi2005andEnterpriseCoreObjectsII,buildinganASP.
NETWebapplication,whilestillusingtheIBMDB2UDBdatabaseasbasisfortheEcoSpace,presentingtheUMLmodelofthedataandgeneratingASP.
NETWebforms.
I'veshownhowtocreateclassinstances,howtoedit,cancel,update,anddeleteitemsintheDataGrid,howtojumptodetailpages(foraselecteditem),andevenhowtobuildassociationrelationswithASP.
NETWebpages.
ibm.
com/developerWorks/developerWorksIBMDB2databaseswithEnterpriseCoreObjectsandASP.
NETPage17of17DownloadableresourcesDescriptionNameSizeSamplesourcecodeforthisprojectsource.
zip(HTTP|FTP)35KBCopyrightIBMCorporation2006(www.
ibm.
com/legal/copytrade.
shtml)Trademarks(www.
ibm.
com/developerworks/ibm/trademarks/)
licloud官方消息:当前对香港机房的接近100台物理机(香港服务器)进行打折处理,30Mbps带宽,低至不到40美元/月,速度快,性价比高,跑绝大多数项目都是绰绰有余了。该款香港服务器自带启动、关闭、一键重装功能,正常工作日内30~60分钟交货(不包括非工作日)。 官方网站:https://licloud.io 特价香港物理服务器 CPU:e3-1230v2(4核心、8线程、3.3GH...
易探云怎么样?易探云隶属于纯乐电商旗下网络服务品牌,香港NTT Communications合作伙伴,YiTanCloud Limited旗下合作云计算品牌,数十年云计算行业经验。发展至今,我们已凝聚起港内领先的开发和运维团队,积累起4年市场服务经验,提供电话热线/在线咨询/服务单系统等多种沟通渠道,7*24不间断服务,3分钟快速响应。目前,易探云提供香港大带宽20Mbps、16G DDR3内存、...
Hostio是一家成立于2006年的国外主机商,提供基于KVM架构的VPS主机,AMD EPYC CPU,NVMe硬盘,1-10Gbps带宽,最低月付5欧元起。商家采用自己的网络AS208258,宿主机采用2 x AMD Epyc 7452 32C/64T 2.3Ghz CPU,16*32GB内存,4个Samsung PM983 NVMe SSD,提供IPv4+IPv6。下面列出几款主机配置信息。...
selectedindexchanged为你推荐
免费开通黄钻怎样能免费开通黄钻会员快速美白好方法快速美白的好点子!?(不是晒黑的)依赖注入什么是依赖注入依赖注入的作用是什么意思网站运营网络运营具体做什么呢今日热点怎么删除今日热点自动弹出怎么卸载或屏蔽网管工具网吧管理软件都有哪些?宽带接入服务器宽带接入服务器的五大功能是什么?网络虚拟机VMware虚拟机三种网络模式的区别有哪些?微信电话本怎么用微信电话本如何使用?php购物车php做购物车需要用到哪些技术,这些技术怎么实现
香港服务器租用99idc 域名服务器上存放着internet主机的 中文域名交易中心 域名商 awardspace 搬瓦工官网 灵动鬼影 ca4249 蜗牛魔方 架设服务器 softbank邮箱 秒杀汇 域名和空间 台湾谷歌 全能空间 成都主机托管 免费网络空间 贵州电信 japanese50m咸熟 forwarder 更多