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/)
水墨云怎么样?本站黑名单idc,有被删除账号风险,建议转出及数据备份!水墨云ink cloud Service是成立于2017年的商家,自2020起开始从事香港、日本、韩国、美国等地区CN2 GIA线路的虚拟服务器租赁,同时还有台湾、国内nat vps相关业务,也有iplc专线产品,相对来说主打的是大带宽服务器产品。注意:本站黑名单IDC,有被删除账号风险,请尽量避免,如果已经购买建议转出及数据备...
昔日数据怎么样?昔日数据是一个来自国内服务器销售商,成立于2020年底,主要销售国内海外云服务器,目前有国内湖北十堰云服务器和香港hkbn云服务器 采用KVM虚拟化技术构架,湖北十堰机房10M带宽月付19元起;香港HKBN,月付12元起; 此次夏日活动全部首月5折促销,有需要的可以关注一下。点击进入:昔日数据官方网站地址昔日数据优惠码:优惠码: XR2021 全场通用(活动持续半个月 2021/7...
imidc怎么样?imidc彩虹数据或彩虹网络现在促销旗下日本多IP站群独立服务器,原价159美元的机器现在只需要88美元,而且给13个独立IPv4,30Mbps直连带宽,不限制月流量!IMIDC又名为彩虹数据,rainbow cloud,香港本土运营商,全线产品都是商家自营的,自有IP网络资源等,提供的产品包括VPS主机、独立服务器、站群独立服务器等,数据中心区域包括香港、日本、台湾、美国和南非...
selectedindexchanged为你推荐
解压程序软件怎么解压arm开发板开发板是什么?如ARM开发板,DSP开发板等。。它和最终目标板有何区别?申请证书求高手教下怎么申请证书童之磊湖北中文在线数字出版有限公司怎么样?如何建立自己的网站怎么创建自己的网站腾讯文章怎样才能在手机腾讯网上发表文章?godaddyGO DADDY服务器空间域名怎么样硬盘人电脑对人有多大辐射?创维云电视功能很喜欢创维云电视,它到底有哪些独特功能?安装迅雷看看播放器怎样安装迅雷看看播放器
网站空间租用 南通服务器租用 我的世界服务器租用 独享100m fastdomain bandwagonhost 骨干网络 gspeed hostloc nerds 免费美国空间 爱奇艺vip免费领取 免费的asp空间 web应用服务器 lamp怎么读 深圳域名 网页加速 购买空间 博客域名 512内存 更多